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 %%% Auxiliary macros %%%
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}}
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}
42 \def\ucwwarn#1{\immediate\write16{*** Warning: #1 ***}}
44 %%% Page size and margins %%%
46 % If you modify these registers, call \setuppage afterwards
47 \ifx\luatexversion\undefined
48 % In LuaTeX, \pagewidth and \pageheight are primitive
55 \newdimen\bottommargin
56 \newdimen\evenpageshift
60 \advance\hsize by -\leftmargin
61 \advance\hsize by -\rightmargin
63 \advance\vsize by -\topmargin
64 \advance\vsize by -\bottommargin
66 \advance\hoffset by -1truein
68 \advance\voffset by -1truein
72 \ifx\luatexversion\undefined
73 \pdfpagewidth=\pagewidth
74 \pdfpageheight=\pageheight
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}}
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}
87 % Use a predefined paper format, calls \setuppage automagically
89 \expandafter\let\expandafter\currentpaper\csname paper-#1\endcsname
90 \ifx\currentpaper\relax
91 \errmessage{Undefined paper format #1}
96 % Switch to landscape orientation, calls \setuppage automagically
99 \pageheight=\pagewidth
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}
112 % Default page parameters
116 %%% Macros with optional arguments %%%
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}
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.
129 \edef\xoptname{\expandafter\eatbackslash\string#1}%
130 \edef#1{\noexpand\withoptarg\csname do\xoptname\endcsname}%
131 \expandafter\def\csname do\xoptname\endcsname
134 % Trick: \eatbackslash eats the next backslash of category 12
135 \begingroup\lccode`\+=`\\
136 \lowercase{\endgroup\def\eatbackslash+{}}
138 % Expand to the optional argument if it exists
139 \def\optargorempty{\ifx\optarg\relax\else\optarg\fi}
141 %%% Placing material at specified coordinates %%%
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}}
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}}}
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}}}
157 % Like \vbox, but with reference point in the upper left corner
158 \long\def\vhang#1{\vtop{\hrule height 0pt\relax #1}}
160 % Like \vhang, but respecting interline skips
161 \long\def\vhanglines#1{\vtop{\hbox to 0pt{}#1}}
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}}
166 %%% Output routine %%%
168 \newbox\pageunderlays
170 \newbox\commonunderlays
171 \newbox\commonoverlays
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}
183 % Our variation on \plainoutput, which manages inner/outer margins and overlays
185 \newdimen\pagebodydepth
186 \def\ucwoutput{\wigglepage\shipout\vbox{%
188 \ifvbox\commonunderlays\copy\commonunderlays\nointerlineskip\fi
189 \ifvbox\pageunderlays\box\pageunderlays\nointerlineskip\fi
191 \pagebodydepth=\prevdepth
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
198 \ifnum\outputpenalty>-\@MM \else\dosupereject\fi}
200 \def\wigglepage{\ifodd\pageno\else\advance\hoffset by \evenpageshift\fi}
202 % Make it easier to redefine footline font (also, fix it so that OFS won't change it unless asked)
204 \footline={\hss\footfont\folio\hss}
210 % \itemize\ibull % or other marker
215 % \numlist\ndotted % or other numbering style
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
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
234 \newif\ifitems\itemsfalse
238 % Penalties (changed at compatibility level 1)
239 \newcount\preitemizepenalty \preitemizepenalty=0
240 \newcount\postitemizepenalty \postitemizepenalty=0
244 \vskip\prenesteditemizeskip
245 \advance\leftskip by \nesteditemindent
246 \advance\rightskip by \nesteditemnarrow
248 \ifnum\preitemizepenalty=0\else\penalty\preitemizepenalty\fi
249 \vskip\preitemizeskip
250 \advance\leftskip by \itemindent
251 \advance\rightskip by \itemnarrow
253 \parskip=\interitemskip
258 \vskip\postnesteditemizeskip
260 \ifnum\postitemizepenalty=0\else\penalty\postitemizepenalty\fi
261 \vskip\postitemizeskip
265 \def\inititemize{\begingroup\preitemize\itemstrue\parindent=0pt}
267 \def\itemize#1{\inititemize\setbox\itembox\llap{#1\hskip\itemmarkerskip}%
270 \def\singleitem{\par\leavevmode\copy\itembox\ignorespaces}
272 \def\endlist{\par\endgroup\postitemize}
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$}}
279 \def\numlist#1{\inititemize\itemcount=0\let\:=\numbereditem
280 \let\itemnumbering=#1}
282 \def\numbereditem{\par\leavevmode\advance\itemcount by 1
283 \llap{\itemnumbering\hskip\itemmarkerskip}\ignorespaces}
285 % Numbering styles for \numlist
286 \def\nnorm{\the\itemcount}
287 \def\ndotted{\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$)}
300 % {\I italic} with automatic italic correction
301 \def\I{\it\aftergroup\/}
303 % A breakable dash, to be repeated on the next line
304 \def\={\discretionary{-}{-}{-}}
306 % Non-breakable identifiers
307 \def\<#1>{\leavevmode\hbox{\I #1}}
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}
319 % Insert a PDF picture
320 % \putimage{width specification}{file}
321 \def\putimage#1#2{\hbox{\pdfximage #1{#2}\pdfrefximage\pdflastximage}}
325 \protected\def~{\plaintilde}
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}
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}}
351 % Define a new localized string: \localedef{language}{identifier}{message}
352 \def\localedef#1#2{\expandafter\def\csname loc:#1:#2\endcsname}
354 % Expand a localized string in the current language: \localemsg{identifier}
356 \ifcsname loc:\localelang:#1\endcsname
357 \csname loc:\localelang:#1\endcsname
359 \ucwwarn{Localized string #1 not defined in language \localelang}%
366 % Require a module: load it if it is not already loaded
368 \ifcsname ucwmod:#1\endcsname
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.)
377 \ifcsname ucwmod:#1\endcsname\endinput\fi
378 \expandafter\let\csname ucwmod:#1\endcsname=\relax
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.
388 \chardef\ucwcurrentcompat=0 % Currently active compatibility level
389 \chardef\ucwmaxcompat=1 % Maximum supported compatibility level
394 % Level 0 (default): old ucwmac
396 \preitemizepenalty=-500
397 \postitemizepenalty=-500
398 \else\errmessage{Unsupported compatibility level #1 requested.}
404 % Let's hide all internal macros