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 % -------------------------------------------------------------------------------
5 \ifx\ucwmodule\undefined\else\endinput\fi
9 % We'll use internal macros of plain TeX
12 \ifx\eTeXversion\undefined
13 \errmessage{ucwmac requires the e-TeX engine or its successor}
16 %%% PDF output detection %%%
21 \ifx\pdfoutput\undefined
22 \else\ifnum\pdfoutput>0
24 \pdfpkresolution=600 % Provide a reasonable default
27 \ifx\directlua\undefined\else
28 % In LuaTeX \pdfpkresolution is not enough
29 \directlua{kpse.init_prog("luatex", 600, "ljfour")}
32 %%% Temporary registers %%%
37 %%% Auxiliary macros %%%
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}}
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}
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
51 \errmessage{Undefined control sequence #2}%
56 % Add \protected to an existing macro
57 \def\addprotected#1{\protected\edef#1{\expandafter\unexpanded\expandafter{#1}}}
62 \def\ucwwarn#1{\immediate\write16{*** UCWmac warning: #1 ***}}
64 %%% Page size and margins %%%
66 % If you modify these registers, call \setuppage afterwards
67 \ifx\luatexversion\undefined
68 % In LuaTeX, \pagewidth and \pageheight are primitive
75 \newdimen\bottommargin
76 \newdimen\evenpageshift
80 \advance\hsize by -\leftmargin
81 \advance\hsize by -\rightmargin
83 \advance\vsize by -\topmargin
84 \advance\vsize by -\bottommargin
86 \advance\hoffset by -1truein
88 \advance\voffset by -1truein
92 \ifx\luatexversion\undefined
93 \pdfpagewidth=\pagewidth
94 \pdfpageheight=\pageheight
99 % Set multiple margins to the same value
100 \def\sethmargins#1{\leftmargin=#1\relax\rightmargin=#1\relax\evenpageshift=0pt\relax}
101 \def\setvmargins#1{\topmargin=#1\relax\bottommargin=#1\relax}
102 \def\setmargins#1{\sethmargins{#1}\setvmargins{#1}}
104 % Define inner/outer margin instead of left/right
105 \def\setinneroutermargin#1#2{\leftmargin#1\relax\rightmargin#2\relax\evenpageshift=\rightmargin\advance\evenpageshift by -\leftmargin}
107 % Use a predefined paper format, calls \setuppage automagically
109 \expandafter\let\expandafter\currentpaper\csname paper-#1\endcsname
110 \ifx\currentpaper\relax
111 \errmessage{Undefined paper format #1}
116 % Switch to landscape orientation, calls \setuppage automagically
119 \pageheight=\pagewidth
125 \def\defpaper#1#2#3{\expandafter\def\csname paper-#1\endcsname{\pagewidth=#2\pageheight=#3\setuppage}}
126 \defpaper{a3}{297truemm}{420truemm}
127 \defpaper{a4}{210truemm}{297truemm}
128 \defpaper{a5}{148truemm}{210truemm}
129 \defpaper{letter}{8.5truein}{11truein}
130 \defpaper{legal}{8.5truein}{14truein}
132 % Default page parameters
136 %%% Macros with optional arguments %%%
138 % After \def\a{\withoptarg\b}, the macro \a behaves in this way:
139 % \a[arg] does \def\optarg{arg} and then it expands \b
140 % \a does \let\optarg=\relax and then it expands \b
141 \def\withoptarg#1{\let\xoptcall=#1\futurelet\next\xopt}
142 \def\xopt{\ifx\next[\expandafter\xoptwith\else\let\optarg=\relax\expandafter\xoptcall\fi}
143 \def\xoptwith[#1]{\def\optarg{#1}\xoptcall}
145 % A shortcut for defining macros with optional arguments:
146 % \optdef\macro behaves as \def\domacro, while \macro itself is defined
147 % as a wrapper calling \domacro using \withoptarg.
149 \edef\xoptname{\expandafter\eatbackslash\string#1}%
150 \edef#1{\noexpand\withoptarg\csname do\xoptname\endcsname}%
151 \expandafter\def\csname do\xoptname\endcsname
154 % Trick: \eatbackslash eats the next backslash of category 12
155 \begingroup\lccode`\+=`\\
156 \lowercase{\endgroup\def\eatbackslash+{}}
158 % Expand to the optional argument if it exists
159 \def\optargorempty{\ifx\optarg\relax\else\optarg\fi}
161 %%% Placing material at specified coordinates %%%
163 % Set all dimensions of a given box register to zero
164 \def\smashbox#1{\ht#1=0pt \dp#1=0pt \wd#1=0pt}
165 \long\def\smashedhbox#1{{\setbox0=\hbox{#1}\smashbox0\box0}}
166 \long\def\smashedvbox#1{{\setbox0=\vbox{#1}\smashbox0\box0}}
168 % Variants of \llap and \rlap working equally on both sides and/or vertically
169 \def\hlap#1{\hbox to 0pt{\hss #1\hss}}
170 \def\vlap#1{\vbox to 0pt{\vss #1\vss}}
171 \def\clap#1{\vlap{\hlap{#1}}}
173 % \placeat{right}{down}{hmaterial} places <hmaterial>, so that its
174 % reference point lies at the given position wrt. the current ref point
175 \long\def\placeat#1#2#3{\smashedhbox{\hskip #1\lower #2\hbox{#3}}}
177 % Like \vbox, but with reference point in the upper left corner
178 \long\def\vhang#1{\vtop{\hrule height 0pt\relax #1}}
180 % Like \vhang, but respecting interline skips
181 \long\def\vhanglines#1{\vtop{\hbox to 0pt{}#1}}
183 % Crosshair with reference point in its center
184 \def\crosshair#1{\clap{\vrule height 0.2pt width #1}\clap{\vrule height #1 width 0.2pt}}
186 %%% Output routine %%%
188 \newbox\pageunderlays
190 \newbox\commonunderlays
191 \newbox\commonoverlays
193 % In addition to the normal page contents, you can define page overlays
194 % and underlays, which are zero-size vboxes positioned absolutely in the
195 % front / in the back of the normal material. Also, there are global
196 % versions of both which are not reset after every page.
197 \def\addlay#1#2{\setbox#1=\vbox{\ifvbox#1\box#1\fi\nointerlineskip\smashedvbox{#2}}}
198 \def\pageunderlay{\addlay\pageunderlays}
199 \def\pageoverlay{\addlay\pageoverlays}
200 \def\commonunderlay{\addlay\commonoverlays}
201 \def\commonoverlay{\addlay\commonoverlays}
203 % Our variation on \plainoutput, which manages inner/outer margins and overlays
205 \newdimen\pagebodydepth
206 \def\ucwoutput{\wigglepage\shipout\vbox{%
208 \ifvbox\commonunderlays\copy\commonunderlays\nointerlineskip\fi
209 \ifvbox\pageunderlays\box\pageunderlays\nointerlineskip\fi
211 \pagebodydepth=\prevdepth
213 \ifvbox\commonoverlays\vbox to 0pt{\vskip -\vsize\copy\commonoverlays\vss}\nointerlineskip\fi
214 \ifvbox\pageoverlays\vbox to 0pt{\vskip -\vsize\box\pageoverlays\vss}\nointerlineskip\fi
215 \prevdepth=\pagebodydepth
218 \ifnum\outputpenalty>-\@MM \else\dosupereject\fi}
220 \def\wigglepage{\ifodd\pageno\else\advance\hoffset by \evenpageshift\fi}
222 % Make it easier to redefine footline font (also, fix it so that OFS won't change it unless asked)
224 \footline={\hss\footfont\folio\hss}
235 % Available styles (others can be defined by \sdef{item:<style>}{<marker>})
249 % Meta-styles (can be used to modify an arbitrary style, currently hard-wired)
251 % #. % with a dot behind
252 % #) % with a parenthesis behind
253 % (#) % enclosed in parentheses
257 % \itemize\ibull % or other marker
262 % \numlist\ndotted % or other numbering style
267 % Default dimensions of itemized lists
268 \newdimen\itemindent \itemindent=0.5in
269 \newdimen\itemnarrow \itemnarrow=0.5in % make lines narrower by this amount
270 \newskip\itemmarkerskip \itemmarkerskip=0.4em % between marker and the item
271 \newskip\preitemizeskip \preitemizeskip=3pt plus 2pt minus 1pt % before the list
272 \newskip\postitemizeskip \postitemizeskip=3pt plus 2pt minus 1pt % after the list
273 \newskip\interitemskip \interitemskip=2pt plus 1pt minus 0.5pt % between two items
275 % Analogues for nested lists
276 \newdimen\nesteditemindent \nesteditemindent=0.25in
277 \newdimen\nesteditemnarrow \nesteditemnarrow=0.25in
278 \newskip\prenesteditemizeskip \prenesteditemizeskip=0pt
279 \newskip\postnesteditemizeskip \postnesteditemizeskip=0pt
281 \newif\ifitems\itemsfalse
286 \newcount\preitemizepenalty \preitemizepenalty=-500
287 \newcount\postitemizepenalty \postitemizepenalty=-500
291 \vskip\prenesteditemizeskip
292 \advance\leftskip by \nesteditemindent
293 \advance\rightskip by \nesteditemnarrow
295 \ifnum\preitemizepenalty=0\else\penalty\preitemizepenalty\fi
296 \vskip\preitemizeskip
297 \advance\leftskip by \itemindent
298 \advance\rightskip by \itemnarrow
300 \parskip=\interitemskip
305 \vskip\postnesteditemizeskip
307 \ifnum\postitemizepenalty=0\else\penalty\postitemizepenalty\fi
308 \vskip\postitemizeskip
312 \def\inititemize{\begingroup\preitemize\itemstrue\parindent=0pt}
314 \def\list#1{\inititemize\itemcount=0\liststyle{#1}\let\:=\listitem}
315 \def\listitem{\par\leavevmode\advance\itemcount by 1
316 \llap{\listmarker\hskip\itemmarkerskip}\ignorespaces}
320 \ifcsname item:\markertmp\endcsname
321 \slet{listmarker}{item:\markertmp}%
323 \slet{listmarker}{metaitem:\markertometa#1^^X}%
324 \slet{markerinner}{item:\markertoinner#1^^X}%
328 \def\markertometa#1{%
336 \expandafter\markertometa
340 \def\markertoinner#1{%
348 \expandafter\markertoinner
352 \def\endlist{\par\endgroup\postitemize}
355 \sdef{item:o}{\raise0.2ex\hbox{$\bullet$}}
356 \sdef{item:O}{\raise0.2ex\hbox{$\circ$}}
357 \sdef{item:*}{\raise0.2ex\hbox{$\ast$}}
359 \sdef{item:.}{\raise0.2ex\hbox{$\cdot$}}
360 \sdef{item:n}{\the\itemcount}
361 \sdef{item:i}{\romannumeral\itemcount}
362 \sdef{item:I}{\uppercase\expandafter{\romannumeral\itemcount}}
363 \sdef{item:a}{\char\numexpr 96+\itemcount\relax}
364 \sdef{item:A}{\char\numexpr 64+\itemcount\relax}
365 \sdef{item:g}{$\ifcase\itemcount\or\alpha\or\beta\or\gamma\or\delta\or\epsilon\or
366 \zeta\or\eta\or\theta\or\iota\or\kappa\or\lambda\or\mu\or\nu\or\xi\or\pi\or\rho
367 \or\sigma\or\tau\or\upsilon\or\phi\or\chi\or\psi\or\omega\fi$}
370 \sdef{metaitem:=.}{\markerinner.}
371 \sdef{metaitem:=)}{\markerinner)}
372 \sdef{metaitem:(=)}{(\markerinner)}
376 \def\itemize#1{\inititemize\setbox\itembox\llap{#1\hskip\itemmarkerskip}%
379 \def\singleitem{\par\leavevmode\copy\itembox\ignorespaces}
381 \def\numlist#1{\inititemize\itemcount=0\let\:=\numbereditem
382 \let\itemnumbering=#1}
384 \def\numbereditem{\par\leavevmode\advance\itemcount by 1
385 \llap{\itemnumbering\hskip\itemmarkerskip}\ignorespaces}
389 \def\ibull{\raise0.2ex\hbox{$\bullet$}}
390 \def\idot{\raise0.2ex\hbox{$\cdot$}}
391 \def\istar{\raise0.2ex\hbox{$\ast$}}
393 \def\nnorm{\the\itemcount}
394 \def\ndotted{\nnorm.}
396 \def\nparenp{(\nnorm)}
397 \def\nroman{\romannumeral\itemcount}
398 \def\nromanp{\nroman)}
399 \def\nalpha{\count@=96\advance\count@ by\itemcount\char\count@)}
400 \def\nAlpha{\count@=64\advance\count@ by\itemcount\char\count@)}
401 \def\ngreek{$\ifcase\itemcount\or\alpha\or\beta\or\gamma\or\delta\or\epsilon\or
402 \zeta\or\eta\or\theta\or\iota\or\kappa\or\lambda\or\mu\or\nu\or\xi\or\pi\or\rho
403 \or\sigma\or\tau\or\upsilon\or\phi\or\chi\or\psi\or\omega\fi$)}
407 % {\I italic} with automatic italic correction
408 \def\I{\it\aftergroup\/}
410 % A breakable dash, to be repeated on the next line
411 \def\={\discretionary{-}{-}{-}}
413 % Non-breakable identifiers
414 \def\<#1>{\leavevmode\hbox{\I #1}}
420 % Variants of \centerline, \leftline and \rightline, which are compatible with
421 % verbatim environments and other catcode hacks
422 \def\cline{\bgroup\def\linet@mp{\aftergroup\box\aftergroup0\aftergroup\egroup\hss\bgroup\aftergroup\hss\aftergroup\egroup}\afterassignment\linet@mp\setbox0\hbox to \hsize}
423 \def\lline{\bgroup\def\linet@mp{\aftergroup\box\aftergroup0\aftergroup\egroup\bgroup\aftergroup\hss\aftergroup\egroup}\afterassignment\linet@mp\setbox0\hbox to \hsize}
424 \def\rline{\bgroup\def\linet@mp{\aftergroup\box\aftergroup0\aftergroup\egroup\hss\bgroup\aftergroup\egroup}\afterassignment\linet@mp\setbox0\hbox to \hsize}
426 % Insert a PDF picture
427 % \putimage{width specification}{file}
428 \def\putimage#1#2{\hbox{\pdfximage #1{#2}\pdfrefximage\pdflastximage}}
432 % Use of pdfTeX color stack:
433 % \colorpush\rgb{1 0 0} puts a new color on the stack
434 % \colorset\rgb{1 0 0} replaces the top color on the stack
435 % \colorpop pops the top color
436 % \colorlocal\rgb{1 0 0} set a color locally until the end of the current group
437 \chardef\colorstk=\pdfcolorstackinit page direct{0 g 0 G}
438 \def\colorset#1{\pdfcolorstack\colorstk set #1}
439 \def\colorpush#1{\pdfcolorstack\colorstk push #1}
440 \def\colorpop{\pdfcolorstack\colorstk pop}
441 \def\colorlocal{\aftergroup\colorpop\colorpush}
443 % Different ways of describing colors: \rgb{R G B}, \gray{G}, \cmyk{C M Y K}
444 % (all components are real numbers between 0 and 1)
445 \def\rgb#1{{#1 rg #1 RG}}
446 \def\gray#1{{#1 g #1 G}}
447 \def\cmyk#1{{#1 k #1 K}}
451 % Define a new localized string: \localedef{language}{identifier}{message}
452 % (we use \language codes to identify languages)
453 \def\localedef#1#2{\tmpcount=#1\expandafter\def\csname loc:\the\tmpcount:#2\endcsname}
455 % Expand a localized string in the current language: \localemsg{identifier}
457 \ifcsname loc:\the\language:#1\endcsname
458 \csname loc:\the\language:#1\endcsname
460 \ucwwarn{Localized string #1 not defined in language \the\language}%
467 % Require a module: load it if it is not already loaded
469 \ifcsname ucwmod:#1\endcsname
475 % Definition of a new module (to be placed at the beginning of its file)
476 % (Also guards against repeated loading if somebody uses \input instead of \ucwmodule.)
478 \ifcsname ucwmod:#1\endcsname\endinput\fi
479 \expandafter\let\csname ucwmod:#1\endcsname=\relax
484 % Let's hide all internal macros