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