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 \def\ucwwarn#1{\immediate\write16{*** Warning: #1 ***}}
40 %%% Page size and margins %%%
42 % If you modify these registers, call \setuppage afterwards
43 \ifx\luatexversion\undefined
44 % In LuaTeX, \pagewidth and \pageheight are primitive
51 \newdimen\bottommargin
52 \newdimen\evenpageshift
56 \advance\hsize by -\leftmargin
57 \advance\hsize by -\rightmargin
59 \advance\vsize by -\topmargin
60 \advance\vsize by -\bottommargin
62 \advance\hoffset by -1truein
64 \advance\voffset by -1truein
68 \ifx\luatexversion\undefined
69 \pdfpagewidth=\pagewidth
70 \pdfpageheight=\pageheight
75 % Set multiple margins to the same value
76 \def\sethmargins#1{\leftmargin=#1\relax\rightmargin=#1\relax\evenpageshift=0pt\relax}
77 \def\setvmargins#1{\topmargin=#1\relax\bottommargin=#1\relax}
78 \def\setmargins#1{\sethmargins{#1}\setvmargins{#1}}
80 % Define inner/outer margin instead of left/right
81 \def\setinneroutermargin#1#2{\leftmargin#1\relax\rightmargin#2\relax\evenpageshift=\rightmargin\advance\evenpageshift by -\leftmargin}
83 % Use a predefined paper format, calls \setuppage automagically
85 \expandafter\let\expandafter\currentpaper\csname paper-#1\endcsname
86 \ifx\currentpaper\relax
87 \errmessage{Undefined paper format #1}
92 % Switch to landscape orientation, calls \setuppage automagically
95 \pageheight=\pagewidth
101 \def\defpaper#1#2#3{\expandafter\def\csname paper-#1\endcsname{\pagewidth=#2\pageheight=#3\setuppage}}
102 \defpaper{a3}{297truemm}{420truemm}
103 \defpaper{a4}{210truemm}{297truemm}
104 \defpaper{a5}{148truemm}{210truemm}
105 \defpaper{letter}{8.5truein}{11truein}
106 \defpaper{legal}{8.5truein}{14truein}
108 % Default page parameters
112 %%% Macros with optional arguments %%%
114 % After \def\a{\withoptarg\b}, the macro \a behaves in this way:
115 % \a[arg] does \def\optarg{arg} and then it expands \b
116 % \a does \let\optarg=\relax and then it expands \b
117 \def\withoptarg#1{\let\xoptcall=#1\futurelet\next\xopt}
118 \def\xopt{\ifx\next[\expandafter\xoptwith\else\let\optarg=\relax\expandafter\xoptcall\fi}
119 \def\xoptwith[#1]{\def\optarg{#1}\xoptcall}
121 % A shortcut for defining macros with optional arguments:
122 % \optdef\macro behaves as \def\domacro, while \macro itself is defined
123 % as a wrapper calling \domacro using \withoptarg.
125 \edef\xoptname{\expandafter\eatbackslash\string#1}%
126 \edef#1{\noexpand\withoptarg\csname do\xoptname\endcsname}%
127 \expandafter\def\csname do\xoptname\endcsname
130 % Trick: \eatbackslash eats the next backslash of category 12
131 \begingroup\lccode`\+=`\\
132 \lowercase{\endgroup\def\eatbackslash+{}}
134 % Expand to the optional argument if it exists
135 \def\optargorempty{\ifx\optarg\relax\else\optarg\fi}
137 %%% Placing material at specified coordinates %%%
139 % Set all dimensions of a given box register to zero
140 \def\smashbox#1{\ht#1=0pt \dp#1=0pt \wd#1=0pt}
141 \long\def\smashedhbox#1{{\setbox0=\hbox{#1}\smashbox0\box0}}
142 \long\def\smashedvbox#1{{\setbox0=\vbox{#1}\smashbox0\box0}}
144 % Variants of \llap and \rlap working equally on both sides and/or vertically
145 \def\hlap#1{\hbox to 0pt{\hss #1\hss}}
146 \def\vlap#1{\vbox to 0pt{\vss #1\vss}}
147 \def\clap#1{\vlap{\hlap{#1}}}
149 % \placeat{right}{down}{hmaterial} places <hmaterial>, so that its
150 % reference point lies at the given position wrt. the current ref point
151 \long\def\placeat#1#2#3{\smashedhbox{\hskip #1\lower #2\hbox{#3}}}
153 % Like \vbox, but with reference point in the upper left corner
154 \long\def\vhang#1{\vtop{\hrule height 0pt\relax #1}}
156 % Like \vhang, but respecting interline skips
157 \long\def\vhanglines#1{\vtop{\hbox to 0pt{}#1}}
159 % Crosshair with reference point in its center
160 \def\crosshair#1{\clap{\vrule height 0.2pt width #1}\clap{\vrule height #1 width 0.2pt}}
162 %%% Output routine %%%
164 \newbox\pageunderlays
166 \newbox\commonunderlays
167 \newbox\commonoverlays
169 % In addition to the normal page contents, you can define page overlays
170 % and underlays, which are zero-size vboxes positioned absolutely in the
171 % front / in the back of the normal material. Also, there are global
172 % versions of both which are not reset after every page.
173 \def\addlay#1#2{\setbox#1=\vbox{\ifvbox#1\box#1\fi\nointerlineskip\smashedvbox{#2}}}
174 \def\pageunderlay{\addlay\pageunderlays}
175 \def\pageoverlay{\addlay\pageoverlays}
176 \def\commonunderlay{\addlay\commonoverlays}
177 \def\commonoverlay{\addlay\commonoverlays}
179 % Our variation on \plainoutput, which manages inner/outer margins and overlays
181 \newdimen\pagebodydepth
182 \def\ucwoutput{\wigglepage\shipout\vbox{%
184 \ifvbox\commonunderlays\copy\commonunderlays\nointerlineskip\fi
185 \ifvbox\pageunderlays\box\pageunderlays\nointerlineskip\fi
187 \pagebodydepth=\prevdepth
189 \ifvbox\commonoverlays\vbox to 0pt{\vskip -\vsize\copy\commonoverlays\vss}\nointerlineskip\fi
190 \ifvbox\pageoverlays\vbox to 0pt{\vskip -\vsize\box\pageoverlays\vss}\nointerlineskip\fi
191 \prevdepth=\pagebodydepth
194 \ifnum\outputpenalty>-\@MM \else\dosupereject\fi}
196 \def\wigglepage{\ifodd\pageno\else\advance\hoffset by \evenpageshift\fi}
198 % Make it easier to redefine footline font (also, fix it so that OFS won't change it unless asked)
200 \footline={\hss\footfont\folio\hss}
206 % \itemize\ibull % or other marker
211 % \numlist\ndotted % or other numbering style
216 % Default dimensions of itemized lists
217 \newdimen\itemindent \itemindent=0.5in
218 \newdimen\itemnarrow \itemnarrow=0.5in % make lines narrower by this amount
219 \newskip\itemmarkerskip \itemmarkerskip=0.4em % between marker and the item
220 \newskip\preitemizeskip \preitemizeskip=3pt plus 2pt minus 1pt % before the list
221 \newskip\postitemizeskip \postitemizeskip=3pt plus 2pt minus 1pt % after the list
222 \newskip\interitemskip \interitemskip=2pt plus 1pt minus 0.5pt % between two items
224 % Analogues for nested lists
225 \newdimen\nesteditemindent \nesteditemindent=0.25in
226 \newdimen\nesteditemnarrow \nesteditemnarrow=0.25in
227 \newskip\prenesteditemizeskip \prenesteditemizeskip=0pt
228 \newskip\postnesteditemizeskip \postnesteditemizeskip=0pt
230 \newif\ifitems\itemsfalse
234 % Penalties (changed at compatibility level 1)
235 \newcount\preitemizepenalty \preitemizepenalty=0
236 \newcount\postitemizepenalty \postitemizepenalty=0
240 \vskip\prenesteditemizeskip
241 \advance\leftskip by \nesteditemindent
242 \advance\rightskip by \nesteditemnarrow
244 \ifnum\preitemizepenalty=0\else\penalty\preitemizepenalty\fi
245 \vskip\preitemizeskip
246 \advance\leftskip by \itemindent
247 \advance\rightskip by \itemnarrow
249 \parskip=\interitemskip
254 \vskip\postnesteditemizeskip
256 \ifnum\postitemizepenalty=0\else\penalty\postitemizepenalty\fi
257 \vskip\postitemizeskip
261 \def\inititemize{\begingroup\preitemize\itemstrue\parindent=0pt}
263 \def\itemize#1{\inititemize\setbox\itembox\llap{#1\hskip\itemmarkerskip}%
266 \def\singleitem{\par\leavevmode\copy\itembox\ignorespaces}
268 \def\endlist{\par\endgroup\postitemize}
270 % Markers for \itemize
271 \def\ibull{\raise0.2ex\hbox{$\bullet$}}
272 \def\idot{\raise0.2ex\hbox{$\cdot$}}
273 \def\istar{\raise0.2ex\hbox{$\ast$}}
275 \def\numlist#1{\inititemize\itemcount=0\let\:=\numbereditem
276 \let\itemnumbering=#1}
278 \def\numbereditem{\par\leavevmode\advance\itemcount by 1
279 \llap{\itemnumbering\hskip\itemmarkerskip}\ignorespaces}
281 % Numbering styles for \numlist
282 \def\nnorm{\the\itemcount}
283 \def\ndotted{\nnorm.}
285 \def\nparenp{(\nnorm)}
286 \def\nroman{\romannumeral\itemcount}
287 \def\nromanp{\nroman)}
288 \def\nalpha{\count@=96\advance\count@ by\itemcount\char\count@)}
289 \def\nAlpha{\count@=64\advance\count@ by\itemcount\char\count@)}
290 \def\ngreek{$\ifcase\itemcount\or\alpha\or\beta\or\gamma\or\delta\or\epsilon\or
291 \zeta\or\eta\or\theta\or\iota\or\kappa\or\lambda\or\mu\or\nu\or\xi\or\pi\or\rho
292 \or\sigma\or\tau\or\upsilon\or\phi\or\chi\or\psi\or\omega\fi$)}
296 % {\I italic} with automatic italic correction
297 \def\I{\it\aftergroup\/}
299 % A breakable dash, to be repeated on the next line
300 \def\={\discretionary{-}{-}{-}}
302 % Non-breakable identifiers
303 \def\<#1>{\leavevmode\hbox{\I #1}}
309 % Variants of \centerline, \leftline and \rightline, which are compatible with
310 % verbatim environments and other catcode hacks
311 \def\cline{\bgroup\def\linet@mp{\aftergroup\box\aftergroup0\aftergroup\egroup\hss\bgroup\aftergroup\hss\aftergroup\egroup}\afterassignment\linet@mp\setbox0\hbox to \hsize}
312 \def\lline{\bgroup\def\linet@mp{\aftergroup\box\aftergroup0\aftergroup\egroup\bgroup\aftergroup\hss\aftergroup\egroup}\afterassignment\linet@mp\setbox0\hbox to \hsize}
313 \def\rline{\bgroup\def\linet@mp{\aftergroup\box\aftergroup0\aftergroup\egroup\hss\bgroup\aftergroup\egroup}\afterassignment\linet@mp\setbox0\hbox to \hsize}
315 % Insert a PDF picture
316 % \putimage{width specification}{file}
317 \def\putimage#1#2{\hbox{\pdfximage #1{#2}\pdfrefximage\pdflastximage}}
321 \protected\def~{\plaintilde}
325 \chardef\colorstk=\pdfcolorstackinit page direct{0 g 0 G}
326 \def\colorpush#1{\pdfcolorstack\colorstk push{#1 rg #1 RG}}
327 \def\colorpushgray#1{\pdfcolorstack\colorstk push{#1 g #1 G}}
328 \def\colorpushcmyk#1{\pdfcolorstack\colorstk push{#1 k #1 K}}
329 \def\colorpop{\pdfcolorstack\colorstk pop}
336 % Define a new localized string: \localedef{language}{identifier}{message}
337 \def\localedef#1#2{\expandafter\def\csname loc:#1:#2\endcsname}
339 % Expand a localized string in the current language: \localemsg{identifier}
341 \ifcsname loc:\localelang:#1\endcsname
342 \csname loc:\localelang:#1\endcsname
344 \ucwwarn{Localized string #1 not defined in language \localelang}%
351 % Require a module: load it if it is not already loaded
353 \ifcsname ucwmod:#1\endcsname
359 % Definition of a new module (to be placed at the beginning of its file)
360 % (Also guards against repeated loading if somebody uses \input instead of \ucwmodule.)
362 \ifcsname ucwmod:#1\endcsname\endinput\fi
363 \expandafter\let\csname ucwmod:#1\endcsname=\relax
366 % Compatibility levels
367 % We try to be backwards compatible as much as we can, so all changes in behavior
368 % (except for addition of new control sequences) are versioned. By default, ucwmac
369 % starts in compatibility level 0, which should produce the same results as historic
370 % versions of ucwmac. Use \ucwcompat{level} to upgrade to a given level, or if you
371 % do not care about compatibility, \ucwcompat\ucwmaxcompat.
373 \chardef\ucwcurrentcompat=0 % Currently active compatibility level
374 \chardef\ucwmaxcompat=1 % Maximum supported compatibility level
379 % Level 0 (default): old ucwmac
381 \preitemizepenalty=-500
382 \postitemizepenalty=-500
383 \else\errmessage{Unsupported compatibility level #1 requested.}
389 % Let's hide all internal macros