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 % -------------------------------------------------------------------------------
7 % We'll use internal macros of plain TeX
10 \ifx\eTeXversion\undefined
11 \errmessage{ucwmac requires the e-TeX engine or its successor}
14 %%% PDF output detection %%%
19 \ifx\pdfoutput\undefined
20 \else\ifnum\pdfoutput>0
22 \pdfpkresolution=600 % Provide a reasonable default
25 %%% Auxiliary macros %%%
27 % Prepend/append #2 to the definition of #1
28 \long\def\prependef#1#2{\expandafter\def\expandafter#1\expandafter{#2#1}}
29 \long\def\appendef#1#2{\expandafter\def\expandafter#1\expandafter{#1#2}}
31 \def\ucwwarn#1{\immediate\write16{*** Warning: #1 ***}}
33 %%% Page size and margins %%%
35 % If you modify these registers, call \setuppage afterwards
36 \ifx\luatexversion\undefined
37 % In LuaTeX, \pagewidth and \pageheight are primitive
44 \newdimen\bottommargin
45 \newdimen\evenpageshift
49 \advance\hsize by -\leftmargin
50 \advance\hsize by -\rightmargin
52 \advance\vsize by -\topmargin
53 \advance\vsize by -\bottommargin
55 \advance\hoffset by -1truein
57 \advance\voffset by -1truein
61 \ifx\luatexversion\undefined
62 \pdfpagewidth=\pagewidth
63 \pdfpageheight=\pageheight
68 % Set multiple margins to the same value
69 \def\sethmargins#1{\leftmargin=#1\relax\rightmargin=#1\relax\evenpageshift=0pt\relax}
70 \def\setvmargins#1{\topmargin=#1\relax\bottommargin=#1\relax}
71 \def\setmargins#1{\sethmargins{#1}\setvmargins{#1}}
73 % Define inner/outer margin instead of left/right
74 \def\setinneroutermargin#1#2{\leftmargin#1\relax\rightmargin#2\relax\evenpageshift=\rightmargin\advance\evenpageshift by -\leftmargin}
76 % Use a predefined paper format, calls \setuppage automagically
78 \expandafter\let\expandafter\currentpaper\csname paper-#1\endcsname
79 \ifx\currentpaper\relax
80 \errmessage{Undefined paper format #1}
85 % Switch to landscape orientation, calls \setuppage automagically
88 \pageheight=\pagewidth
94 \def\defpaper#1#2#3{\expandafter\def\csname paper-#1\endcsname{\pagewidth=#2\pageheight=#3\setuppage}}
95 \defpaper{a3}{297truemm}{420truemm}
96 \defpaper{a4}{210truemm}{297truemm}
97 \defpaper{a5}{148truemm}{210truemm}
98 \defpaper{letter}{8.5truein}{11truein}
99 \defpaper{legal}{8.5truein}{14truein}
101 % Default page parameters
105 %%% Macros with optional arguments %%%
107 % After \def\a{\withoptarg\b}, the macro \a behaves in this way:
108 % \a[arg] does \def\optarg{arg} and then it expands \b
109 % \a does \let\optarg=\relax and then it expands \b
110 \def\withoptarg#1{\let\xoptcall=#1\futurelet\next\xopt}
111 \def\xopt{\ifx\next[\expandafter\xoptwith\else\let\optarg=\relax\expandafter\xoptcall\fi}
112 \def\xoptwith[#1]{\def\optarg{#1}\xoptcall}
114 % A shortcut for defining macros with optional arguments:
115 % \optdef\macro behaves as \def\domacro, while \macro itself is defined
116 % as a wrapper calling \domacro using \withoptarg.
118 \edef\xoptname{\expandafter\eatbackslash\string#1}%
119 \edef#1{\noexpand\withoptarg\csname do\xoptname\endcsname}%
120 \expandafter\def\csname do\xoptname\endcsname
123 % Trick: \eatbackslash eats the next backslash of category 12
124 \begingroup\lccode`\+=`\\
125 \lowercase{\endgroup\def\eatbackslash+{}}
127 % Expand to the optional argument if it exists
128 \def\optargorempty{\ifx\optarg\relax\else\optarg\fi}
130 %%% Placing material at specified coordinates %%%
132 % Set all dimensions of a given box register to zero
133 \def\smashbox#1{\ht#1=0pt \dp#1=0pt \wd#1=0pt}
134 \long\def\smashedhbox#1{{\setbox0=\hbox{#1}\smashbox0\box0}}
135 \long\def\smashedvbox#1{{\setbox0=\vbox{#1}\smashbox0\box0}}
137 % Variants of \llap and \rlap working equally on both sides and/or vertically
138 \def\hlap#1{\hbox to 0pt{\hss #1\hss}}
139 \def\vlap#1{\vbox to 0pt{\vss #1\vss}}
140 \def\clap#1{\vlap{\hlap{#1}}}
142 % \placeat{right}{down}{hmaterial} places <hmaterial>, so that its
143 % reference point lies at the given position wrt. the current ref point
144 \long\def\placeat#1#2#3{\smashedhbox{\hskip #1\lower #2\hbox{#3}}}
146 % Like \vbox, but with reference point in the upper left corner
147 \long\def\vhang#1{\vtop{\hrule height 0pt\relax #1}}
149 % Like \vhang, but respecting interline skips
150 \long\def\vhanglines#1{\vtop{\hbox to 0pt{}#1}}
152 % Crosshair with reference point in its center
153 \def\crosshair#1{\clap{\vrule height 0.2pt width #1}\clap{\vrule height #1 width 0.2pt}}
155 %%% Output routine %%%
157 \newbox\pageunderlays
159 \newbox\commonunderlays
160 \newbox\commonoverlays
162 % In addition to the normal page contents, you can define page overlays
163 % and underlays, which are zero-size vboxes positioned absolutely in the
164 % front / in the back of the normal material. Also, there are global
165 % versions of both which are not reset after every page.
166 \def\addlay#1#2{\setbox#1=\vbox{\ifvbox#1\box#1\fi\nointerlineskip\smashedvbox{#2}}}
167 \def\pageunderlay{\addlay\pageunderlays}
168 \def\pageoverlay{\addlay\pageoverlays}
169 \def\commonunderlay{\addlay\commonoverlays}
170 \def\commonoverlay{\addlay\commonoverlays}
172 % Our variation on \plainoutput, which manages inner/outer margins and overlays
174 \newdimen\pagebodydepth
175 \def\ucwoutput{\wigglepage\shipout\vbox{%
177 \ifvbox\commonunderlays\copy\commonunderlays\nointerlineskip\fi
178 \ifvbox\pageunderlays\box\pageunderlays\nointerlineskip\fi
180 \pagebodydepth=\prevdepth
182 \ifvbox\commonoverlays\vbox to 0pt{\vskip -\vsize\copy\commonoverlays\vss}\nointerlineskip\fi
183 \ifvbox\pageoverlays\vbox to 0pt{\vskip -\vsize\box\pageoverlays\vss}\nointerlineskip\fi
184 \prevdepth=\pagebodydepth
187 \ifnum\outputpenalty>-\@MM \else\dosupereject\fi}
189 \def\wigglepage{\ifodd\pageno\else\advance\hoffset by \evenpageshift\fi}
191 % Make it easier to redefine footline font (also, fix it so that OFS won't change it unless asked)
193 \footline={\hss\footfont\folio\hss}
197 % Default dimensions of itemized lists
198 \newdimen\itemindent \itemindent=0.5in
199 \newdimen\itemnarrow \itemnarrow=0.5in % make lines narrower by this amount
200 \newskip\itemmarkerskip \itemmarkerskip=0.4em % between marker and the item
201 \newskip\preitemizeskip \preitemizeskip=3pt plus 2pt minus 1pt % before the list
202 \newskip\postitemizeskip \postitemizeskip=3pt plus 2pt minus 1pt % after the list
203 \newskip\interitemskip \interitemskip=2pt plus 1pt minus 0.5pt % between two items
205 % Analogues for nested lists
206 \newdimen\nesteditemindent \nesteditemindent=0.25in
207 \newdimen\nesteditemnarrow \nesteditemnarrow=0.25in
208 \newskip\prenesteditemizeskip \prenesteditemizeskip=0pt
209 \newskip\postnesteditemizeskip \postnesteditemizeskip=0pt
211 \newif\ifitems\itemsfalse
217 \vskip\prenesteditemizeskip
218 \advance\leftskip by \nesteditemindent
219 \advance\rightskip by \nesteditemnarrow
221 \vskip\preitemizeskip
222 \advance\leftskip by \itemindent
223 \advance\rightskip by \itemnarrow
225 \parskip=\interitemskip
230 \vskip\postnesteditemizeskip
232 \vskip\postitemizeskip
236 \def\inititemize{\begingroup\preitemize\itemstrue\parindent=0pt}
238 \def\itemize#1{\inititemize\setbox\itembox\llap{#1\hskip\itemmarkerskip}%
241 \def\singleitem{\par\leavevmode\copy\itembox\ignorespaces}
243 \def\endlist{\par\endgroup\postitemize}
245 \def\ibull{\raise0.2ex\hbox{$\bullet$}} % Signs frequently used for \itemize
246 \def\idot{\raise0.2ex\hbox{$\cdot$}}
247 \def\istar{\raise0.2ex\hbox{$\ast$}}
249 \def\numlist#1{\inititemize\itemcount=0\let\:=\numbereditem
250 \let\itemnumbering=#1}
252 \def\numbereditem{\par\leavevmode\advance\itemcount by 1
253 \llap{\itemnumbering\hskip\itemmarkerskip}\ignorespaces}
255 \def\nnorm{\the\itemcount}
256 \def\ndotted{\nnorm.}
258 \def\nparenp{(\nnorm)}
259 \def\nroman{\romannumeral\itemcount}
260 \def\nromanp{\nroman)}
261 \def\nalpha{\count@=96\advance\count@ by\itemcount\char\count@)}
262 \def\nAlpha{\count@=64\advance\count@ by\itemcount\char\count@)}
263 \def\ngreek{$\ifcase\itemcount\or\alpha\or\beta\or\gamma\or\delta\or\epsilon\or
264 \zeta\or\eta\or\theta\or\iota\or\kappa\or\lambda\or\mu\or\nu\or\xi\or\pi\or\rho
265 \or\sigma\or\tau\or\upsilon\or\phi\or\chi\or\psi\or\omega\fi$)}
269 % {\I italic} with automatic italic correction
270 \def\I{\it\aftergroup\/}
272 % A breakable dash, to be repeated on the next line
273 \def\={\discretionary{-}{-}{-}}
275 % Non-breakable identifiers
276 \def\<#1>{\leavevmode\hbox{\I #1}}
282 % Variants of \centerline, \leftline and \rightline, which are compatible with
283 % verbatim environments and other catcode hacks
284 \def\cline{\bgroup\def\linet@mp{\aftergroup\box\aftergroup0\aftergroup\egroup\hss\bgroup\aftergroup\hss\aftergroup\egroup}\afterassignment\linet@mp\setbox0\hbox to \hsize}
285 \def\lline{\bgroup\def\linet@mp{\aftergroup\box\aftergroup0\aftergroup\egroup\bgroup\aftergroup\hss\aftergroup\egroup}\afterassignment\linet@mp\setbox0\hbox to \hsize}
286 \def\rline{\bgroup\def\linet@mp{\aftergroup\box\aftergroup0\aftergroup\egroup\hss\bgroup\aftergroup\egroup}\afterassignment\linet@mp\setbox0\hbox to \hsize}
288 % Insert a PDF picture
289 % \putimage{width specification}{file}
290 \def\putimage#1#2{\hbox{\pdfximage #1{#2}\pdfrefximage\pdflastximage}}
294 \protected\def~{\plaintilde}
301 % Define a new localized string: \localedef{language}{identifier}{message}
302 \def\localedef#1#2{\expandafter\def\csname loc:#1:#2\endcsname}
304 % Expand a localized string in the current language: \localemsg{identifier}
306 \ifcsname loc:\localelang:#1\endcsname
307 \csname loc:\localelang:#1\endcsname
309 \ucwwarn{Localized string #1 not defined in language \localelang}%
316 % Let's hide all internal macros