]> mj.ucw.cz Git - ucwmac.git/blob - ucwmac.tex
d4b4e3d60c93c448f34053da0dea477ed63a1b0e
[ucwmac.git] / ucwmac.tex
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 % -------------------------------------------------------------------------------
4
5 \ifx\ucwmodule\undefined\else\endinput\fi
6
7 %%% Prolog %%%
8
9 % We'll use internal macros of plain TeX
10 \catcode`@=11
11
12 \ifx\eTeXversion\undefined
13 \errmessage{ucwmac requires the e-TeX engine or its successor}
14 \fi
15
16 %%% PDF output detection %%%
17
18 \newif\ifpdf
19 \pdffalse
20
21 \ifx\pdfoutput\undefined
22 \else\ifnum\pdfoutput>0
23         \pdftrue
24         \pdfpkresolution=600    % Provide a reasonable default
25 \fi\fi
26
27 \ifx\directlua\undefined\else
28         % In LuaTeX \pdfpkresolution is not enough
29         \directlua{kpse.init_prog("luatex", 600, "ljfour")}
30 \fi
31
32 %%% Auxiliary macros %%%
33
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}}
37
38 \def\ucwwarn#1{\immediate\write16{*** Warning: #1 ***}}
39
40 %%% Page size and margins %%%
41
42 % If you modify these registers, call \setuppage afterwards
43 \ifx\luatexversion\undefined
44         % In LuaTeX, \pagewidth and \pageheight are primitive
45         \newdimen\pagewidth
46         \newdimen\pageheight
47 \fi
48 \newdimen\leftmargin
49 \newdimen\rightmargin
50 \newdimen\topmargin
51 \newdimen\bottommargin
52 \newdimen\evenpageshift
53
54 \def\setuppage{%
55         \hsize=\pagewidth
56         \advance\hsize by -\leftmargin
57         \advance\hsize by -\rightmargin
58         \vsize=\pageheight
59         \advance\vsize by -\topmargin
60         \advance\vsize by -\bottommargin
61         \hoffset=\leftmargin
62         \advance\hoffset by -1truein
63         \voffset=\topmargin
64         \advance\voffset by -1truein
65         \ifpdf
66                 \pdfhorigin=1truein
67                 \pdfvorigin=1truein
68                 \ifx\luatexversion\undefined
69                         \pdfpagewidth=\pagewidth
70                         \pdfpageheight=\pageheight
71                 \fi
72         \fi
73 }
74
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}}
79
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}
82
83 % Use a predefined paper format, calls \setuppage automagically
84 \def\setpaper#1{%
85         \expandafter\let\expandafter\currentpaper\csname paper-#1\endcsname
86         \ifx\currentpaper\relax
87                 \errmessage{Undefined paper format #1}
88         \fi
89         \currentpaper
90 }
91
92 % Switch to landscape orientation, calls \setuppage automagically
93 \def\landscape{%
94         \dimen0=\pageheight
95         \pageheight=\pagewidth
96         \pagewidth=\dimen0
97         \setuppage
98 }
99
100 % Common paper sizes
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}
107
108 % Default page parameters
109 \setmargins{1truein}
110 \setpaper{a4}
111
112 %%% Macros with optional arguments %%%
113
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}
120
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.
124 \def\optdef#1{%
125         \edef\xoptname{\expandafter\eatbackslash\string#1}%
126         \edef#1{\noexpand\withoptarg\csname do\xoptname\endcsname}%
127         \expandafter\def\csname do\xoptname\endcsname
128 }
129
130 % Trick: \eatbackslash eats the next backslash of category 12
131 \begingroup\lccode`\+=`\\
132 \lowercase{\endgroup\def\eatbackslash+{}}
133
134 % Expand to the optional argument if it exists
135 \def\optargorempty{\ifx\optarg\relax\else\optarg\fi}
136
137 %%% Placing material at specified coordinates %%%
138
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}}
143
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}}}
148
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}}}
152
153 % Like \vbox, but with reference point in the upper left corner
154 \long\def\vhang#1{\vtop{\hrule height 0pt\relax #1}}
155
156 % Like \vhang, but respecting interline skips
157 \long\def\vhanglines#1{\vtop{\hbox to 0pt{}#1}}
158
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}}
161
162 %%% Output routine %%%
163
164 \newbox\pageunderlays
165 \newbox\pageoverlays
166 \newbox\commonunderlays
167 \newbox\commonoverlays
168
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}
178
179 % Our variation on \plainoutput, which manages inner/outer margins and overlays
180 \output{\ucwoutput}
181 \newdimen\pagebodydepth
182 \def\ucwoutput{\wigglepage\shipout\vbox{%
183         \makeheadline
184         \ifvbox\commonunderlays\copy\commonunderlays\nointerlineskip\fi
185         \ifvbox\pageunderlays\box\pageunderlays\nointerlineskip\fi
186         \pagebody
187         \pagebodydepth=\prevdepth
188         \nointerlineskip
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
192         \makefootline
193 }\advancepageno
194 \ifnum\outputpenalty>-\@MM \else\dosupereject\fi}
195
196 \def\wigglepage{\ifodd\pageno\else\advance\hoffset by \evenpageshift\fi}
197
198 % Make it easier to redefine footline font (also, fix it so that OFS won't change it unless asked)
199 \let\footfont=\tenrm
200 \footline={\hss\footfont\folio\hss}
201
202 %%% Itemization %%%
203
204 % Usage:
205 %
206 % \itemize\ibull        % or other marker
207 % \:first item
208 % \:second item
209 % \endlist
210 %
211 % \numlist\ndotted      % or other numbering style
212 % \:first
213 % \:second
214 % \endlist
215
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
223
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
229
230 \newif\ifitems\itemsfalse
231 \newbox\itembox
232 \newcount\itemcount
233
234 % Penalties (changed at compatibility level 1)
235 \newcount\preitemizepenalty     \preitemizepenalty=0
236 \newcount\postitemizepenalty    \postitemizepenalty=0
237
238 \def\preitemize{
239         \ifitems
240                 \vskip\prenesteditemizeskip
241                 \advance\leftskip by \nesteditemindent
242                 \advance\rightskip by \nesteditemnarrow
243         \else
244                 \ifnum\preitemizepenalty=0\else\penalty\preitemizepenalty\fi
245                 \vskip\preitemizeskip
246                 \advance\leftskip by \itemindent
247                 \advance\rightskip by \itemnarrow
248         \fi
249         \parskip=\interitemskip
250 }
251
252 \def\postitemize{
253         \ifitems
254                 \vskip\postnesteditemizeskip
255         \else
256                 \ifnum\postitemizepenalty=0\else\penalty\postitemizepenalty\fi
257                 \vskip\postitemizeskip
258         \fi
259 }
260
261 \def\inititemize{\begingroup\preitemize\itemstrue\parindent=0pt}
262
263 \def\itemize#1{\inititemize\setbox\itembox\llap{#1\hskip\itemmarkerskip}%
264 \let\:=\singleitem}
265
266 \def\singleitem{\par\leavevmode\copy\itembox\ignorespaces}
267
268 \def\endlist{\par\endgroup\postitemize}
269
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$}}
274
275 \def\numlist#1{\inititemize\itemcount=0\let\:=\numbereditem
276 \let\itemnumbering=#1}
277
278 \def\numbereditem{\par\leavevmode\advance\itemcount by 1
279 \llap{\itemnumbering\hskip\itemmarkerskip}\ignorespaces}
280
281 % Numbering styles for \numlist
282 \def\nnorm{\the\itemcount}
283 \def\ndotted{\nnorm.}
284 \def\nparen{\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$)}
293
294 %%% Miscellanea %%%
295
296 % {\I italic} with automatic italic correction
297 \def\I{\it\aftergroup\/}
298
299 % A breakable dash, to be repeated on the next line
300 \def\={\discretionary{-}{-}{-}}
301
302 % Non-breakable identifiers
303 \def\<#1>{\leavevmode\hbox{\I #1}}
304
305 % Handy shortcuts
306 \let\>=\noindent
307 \def\\{\hfil\break}
308
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}
314
315 % Insert a PDF picture
316 % \putimage{width specification}{file}
317 \def\putimage#1#2{\hbox{\pdfximage #1{#2}\pdfrefximage\pdflastximage}}
318
319 % Let ~ be protected
320 \let\plaintilde=~
321 \protected\def~{\plaintilde}
322
323 %%% Colors %%%
324
325 % Use of pdfTeX color stack:
326 % \colorpush\rgb{1 0 0} puts a new color on the stack
327 % \colorset\rgb{1 0 0} replaces the top color on the stack
328 % \colorpop pops the top color
329 % \colorlocal\rgb{1 0 0} set a color locally until the end of the current group
330 \chardef\colorstk=\pdfcolorstackinit page direct{0 g 0 G}
331 \def\colorset#1{\pdfcolorstack\colorstk set #1}
332 \def\colorpush#1{\pdfcolorstack\colorstk push #1}
333 \def\colorpop{\pdfcolorstack\colorstk pop}
334 \def\colorlocal{\aftergroup\colorpop\colorpush}
335
336 % Different ways of describing colors: \rgb{R G B}, \gray{G}, \cmyk{C M Y K}
337 % (all components are real numbers between 0 and 1)
338 \def\rgb#1{{#1 rg #1 RG}}
339 \def\gray#1{{#1 g #1 G}}
340 \def\cmyk#1{{#1 k #1 K}}
341
342 %%% Localization %%%
343
344 % Current language
345 \def\localelang{en}
346
347 % Define a new localized string: \localedef{language}{identifier}{message}
348 \def\localedef#1#2{\expandafter\def\csname loc:#1:#2\endcsname}
349
350 % Expand a localized string in the current language: \localemsg{identifier}
351 \def\localestr#1{%
352         \ifcsname loc:\localelang:#1\endcsname
353                 \csname loc:\localelang:#1\endcsname
354         \else
355                 \ucwwarn{Localized string #1 not defined in language \localelang}%
356                 ???%
357         \fi
358 }
359
360 %%% Modules %%%
361
362 % Require a module: load it if it is not already loaded
363 \def\ucwmodule#1{
364         \ifcsname ucwmod:#1\endcsname
365         \else
366                 \input ucw-#1.tex
367         \fi
368 }
369
370 % Definition of a new module (to be placed at the beginning of its file)
371 % (Also guards against repeated loading if somebody uses \input instead of \ucwmodule.)
372 \def\ucwdefmodule#1{
373         \ifcsname ucwmod:#1\endcsname\endinput\fi
374         \expandafter\let\csname ucwmod:#1\endcsname=\relax
375 }
376
377 % Compatibility levels
378 % We try to be backwards compatible as much as we can, so all changes in behavior
379 % (except for addition of new control sequences) are versioned. By default, ucwmac
380 % starts in compatibility level 0, which should produce the same results as historic
381 % versions of ucwmac. Use \ucwcompat{level} to upgrade to a given level, or if you
382 % do not care about compatibility, \ucwcompat\ucwmaxcompat.
383
384 \chardef\ucwcurrentcompat=0     % Currently active compatibility level
385 \chardef\ucwmaxcompat=1         % Maximum supported compatibility level
386
387 \def\ucwcompat#1{
388         \ucwcurrentcompat=#1
389         \ifcase #1
390                 % Level 0 (default): old ucwmac
391         \or     % Level 1
392                 \preitemizepenalty=-500
393                 \postitemizepenalty=-500
394         \else\errmessage{Unsupported compatibility level #1 requested.}
395         \fi
396 }
397
398 %%% Epilog %%%
399
400 % Let's hide all internal macros
401 \catcode`@=12