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
236 \vskip\prenesteditemizeskip
237 \advance\leftskip by \nesteditemindent
238 \advance\rightskip by \nesteditemnarrow
240 \vskip\preitemizeskip
241 \advance\leftskip by \itemindent
242 \advance\rightskip by \itemnarrow
244 \parskip=\interitemskip
249 \vskip\postnesteditemizeskip
251 \vskip\postitemizeskip
255 \def\inititemize{\begingroup\preitemize\itemstrue\parindent=0pt}
257 \def\itemize#1{\inititemize\setbox\itembox\llap{#1\hskip\itemmarkerskip}%
260 \def\singleitem{\par\leavevmode\copy\itembox\ignorespaces}
262 \def\endlist{\par\endgroup\postitemize}
264 % Markers for \itemize
265 \def\ibull{\raise0.2ex\hbox{$\bullet$}}
266 \def\idot{\raise0.2ex\hbox{$\cdot$}}
267 \def\istar{\raise0.2ex\hbox{$\ast$}}
269 \def\numlist#1{\inititemize\itemcount=0\let\:=\numbereditem
270 \let\itemnumbering=#1}
272 \def\numbereditem{\par\leavevmode\advance\itemcount by 1
273 \llap{\itemnumbering\hskip\itemmarkerskip}\ignorespaces}
275 % Numbering styles for \numlist
276 \def\nnorm{\the\itemcount}
277 \def\ndotted{\nnorm.}
279 \def\nparenp{(\nnorm)}
280 \def\nroman{\romannumeral\itemcount}
281 \def\nromanp{\nroman)}
282 \def\nalpha{\count@=96\advance\count@ by\itemcount\char\count@)}
283 \def\nAlpha{\count@=64\advance\count@ by\itemcount\char\count@)}
284 \def\ngreek{$\ifcase\itemcount\or\alpha\or\beta\or\gamma\or\delta\or\epsilon\or
285 \zeta\or\eta\or\theta\or\iota\or\kappa\or\lambda\or\mu\or\nu\or\xi\or\pi\or\rho
286 \or\sigma\or\tau\or\upsilon\or\phi\or\chi\or\psi\or\omega\fi$)}
290 % {\I italic} with automatic italic correction
291 \def\I{\it\aftergroup\/}
293 % A breakable dash, to be repeated on the next line
294 \def\={\discretionary{-}{-}{-}}
296 % Non-breakable identifiers
297 \def\<#1>{\leavevmode\hbox{\I #1}}
303 % Variants of \centerline, \leftline and \rightline, which are compatible with
304 % verbatim environments and other catcode hacks
305 \def\cline{\bgroup\def\linet@mp{\aftergroup\box\aftergroup0\aftergroup\egroup\hss\bgroup\aftergroup\hss\aftergroup\egroup}\afterassignment\linet@mp\setbox0\hbox to \hsize}
306 \def\lline{\bgroup\def\linet@mp{\aftergroup\box\aftergroup0\aftergroup\egroup\bgroup\aftergroup\hss\aftergroup\egroup}\afterassignment\linet@mp\setbox0\hbox to \hsize}
307 \def\rline{\bgroup\def\linet@mp{\aftergroup\box\aftergroup0\aftergroup\egroup\hss\bgroup\aftergroup\egroup}\afterassignment\linet@mp\setbox0\hbox to \hsize}
309 % Insert a PDF picture
310 % \putimage{width specification}{file}
311 \def\putimage#1#2{\hbox{\pdfximage #1{#2}\pdfrefximage\pdflastximage}}
315 \protected\def~{\plaintilde}
319 \chardef\colorstk=\pdfcolorstackinit page direct{0 g 0 G}
320 \def\colorpush#1{\pdfcolorstack\colorstk push{#1 rg #1 RG}}
321 \def\colorpushgray#1{\pdfcolorstack\colorstk push{#1 g #1 G}}
322 \def\colorpushcmyk#1{\pdfcolorstack\colorstk push{#1 k #1 K}}
323 \def\colorpop{\pdfcolorstack\colorstk pop}
330 % Define a new localized string: \localedef{language}{identifier}{message}
331 \def\localedef#1#2{\expandafter\def\csname loc:#1:#2\endcsname}
333 % Expand a localized string in the current language: \localemsg{identifier}
335 \ifcsname loc:\localelang:#1\endcsname
336 \csname loc:\localelang:#1\endcsname
338 \ucwwarn{Localized string #1 not defined in language \localelang}%
345 % Require a module: load it if it is not already loaded
347 \ifcsname ucwmod:#1\endcsname
353 % Definition of a new module (to be placed at the beginning of its file)
354 % (Also guards against repeated loading if somebody uses \input instead of \ucwmodule.)
356 \ifcsname ucwmod:#1\endcsname\endinput\fi
357 \expandafter\let\csname ucwmod:#1\endcsname=\relax
362 % Let's hide all internal macros