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