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}
204 % Default dimensions of itemized lists
205 \newdimen\itemindent \itemindent=0.5in
206 \newdimen\itemnarrow \itemnarrow=0.5in % make lines narrower by this amount
207 \newskip\itemmarkerskip \itemmarkerskip=0.4em % between marker and the item
208 \newskip\preitemizeskip \preitemizeskip=3pt plus 2pt minus 1pt % before the list
209 \newskip\postitemizeskip \postitemizeskip=3pt plus 2pt minus 1pt % after the list
210 \newskip\interitemskip \interitemskip=2pt plus 1pt minus 0.5pt % between two items
212 % Analogues for nested lists
213 \newdimen\nesteditemindent \nesteditemindent=0.25in
214 \newdimen\nesteditemnarrow \nesteditemnarrow=0.25in
215 \newskip\prenesteditemizeskip \prenesteditemizeskip=0pt
216 \newskip\postnesteditemizeskip \postnesteditemizeskip=0pt
218 \newif\ifitems\itemsfalse
224 \vskip\prenesteditemizeskip
225 \advance\leftskip by \nesteditemindent
226 \advance\rightskip by \nesteditemnarrow
228 \vskip\preitemizeskip
229 \advance\leftskip by \itemindent
230 \advance\rightskip by \itemnarrow
232 \parskip=\interitemskip
237 \vskip\postnesteditemizeskip
239 \vskip\postitemizeskip
243 \def\inititemize{\begingroup\preitemize\itemstrue\parindent=0pt}
245 \def\itemize#1{\inititemize\setbox\itembox\llap{#1\hskip\itemmarkerskip}%
248 \def\singleitem{\par\leavevmode\copy\itembox\ignorespaces}
250 \def\endlist{\par\endgroup\postitemize}
252 \def\ibull{\raise0.2ex\hbox{$\bullet$}} % Signs frequently used for \itemize
253 \def\idot{\raise0.2ex\hbox{$\cdot$}}
254 \def\istar{\raise0.2ex\hbox{$\ast$}}
256 \def\numlist#1{\inititemize\itemcount=0\let\:=\numbereditem
257 \let\itemnumbering=#1}
259 \def\numbereditem{\par\leavevmode\advance\itemcount by 1
260 \llap{\itemnumbering\hskip\itemmarkerskip}\ignorespaces}
262 \def\nnorm{\the\itemcount}
263 \def\ndotted{\nnorm.}
265 \def\nparenp{(\nnorm)}
266 \def\nroman{\romannumeral\itemcount}
267 \def\nromanp{\nroman)}
268 \def\nalpha{\count@=96\advance\count@ by\itemcount\char\count@)}
269 \def\nAlpha{\count@=64\advance\count@ by\itemcount\char\count@)}
270 \def\ngreek{$\ifcase\itemcount\or\alpha\or\beta\or\gamma\or\delta\or\epsilon\or
271 \zeta\or\eta\or\theta\or\iota\or\kappa\or\lambda\or\mu\or\nu\or\xi\or\pi\or\rho
272 \or\sigma\or\tau\or\upsilon\or\phi\or\chi\or\psi\or\omega\fi$)}
276 % {\I italic} with automatic italic correction
277 \def\I{\it\aftergroup\/}
279 % A breakable dash, to be repeated on the next line
280 \def\={\discretionary{-}{-}{-}}
282 % Non-breakable identifiers
283 \def\<#1>{\leavevmode\hbox{\I #1}}
289 % Variants of \centerline, \leftline and \rightline, which are compatible with
290 % verbatim environments and other catcode hacks
291 \def\cline{\bgroup\def\linet@mp{\aftergroup\box\aftergroup0\aftergroup\egroup\hss\bgroup\aftergroup\hss\aftergroup\egroup}\afterassignment\linet@mp\setbox0\hbox to \hsize}
292 \def\lline{\bgroup\def\linet@mp{\aftergroup\box\aftergroup0\aftergroup\egroup\bgroup\aftergroup\hss\aftergroup\egroup}\afterassignment\linet@mp\setbox0\hbox to \hsize}
293 \def\rline{\bgroup\def\linet@mp{\aftergroup\box\aftergroup0\aftergroup\egroup\hss\bgroup\aftergroup\egroup}\afterassignment\linet@mp\setbox0\hbox to \hsize}
295 % Insert a PDF picture
296 % \putimage{width specification}{file}
297 \def\putimage#1#2{\hbox{\pdfximage #1{#2}\pdfrefximage\pdflastximage}}
301 \protected\def~{\plaintilde}
305 \chardef\colorstk=\pdfcolorstackinit page direct{0 g 0 G}
306 \def\colorpush#1{\pdfcolorstack\colorstk push{#1 rg #1 RG}}
307 \def\colorpushgray#1{\pdfcolorstack\colorstk push{#1 g #1 G}}
308 \def\colorpushcmyk#1{\pdfcolorstack\colorstk push{#1 k #1 K}}
309 \def\colorpop{\pdfcolorstack\colorstk pop}
316 % Define a new localized string: \localedef{language}{identifier}{message}
317 \def\localedef#1#2{\expandafter\def\csname loc:#1:#2\endcsname}
319 % Expand a localized string in the current language: \localemsg{identifier}
321 \ifcsname loc:\localelang:#1\endcsname
322 \csname loc:\localelang:#1\endcsname
324 \ucwwarn{Localized string #1 not defined in language \localelang}%
332 \ifcsname ucwmod:#1\endcsname
339 \ifcsname ucwmod:#1\endcsname\endinput\fi
340 \expandafter\let\csname ucwmod:#1\endcsname=\relax
345 % Let's hide all internal macros