]> mj.ucw.cz Git - ucwmac.git/blob - ucwmac.tex
Let ~ be protected
[ucwmac.git] / ucwmac.tex
1 % The UCW Macro Collection (a successor of mjmac.tex)
2 % Written by Martin Mares <mj@ucw.cz> in 2010--2018 and placed into public domain
3 % -------------------------------------------------------------------------------
4
5 %%% Prolog %%%
6
7 % We'll use internal macros of plain TeX
8 \catcode`@=11
9
10 \ifx\eTeXversion\undefined
11 \errmessage{ucwmac requires the e-TeX engine or its successor}
12 \fi
13
14 %%% PDF output detection %%%
15
16 \newif\ifpdf
17 \pdffalse
18
19 \ifx\pdfoutput\undefined
20 \else\ifnum\pdfoutput>0
21         \pdftrue
22         \pdfpkresolution=600    % Provide a reasonable default
23 \fi\fi
24
25 %%% Page size and margins %%%
26
27 % If you modify these registers, call \setuppage afterwards
28 \ifx\luatexversion\undefined
29         % In LuaTeX, \pagewidth and \pageheight are primitive
30         \newdimen\pagewidth
31         \newdimen\pageheight
32 \fi
33 \newdimen\leftmargin
34 \newdimen\rightmargin
35 \newdimen\topmargin
36 \newdimen\bottommargin
37 \newdimen\evenpageshift
38
39 \def\setuppage{%
40         \hsize=\pagewidth
41         \advance\hsize by -\leftmargin
42         \advance\hsize by -\rightmargin
43         \vsize=\pageheight
44         \advance\vsize by -\topmargin
45         \advance\vsize by -\bottommargin
46         \hoffset=\leftmargin
47         \advance\hoffset by -1truein
48         \voffset=\topmargin
49         \advance\voffset by -1truein
50         \ifpdf
51                 \pdfhorigin=1truein
52                 \pdfvorigin=1truein
53                 \ifx\luatexversion\undefined
54                         \pdfpagewidth=\pagewidth
55                         \pdfpageheight=\pageheight
56                 \fi
57         \fi
58 }
59
60 % Set multiple margins to the same value
61 \def\sethmargins#1{\leftmargin=#1\relax\rightmargin=#1\relax\evenpageshift=0pt\relax}
62 \def\setvmargins#1{\topmargin=#1\relax\bottommargin=#1\relax}
63 \def\setmargins#1{\sethmargins{#1}\setvmargins{#1}}
64
65 % Define inner/outer margin instead of left/right
66 \def\setinneroutermargin#1#2{\leftmargin#1\relax\rightmargin#2\relax\evenpageshift=\rightmargin\advance\evenpageshift by -\leftmargin}
67
68 % Use a predefined paper format, calls \setuppage automagically
69 \def\setpaper#1{%
70         \expandafter\let\expandafter\currentpaper\csname paper-#1\endcsname
71         \ifx\currentpaper\relax
72                 \errmessage{Undefined paper format #1}
73         \fi
74         \currentpaper
75 }
76
77 % Switch to landscape orientation, calls \setuppage automagically
78 \def\landscape{%
79         \dimen0=\pageheight
80         \pageheight=\pagewidth
81         \pagewidth=\dimen0
82         \setuppage
83 }
84
85 % Common paper sizes
86 \def\defpaper#1#2#3{\expandafter\def\csname paper-#1\endcsname{\pagewidth=#2\pageheight=#3\setuppage}}
87 \defpaper{a3}{297truemm}{420truemm}
88 \defpaper{a4}{210truemm}{297truemm}
89 \defpaper{a5}{148truemm}{210truemm}
90 \defpaper{letter}{8.5truein}{11truein}
91 \defpaper{legal}{8.5truein}{14truein}
92
93 % Default page parameters
94 \setmargins{1truein}
95 \setpaper{a4}
96
97 %%% Placing material at specified coordinates %%%
98
99 % Set all dimensions of a given box register to zero
100 \def\smashbox#1{\ht#1=0pt \dp#1=0pt \wd#1=0pt}
101 \long\def\smashedhbox#1{{\setbox0=\hbox{#1}\smashbox0\box0}}
102 \long\def\smashedvbox#1{{\setbox0=\vbox{#1}\smashbox0\box0}}
103
104 % Variants of \llap and \rlap working equally on both sides and/or vertically
105 \def\hlap#1{\hbox to 0pt{\hss #1\hss}}
106 \def\vlap#1{\vbox to 0pt{\vss #1\vss}}
107 \def\clap#1{\vlap{\hlap{#1}}}
108
109 % \placeat{right}{down}{hmaterial} places <hmaterial>, so that its
110 % reference point lies at the given position wrt. the current ref point
111 \long\def\placeat#1#2#3{\smashedhbox{\hskip #1\lower #2\hbox{#3}}}
112
113 % Like \vbox, but with reference point in the upper left corner
114 \long\def\vhang#1{\vtop{\hrule height 0pt\relax #1}}
115
116 % Like \vhang, but respecting interline skips
117 \long\def\vhanglines#1{\vtop{\hbox to 0pt{}#1}}
118
119 % Crosshair with reference point in its center
120 \def\crosshair#1{\clap{\vrule height 0.2pt width #1}\clap{\vrule height #1 width 0.2pt}}
121
122 %%% Output routine %%%
123
124 \newbox\pageunderlays
125 \newbox\pageoverlays
126 \newbox\commonunderlays
127 \newbox\commonoverlays
128
129 % In addition to the normal page contents, you can define page overlays
130 % and underlays, which are zero-size vboxes positioned absolutely in the
131 % front / in the back of the normal material. Also, there are global
132 % versions of both which are not reset after every page.
133 \def\addlay#1#2{\setbox#1=\vbox{\ifvbox#1\box#1\fi\nointerlineskip\smashedvbox{#2}}}
134 \def\pageunderlay{\addlay\pageunderlays}
135 \def\pageoverlay{\addlay\pageoverlays}
136 \def\commonunderlay{\addlay\commonoverlays}
137 \def\commonoverlay{\addlay\commonoverlays}
138
139 % Our variation on \plainoutput, which manages inner/outer margins and overlays
140 \output{\ucwoutput}
141 \newdimen\pagebodydepth
142 \def\ucwoutput{\wigglepage\shipout\vbox{%
143         \makeheadline
144         \ifvbox\commonunderlays\copy\commonunderlays\nointerlineskip\fi
145         \ifvbox\pageunderlays\box\pageunderlays\nointerlineskip\fi
146         \pagebody
147         \pagebodydepth=\prevdepth
148         \nointerlineskip
149         \ifvbox\commonoverlays\vbox to 0pt{\vskip -\vsize\copy\commonoverlays\vss}\nointerlineskip\fi
150         \ifvbox\pageoverlays\vbox to 0pt{\vskip -\vsize\box\pageoverlays\vss}\nointerlineskip\fi
151         \prevdepth=\pagebodydepth
152         \makefootline
153 }\advancepageno
154 \ifnum\outputpenalty>-\@MM \else\dosupereject\fi}
155
156 \def\wigglepage{\ifodd\pageno\else\advance\hoffset by \evenpageshift\fi}
157
158 % Make it easier to redefine footline font (also, fix it so that OFS won't change it unless asked)
159 \let\footfont=\tenrm
160 \footline={\hss\footfont\folio\hss}
161
162 %%% Itemization %%%
163
164 % Default dimensions of itemized lists
165 \newdimen\itemindent            \itemindent=0.5in
166 \newdimen\itemnarrow            \itemnarrow=0.5in                       % make lines narrower by this amount
167 \newskip\itemmarkerskip         \itemmarkerskip=0.4em                   % between marker and the item
168 \newskip\preitemizeskip         \preitemizeskip=3pt plus 2pt minus 1pt  % before the list
169 \newskip\postitemizeskip        \postitemizeskip=3pt plus 2pt minus 1pt % after the list
170 \newskip\interitemskip          \interitemskip=2pt plus 1pt minus 0.5pt % between two items
171
172 % Analogues for nested lists
173 \newdimen\nesteditemindent      \nesteditemindent=0.25in
174 \newdimen\nesteditemnarrow      \nesteditemnarrow=0.25in
175 \newskip\prenesteditemizeskip   \prenesteditemizeskip=0pt
176 \newskip\postnesteditemizeskip  \postnesteditemizeskip=0pt
177
178 \newif\ifitems\itemsfalse
179 \newbox\itembox
180 \newcount\itemcount
181
182 \def\preitemize{
183         \ifitems
184                 \vskip\prenesteditemizeskip
185                 \advance\leftskip by \nesteditemindent
186                 \advance\rightskip by \nesteditemnarrow
187         \else
188                 \vskip\preitemizeskip
189                 \advance\leftskip by \itemindent
190                 \advance\rightskip by \itemnarrow
191         \fi
192         \parskip=\interitemskip
193 }
194
195 \def\postitemize{
196         \ifitems
197                 \vskip\postnesteditemizeskip
198         \else
199                 \vskip\postitemizeskip
200         \fi
201 }
202
203 \def\inititemize{\begingroup\preitemize\itemstrue\parindent=0pt}
204
205 \def\itemize#1{\inititemize\setbox\itembox\llap{#1\hskip\itemmarkerskip}%
206 \let\:=\singleitem}
207
208 \def\singleitem{\par\leavevmode\copy\itembox\ignorespaces}
209
210 \def\endlist{\par\endgroup\postitemize}
211
212 \def\ibull{\raise0.2ex\hbox{$\bullet$}} % Signs frequently used for \itemize
213 \def\idot{\raise0.2ex\hbox{$\cdot$}}
214 \def\istar{\raise0.2ex\hbox{$\ast$}}
215
216 \def\numlist#1{\inititemize\itemcount=0\let\:=\numbereditem
217 \let\itemnumbering=#1}
218
219 \def\numbereditem{\par\leavevmode\advance\itemcount by 1
220 \llap{\itemnumbering\hskip\itemmarkerskip}\ignorespaces}
221
222 \def\nnorm{\the\itemcount}
223 \def\ndotted{\nnorm.}
224 \def\nparen{\nnorm)}
225 \def\nparenp{(\nnorm)}
226 \def\nroman{\romannumeral\itemcount}
227 \def\nromanp{\nroman)}
228 \def\nalpha{\count@=96\advance\count@ by\itemcount\char\count@)}
229 \def\nAlpha{\count@=64\advance\count@ by\itemcount\char\count@)}
230 \def\ngreek{$\ifcase\itemcount\or\alpha\or\beta\or\gamma\or\delta\or\epsilon\or
231 \zeta\or\eta\or\theta\or\iota\or\kappa\or\lambda\or\mu\or\nu\or\xi\or\pi\or\rho
232 \or\sigma\or\tau\or\upsilon\or\phi\or\chi\or\psi\or\omega\fi$)}
233
234 %%% Miscellanea %%%
235
236 % {\I italic} with automatic italic correction
237 \def\I{\it\aftergroup\/}
238
239 % A breakable dash, to be repeated on the next line
240 \def\={\discretionary{-}{-}{-}}
241
242 % Non-breakable identifiers
243 \def\<#1>{\leavevmode\hbox{\I #1}}
244
245 % A handy shortcut
246 \let\>=\noindent
247
248 % Variants of \centerline, \leftline and \rightline, which are compatible with
249 % verbatim environments and other catcode hacks
250 \def\cline{\bgroup\def\linet@mp{\aftergroup\box\aftergroup0\aftergroup\egroup\hss\bgroup\aftergroup\hss\aftergroup\egroup}\afterassignment\linet@mp\setbox0\hbox to \hsize}
251 \def\lline{\bgroup\def\linet@mp{\aftergroup\box\aftergroup0\aftergroup\egroup\bgroup\aftergroup\hss\aftergroup\egroup}\afterassignment\linet@mp\setbox0\hbox to \hsize}
252 \def\rline{\bgroup\def\linet@mp{\aftergroup\box\aftergroup0\aftergroup\egroup\hss\bgroup\aftergroup\egroup}\afterassignment\linet@mp\setbox0\hbox to \hsize}
253
254 % Insert a PDF picture
255 % \putimage{width specification}{file}
256 \def\putimage#1#2{\hbox{\pdfximage #1{#2}\pdfrefximage\pdflastximage}}
257
258 % Let ~ be protected
259 \let\plaintilde=~
260 \protected\def~{\plaintilde}
261
262 %%% Epilog %%%
263
264 % Let's hide all internal macros
265 \catcode`@=12