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