]> mj.ucw.cz Git - ucwmac.git/blob - ucwmac2.tex
e7c48e8287cdd64668dc1a59c42a210e01627f8a
[ucwmac.git] / ucwmac2.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 \ifx\ucwmodule\undefined\else\endinput\fi
6
7 %%% Prolog %%%
8
9 % We'll use internal macros of plain TeX
10 \catcode`@=11
11
12 \ifx\eTeXversion\undefined
13 \errmessage{ucwmac requires the e-TeX engine or its successor}
14 \fi
15
16 %%% PDF output detection %%%
17
18 \newif\ifpdf
19 \pdffalse
20
21 \ifx\pdfoutput\undefined
22 \else\ifnum\pdfoutput>0
23         \pdftrue
24         \pdfpkresolution=600    % Provide a reasonable default
25 \fi\fi
26
27 \ifx\directlua\undefined\else
28         % In LuaTeX \pdfpkresolution is not enough
29         \directlua{kpse.init_prog("luatex", 600, "ljfour")}
30 \fi
31
32 %%% Temporary registers %%%
33
34 \newcount\tmpcount
35 \newdimen\tmpdimen
36
37 %%% Auxiliary macros %%%
38
39 % Prepend/append #2 to the definition of #1
40 \long\def\prependef#1#2{\expandafter\def\expandafter#1\expandafter{#2#1}}
41 \long\def\appendef#1#2{\expandafter\def\expandafter#1\expandafter{#1#2}}
42
43 % Variants of \def and \let, where the control sequence name is given as a string
44 \def\sdef#1{\expandafter\def\csname#1\endcsname}
45 \def\slet#1#2{\expandafter\let\csname#1\expandafter\endcsname\csname#2\endcsname}
46
47 % Add \protected to an existing macro
48 \def\addprotected#1{\protected\edef#1{\expandafter\unexpanded\expandafter{#1}}}
49
50 % Protect ~
51 \addprotected~
52
53 \def\ucwwarn#1{\immediate\write16{*** UCWmac warning: #1 ***}}
54
55 %%% Page size and margins %%%
56
57 % If you modify these registers, call \setuppage afterwards
58 \ifx\luatexversion\undefined
59         % In LuaTeX, \pagewidth and \pageheight are primitive
60         \newdimen\pagewidth
61         \newdimen\pageheight
62 \fi
63 \newdimen\leftmargin
64 \newdimen\rightmargin
65 \newdimen\topmargin
66 \newdimen\bottommargin
67 \newdimen\evenpageshift
68
69 \def\setuppage{%
70         \hsize=\pagewidth
71         \advance\hsize by -\leftmargin
72         \advance\hsize by -\rightmargin
73         \vsize=\pageheight
74         \advance\vsize by -\topmargin
75         \advance\vsize by -\bottommargin
76         \hoffset=\leftmargin
77         \advance\hoffset by -1truein
78         \voffset=\topmargin
79         \advance\voffset by -1truein
80         \ifpdf
81                 \pdfhorigin=1truein
82                 \pdfvorigin=1truein
83                 \ifx\luatexversion\undefined
84                         \pdfpagewidth=\pagewidth
85                         \pdfpageheight=\pageheight
86                 \fi
87         \fi
88 }
89
90 % Set multiple margins to the same value
91 \def\sethmargins#1{\leftmargin=#1\relax\rightmargin=#1\relax\evenpageshift=0pt\relax}
92 \def\setvmargins#1{\topmargin=#1\relax\bottommargin=#1\relax}
93 \def\setmargins#1{\sethmargins{#1}\setvmargins{#1}}
94
95 % Define inner/outer margin instead of left/right
96 \def\setinneroutermargin#1#2{\leftmargin#1\relax\rightmargin#2\relax\evenpageshift=\rightmargin\advance\evenpageshift by -\leftmargin}
97
98 % Use a predefined paper format, calls \setuppage automagically
99 \def\setpaper#1{%
100         \expandafter\let\expandafter\currentpaper\csname paper-#1\endcsname
101         \ifx\currentpaper\relax
102                 \errmessage{Undefined paper format #1}
103         \fi
104         \currentpaper
105 }
106
107 % Switch to landscape orientation, calls \setuppage automagically
108 \def\landscape{%
109         \dimen0=\pageheight
110         \pageheight=\pagewidth
111         \pagewidth=\dimen0
112         \setuppage
113 }
114
115 % Common paper sizes
116 \def\defpaper#1#2#3{\expandafter\def\csname paper-#1\endcsname{\pagewidth=#2\pageheight=#3\setuppage}}
117 \defpaper{a3}{297truemm}{420truemm}
118 \defpaper{a4}{210truemm}{297truemm}
119 \defpaper{a5}{148truemm}{210truemm}
120 \defpaper{letter}{8.5truein}{11truein}
121 \defpaper{legal}{8.5truein}{14truein}
122
123 % Default page parameters
124 \setmargins{1truein}
125 \setpaper{a4}
126
127 %%% Macros with optional arguments %%%
128
129 % After \def\a{\withoptarg\b}, the macro \a behaves in this way:
130 %       \a[arg]         does \def\optarg{arg} and then it expands \b
131 %       \a              does \let\optarg=\relax and then it expands \b
132 \def\withoptarg#1{\let\xoptcall=#1\futurelet\next\xopt}
133 \def\xopt{\ifx\next[\expandafter\xoptwith\else\let\optarg=\relax\expandafter\xoptcall\fi}
134 \def\xoptwith[#1]{\def\optarg{#1}\xoptcall}
135
136 % A shortcut for defining macros with optional arguments:
137 % \optdef\macro behaves as \def\domacro, while \macro itself is defined
138 % as a wrapper calling \domacro using \withoptarg.
139 \def\optdef#1{%
140         \edef\xoptname{\expandafter\eatbackslash\string#1}%
141         \edef#1{\noexpand\withoptarg\csname do\xoptname\endcsname}%
142         \expandafter\def\csname do\xoptname\endcsname
143 }
144
145 % Trick: \eatbackslash eats the next backslash of category 12
146 \begingroup\lccode`\+=`\\
147 \lowercase{\endgroup\def\eatbackslash+{}}
148
149 % Expand to the optional argument if it exists
150 \def\optargorempty{\ifx\optarg\relax\else\optarg\fi}
151
152 %%% Placing material at specified coordinates %%%
153
154 % Set all dimensions of a given box register to zero
155 \def\smashbox#1{\ht#1=0pt \dp#1=0pt \wd#1=0pt}
156 \long\def\smashedhbox#1{{\setbox0=\hbox{#1}\smashbox0\box0}}
157 \long\def\smashedvbox#1{{\setbox0=\vbox{#1}\smashbox0\box0}}
158
159 % Variants of \llap and \rlap working equally on both sides and/or vertically
160 \def\hlap#1{\hbox to 0pt{\hss #1\hss}}
161 \def\vlap#1{\vbox to 0pt{\vss #1\vss}}
162 \def\clap#1{\vlap{\hlap{#1}}}
163
164 % \placeat{right}{down}{hmaterial} places <hmaterial>, so that its
165 % reference point lies at the given position wrt. the current ref point
166 \long\def\placeat#1#2#3{\smashedhbox{\hskip #1\lower #2\hbox{#3}}}
167
168 % Like \vbox, but with reference point in the upper left corner
169 \long\def\vhang#1{\vtop{\hrule height 0pt\relax #1}}
170
171 % Like \vhang, but respecting interline skips
172 \long\def\vhanglines#1{\vtop{\hbox to 0pt{}#1}}
173
174 % Crosshair with reference point in its center
175 \def\crosshair#1{\clap{\vrule height 0.2pt width #1}\clap{\vrule height #1 width 0.2pt}}
176
177 %%% Output routine %%%
178
179 \newbox\pageunderlays
180 \newbox\pageoverlays
181 \newbox\commonunderlays
182 \newbox\commonoverlays
183
184 % In addition to the normal page contents, you can define page overlays
185 % and underlays, which are zero-size vboxes positioned absolutely in the
186 % front / in the back of the normal material. Also, there are global
187 % versions of both which are not reset after every page.
188 \def\addlay#1#2{\setbox#1=\vbox{\ifvbox#1\box#1\fi\nointerlineskip\smashedvbox{#2}}}
189 \def\pageunderlay{\addlay\pageunderlays}
190 \def\pageoverlay{\addlay\pageoverlays}
191 \def\commonunderlay{\addlay\commonoverlays}
192 \def\commonoverlay{\addlay\commonoverlays}
193
194 % Our variation on \plainoutput, which manages inner/outer margins and overlays
195 \output{\ucwoutput}
196 \newdimen\pagebodydepth
197 \def\ucwoutput{\wigglepage\shipout\vbox{%
198         \makeheadline
199         \ifvbox\commonunderlays\copy\commonunderlays\nointerlineskip\fi
200         \ifvbox\pageunderlays\box\pageunderlays\nointerlineskip\fi
201         \pagebody
202         \pagebodydepth=\prevdepth
203         \nointerlineskip
204         \ifvbox\commonoverlays\vbox to 0pt{\vskip -\vsize\copy\commonoverlays\vss}\nointerlineskip\fi
205         \ifvbox\pageoverlays\vbox to 0pt{\vskip -\vsize\box\pageoverlays\vss}\nointerlineskip\fi
206         \prevdepth=\pagebodydepth
207         \makefootline
208 }\advancepageno
209 \ifnum\outputpenalty>-\@MM \else\dosupereject\fi}
210
211 \def\wigglepage{\ifodd\pageno\else\advance\hoffset by \evenpageshift\fi}
212
213 % Make it easier to redefine footline font (also, fix it so that OFS won't change it unless asked)
214 \let\footfont=\tenrm
215 \footline={\hss\footfont\folio\hss}
216
217 %%% Itemization %%%
218
219 % Usage:
220 %
221 % \list{style}
222 % \:first item
223 % \:second item
224 % \endlist
225 %
226 % Available styles (others can be defined by \sdef{item:<style>}{<marker>})
227 %
228 %       o               % bullet
229 %       O               % empty circle
230 %       *               % asterisk
231 %       -               % en-dash
232 %       .               % dot
233 %       n               % 1, 2, 3
234 %       i               % i, ii, iii
235 %       I               % I, II, III
236 %       a               % a, b, c
237 %       A               % A, B, C
238 %       g               % α, β, γ
239 %
240 % Meta-styles (can be used to modify an arbitrary style, currently hard-wired)
241 %
242 %       #.              % with a dot behind
243 %       #)              % with a parenthesis behind
244 %       (#)             % enclosed in parentheses
245 %
246 % Historic usage:
247 %
248 % \itemize\ibull        % or other marker
249 % \:first item
250 % \:second item
251 % \endlist
252 %
253 % \numlist\ndotted      % or other numbering style
254 % \:first
255 % \:second
256 % \endlist
257
258 % Default dimensions of itemized lists
259 \newdimen\itemindent            \itemindent=0.5in
260 \newdimen\itemnarrow            \itemnarrow=0.5in                       % make lines narrower by this amount
261 \newskip\itemmarkerskip         \itemmarkerskip=0.4em                   % between marker and the item
262 \newskip\preitemizeskip         \preitemizeskip=3pt plus 2pt minus 1pt  % before the list
263 \newskip\postitemizeskip        \postitemizeskip=3pt plus 2pt minus 1pt % after the list
264 \newskip\interitemskip          \interitemskip=2pt plus 1pt minus 0.5pt % between two items
265
266 % Analogues for nested lists
267 \newdimen\nesteditemindent      \nesteditemindent=0.25in
268 \newdimen\nesteditemnarrow      \nesteditemnarrow=0.25in
269 \newskip\prenesteditemizeskip   \prenesteditemizeskip=0pt
270 \newskip\postnesteditemizeskip  \postnesteditemizeskip=0pt
271
272 \newif\ifitems\itemsfalse
273 \newbox\itembox
274 \newcount\itemcount
275
276 % Penalties
277 \newcount\preitemizepenalty     \preitemizepenalty=-500
278 \newcount\postitemizepenalty    \postitemizepenalty=-500
279
280 \def\preitemize{
281         \ifitems
282                 \vskip\prenesteditemizeskip
283                 \advance\leftskip by \nesteditemindent
284                 \advance\rightskip by \nesteditemnarrow
285         \else
286                 \ifnum\preitemizepenalty=0\else\penalty\preitemizepenalty\fi
287                 \vskip\preitemizeskip
288                 \advance\leftskip by \itemindent
289                 \advance\rightskip by \itemnarrow
290         \fi
291         \parskip=\interitemskip
292 }
293
294 \def\postitemize{
295         \ifitems
296                 \vskip\postnesteditemizeskip
297         \else
298                 \ifnum\postitemizepenalty=0\else\penalty\postitemizepenalty\fi
299                 \vskip\postitemizeskip
300         \fi
301 }
302
303 \def\inititemize{\begingroup\preitemize\itemstrue\parindent=0pt}
304
305 \def\list#1{\inititemize\itemcount=0\liststyle{#1}\let\:=\listitem}
306 \def\listitem{\par\leavevmode\advance\itemcount by 1
307         \llap{\listmarker\hskip\itemmarkerskip}\ignorespaces}
308
309 \def\liststyle#1{%
310         \edef\markertmp{#1}
311         \ifcsname item:\markertmp\endcsname
312                 \slet{listmarker}{item:\markertmp}%
313         \else
314                 \slet{listmarker}{metaitem:\markertometa#1^^X}%
315                 \slet{markerinner}{item:\markertoinner#1^^X}%
316         \fi
317 }
318
319 \def\markertometa#1{%
320         \ifx#1^^X%
321         \else
322                 \ifx#1((%
323                 \else\ifx#1))%
324                 \else\ifx#1..%
325                 \else=%
326                 \fi\fi\fi
327                 \expandafter\markertometa
328         \fi
329 }
330
331 \def\markertoinner#1{%
332         \ifx#1^^X%
333         \else
334                 \ifx#1(%
335                 \else\ifx#1)%
336                 \else\ifx#1.%
337                 \else#1%
338                 \fi\fi\fi
339                 \expandafter\markertoinner
340         \fi
341 }
342
343 \def\itemize#1{\inititemize\setbox\itembox\llap{#1\hskip\itemmarkerskip}%
344 \let\:=\singleitem}
345
346 \def\singleitem{\par\leavevmode\copy\itembox\ignorespaces}
347
348 \def\endlist{\par\endgroup\postitemize}
349
350 % Markers for \itemize
351 \def\ibull{\raise0.2ex\hbox{$\bullet$}}
352 \def\idot{\raise0.2ex\hbox{$\cdot$}}
353 \def\istar{\raise0.2ex\hbox{$\ast$}}
354
355 \def\numlist#1{\inititemize\itemcount=0\let\:=\numbereditem
356 \let\itemnumbering=#1}
357
358 \def\numbereditem{\par\leavevmode\advance\itemcount by 1
359 \llap{\itemnumbering\hskip\itemmarkerskip}\ignorespaces}
360
361 % Numbering styles for \numlist
362 \def\nnorm{\the\itemcount}
363 \def\ndotted{\nnorm.}
364 \def\nparen{\nnorm)}
365 \def\nparenp{(\nnorm)}
366 \def\nroman{\romannumeral\itemcount}
367 \def\nromanp{\nroman)}
368 \def\nalpha{\count@=96\advance\count@ by\itemcount\char\count@)}
369 \def\nAlpha{\count@=64\advance\count@ by\itemcount\char\count@)}
370 \def\ngreek{$\ifcase\itemcount\or\alpha\or\beta\or\gamma\or\delta\or\epsilon\or
371 \zeta\or\eta\or\theta\or\iota\or\kappa\or\lambda\or\mu\or\nu\or\xi\or\pi\or\rho
372 \or\sigma\or\tau\or\upsilon\or\phi\or\chi\or\psi\or\omega\fi$)}
373
374 % List styles
375 \sdef{item:o}{\raise0.2ex\hbox{$\bullet$}}
376 \sdef{item:O}{\raise0.2ex\hbox{$\circ$}}
377 \sdef{item:*}{\raise0.2ex\hbox{$\ast$}}
378 \sdef{item:-}{--}
379 \sdef{item:.}{\raise0.2ex\hbox{$\cdot$}}
380 \sdef{item:n}{\the\itemcount}
381 \sdef{item:i}{\romannumeral\itemcount}
382 \sdef{item:I}{\uppercase\expandafter{\romannumeral\itemcount}}
383 \sdef{item:a}{\char\numexpr 96+\itemcount\relax}
384 \sdef{item:A}{\char\numexpr 64+\itemcount\relax}
385 \sdef{item:g}{$\ifcase\itemcount\or\alpha\or\beta\or\gamma\or\delta\or\epsilon\or
386 \zeta\or\eta\or\theta\or\iota\or\kappa\or\lambda\or\mu\or\nu\or\xi\or\pi\or\rho
387 \or\sigma\or\tau\or\upsilon\or\phi\or\chi\or\psi\or\omega\fi$}
388
389 % List meta-styles
390 \sdef{metaitem:=.}{\markerinner.}
391 \sdef{metaitem:=)}{\markerinner)}
392 \sdef{metaitem:(=)}{(\markerinner)}
393
394 %%% Miscellanea %%%
395
396 % {\I italic} with automatic italic correction
397 \def\I{\it\aftergroup\/}
398
399 % A breakable dash, to be repeated on the next line
400 \def\={\discretionary{-}{-}{-}}
401
402 % Non-breakable identifiers
403 \def\<#1>{\leavevmode\hbox{\I #1}}
404
405 % Handy shortcuts
406 \let\>=\noindent
407 \def\\{\hfil\break}
408
409 % Variants of \centerline, \leftline and \rightline, which are compatible with
410 % verbatim environments and other catcode hacks
411 \def\cline{\bgroup\def\linet@mp{\aftergroup\box\aftergroup0\aftergroup\egroup\hss\bgroup\aftergroup\hss\aftergroup\egroup}\afterassignment\linet@mp\setbox0\hbox to \hsize}
412 \def\lline{\bgroup\def\linet@mp{\aftergroup\box\aftergroup0\aftergroup\egroup\bgroup\aftergroup\hss\aftergroup\egroup}\afterassignment\linet@mp\setbox0\hbox to \hsize}
413 \def\rline{\bgroup\def\linet@mp{\aftergroup\box\aftergroup0\aftergroup\egroup\hss\bgroup\aftergroup\egroup}\afterassignment\linet@mp\setbox0\hbox to \hsize}
414
415 % Insert a PDF picture
416 % \putimage{width specification}{file}
417 \def\putimage#1#2{\hbox{\pdfximage #1{#2}\pdfrefximage\pdflastximage}}
418
419 %%% Colors %%%
420
421 % Use of pdfTeX color stack:
422 % \colorpush\rgb{1 0 0} puts a new color on the stack
423 % \colorset\rgb{1 0 0} replaces the top color on the stack
424 % \colorpop pops the top color
425 % \colorlocal\rgb{1 0 0} set a color locally until the end of the current group
426 \chardef\colorstk=\pdfcolorstackinit page direct{0 g 0 G}
427 \def\colorset#1{\pdfcolorstack\colorstk set #1}
428 \def\colorpush#1{\pdfcolorstack\colorstk push #1}
429 \def\colorpop{\pdfcolorstack\colorstk pop}
430 \def\colorlocal{\aftergroup\colorpop\colorpush}
431
432 % Different ways of describing colors: \rgb{R G B}, \gray{G}, \cmyk{C M Y K}
433 % (all components are real numbers between 0 and 1)
434 \def\rgb#1{{#1 rg #1 RG}}
435 \def\gray#1{{#1 g #1 G}}
436 \def\cmyk#1{{#1 k #1 K}}
437
438 %%% Localization %%%
439
440 % Define a new localized string: \localedef{language}{identifier}{message}
441 % (we use \language codes to identify languages)
442 \def\localedef#1#2{\tmpcount=#1\expandafter\def\csname loc:\the\tmpcount:#2\endcsname}
443
444 % Expand a localized string in the current language: \localemsg{identifier}
445 \def\localestr#1{%
446         \ifcsname loc:\the\language:#1\endcsname
447                 \csname loc:\the\language:#1\endcsname
448         \else
449                 \ucwwarn{Localized string #1 not defined in language \the\language}%
450                 ???%
451         \fi
452 }
453
454 %%% Modules %%%
455
456 % Require a module: load it if it is not already loaded
457 \def\ucwmodule#1{
458         \ifcsname ucwmod:#1\endcsname
459         \else
460                 \input ucw-#1.tex
461         \fi
462 }
463
464 % Definition of a new module (to be placed at the beginning of its file)
465 % (Also guards against repeated loading if somebody uses \input instead of \ucwmodule.)
466 \def\ucwdefmodule#1{
467         \ifcsname ucwmod:#1\endcsname\endinput\fi
468         \expandafter\let\csname ucwmod:#1\endcsname=\relax
469 }
470
471 %%% Epilog %%%
472
473 % Let's hide all internal macros
474 \catcode`@=12