]> mj.ucw.cz Git - ucwmac.git/blob - ucwmac.tex
Nicer warning messages
[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 % Variants of \def and \let, where the control sequence name is given as a string
39 \def\sdef#1{\expandafter\def\csname#1\endcsname}
40 \def\slet#1#2{\expandafter\let\csname#1\expandafter\endcsname\csname#2\endcsname}
41
42 % Add \protected to an existing macro
43 \def\addprotected#1{\protected\edef#1{\expandafter\unexpanded\expandafter{#1}}}
44
45 % Protect ~
46 \addprotected~
47
48 \def\ucwwarn#1{\immediate\write16{*** UCWmac warning: #1 ***}}
49
50 %%% Page size and margins %%%
51
52 % If you modify these registers, call \setuppage afterwards
53 \ifx\luatexversion\undefined
54         % In LuaTeX, \pagewidth and \pageheight are primitive
55         \newdimen\pagewidth
56         \newdimen\pageheight
57 \fi
58 \newdimen\leftmargin
59 \newdimen\rightmargin
60 \newdimen\topmargin
61 \newdimen\bottommargin
62 \newdimen\evenpageshift
63
64 \def\setuppage{%
65         \hsize=\pagewidth
66         \advance\hsize by -\leftmargin
67         \advance\hsize by -\rightmargin
68         \vsize=\pageheight
69         \advance\vsize by -\topmargin
70         \advance\vsize by -\bottommargin
71         \hoffset=\leftmargin
72         \advance\hoffset by -1truein
73         \voffset=\topmargin
74         \advance\voffset by -1truein
75         \ifpdf
76                 \pdfhorigin=1truein
77                 \pdfvorigin=1truein
78                 \ifx\luatexversion\undefined
79                         \pdfpagewidth=\pagewidth
80                         \pdfpageheight=\pageheight
81                 \fi
82         \fi
83 }
84
85 % Set multiple margins to the same value
86 \def\sethmargins#1{\leftmargin=#1\relax\rightmargin=#1\relax\evenpageshift=0pt\relax}
87 \def\setvmargins#1{\topmargin=#1\relax\bottommargin=#1\relax}
88 \def\setmargins#1{\sethmargins{#1}\setvmargins{#1}}
89
90 % Define inner/outer margin instead of left/right
91 \def\setinneroutermargin#1#2{\leftmargin#1\relax\rightmargin#2\relax\evenpageshift=\rightmargin\advance\evenpageshift by -\leftmargin}
92
93 % Use a predefined paper format, calls \setuppage automagically
94 \def\setpaper#1{%
95         \expandafter\let\expandafter\currentpaper\csname paper-#1\endcsname
96         \ifx\currentpaper\relax
97                 \errmessage{Undefined paper format #1}
98         \fi
99         \currentpaper
100 }
101
102 % Switch to landscape orientation, calls \setuppage automagically
103 \def\landscape{%
104         \dimen0=\pageheight
105         \pageheight=\pagewidth
106         \pagewidth=\dimen0
107         \setuppage
108 }
109
110 % Common paper sizes
111 \def\defpaper#1#2#3{\expandafter\def\csname paper-#1\endcsname{\pagewidth=#2\pageheight=#3\setuppage}}
112 \defpaper{a3}{297truemm}{420truemm}
113 \defpaper{a4}{210truemm}{297truemm}
114 \defpaper{a5}{148truemm}{210truemm}
115 \defpaper{letter}{8.5truein}{11truein}
116 \defpaper{legal}{8.5truein}{14truein}
117
118 % Default page parameters
119 \setmargins{1truein}
120 \setpaper{a4}
121
122 %%% Macros with optional arguments %%%
123
124 % After \def\a{\withoptarg\b}, the macro \a behaves in this way:
125 %       \a[arg]         does \def\optarg{arg} and then it expands \b
126 %       \a              does \let\optarg=\relax and then it expands \b
127 \def\withoptarg#1{\let\xoptcall=#1\futurelet\next\xopt}
128 \def\xopt{\ifx\next[\expandafter\xoptwith\else\let\optarg=\relax\expandafter\xoptcall\fi}
129 \def\xoptwith[#1]{\def\optarg{#1}\xoptcall}
130
131 % A shortcut for defining macros with optional arguments:
132 % \optdef\macro behaves as \def\domacro, while \macro itself is defined
133 % as a wrapper calling \domacro using \withoptarg.
134 \def\optdef#1{%
135         \edef\xoptname{\expandafter\eatbackslash\string#1}%
136         \edef#1{\noexpand\withoptarg\csname do\xoptname\endcsname}%
137         \expandafter\def\csname do\xoptname\endcsname
138 }
139
140 % Trick: \eatbackslash eats the next backslash of category 12
141 \begingroup\lccode`\+=`\\
142 \lowercase{\endgroup\def\eatbackslash+{}}
143
144 % Expand to the optional argument if it exists
145 \def\optargorempty{\ifx\optarg\relax\else\optarg\fi}
146
147 %%% Placing material at specified coordinates %%%
148
149 % Set all dimensions of a given box register to zero
150 \def\smashbox#1{\ht#1=0pt \dp#1=0pt \wd#1=0pt}
151 \long\def\smashedhbox#1{{\setbox0=\hbox{#1}\smashbox0\box0}}
152 \long\def\smashedvbox#1{{\setbox0=\vbox{#1}\smashbox0\box0}}
153
154 % Variants of \llap and \rlap working equally on both sides and/or vertically
155 \def\hlap#1{\hbox to 0pt{\hss #1\hss}}
156 \def\vlap#1{\vbox to 0pt{\vss #1\vss}}
157 \def\clap#1{\vlap{\hlap{#1}}}
158
159 % \placeat{right}{down}{hmaterial} places <hmaterial>, so that its
160 % reference point lies at the given position wrt. the current ref point
161 \long\def\placeat#1#2#3{\smashedhbox{\hskip #1\lower #2\hbox{#3}}}
162
163 % Like \vbox, but with reference point in the upper left corner
164 \long\def\vhang#1{\vtop{\hrule height 0pt\relax #1}}
165
166 % Like \vhang, but respecting interline skips
167 \long\def\vhanglines#1{\vtop{\hbox to 0pt{}#1}}
168
169 % Crosshair with reference point in its center
170 \def\crosshair#1{\clap{\vrule height 0.2pt width #1}\clap{\vrule height #1 width 0.2pt}}
171
172 %%% Output routine %%%
173
174 \newbox\pageunderlays
175 \newbox\pageoverlays
176 \newbox\commonunderlays
177 \newbox\commonoverlays
178
179 % In addition to the normal page contents, you can define page overlays
180 % and underlays, which are zero-size vboxes positioned absolutely in the
181 % front / in the back of the normal material. Also, there are global
182 % versions of both which are not reset after every page.
183 \def\addlay#1#2{\setbox#1=\vbox{\ifvbox#1\box#1\fi\nointerlineskip\smashedvbox{#2}}}
184 \def\pageunderlay{\addlay\pageunderlays}
185 \def\pageoverlay{\addlay\pageoverlays}
186 \def\commonunderlay{\addlay\commonoverlays}
187 \def\commonoverlay{\addlay\commonoverlays}
188
189 % Our variation on \plainoutput, which manages inner/outer margins and overlays
190 \output{\ucwoutput}
191 \newdimen\pagebodydepth
192 \def\ucwoutput{\wigglepage\shipout\vbox{%
193         \makeheadline
194         \ifvbox\commonunderlays\copy\commonunderlays\nointerlineskip\fi
195         \ifvbox\pageunderlays\box\pageunderlays\nointerlineskip\fi
196         \pagebody
197         \pagebodydepth=\prevdepth
198         \nointerlineskip
199         \ifvbox\commonoverlays\vbox to 0pt{\vskip -\vsize\copy\commonoverlays\vss}\nointerlineskip\fi
200         \ifvbox\pageoverlays\vbox to 0pt{\vskip -\vsize\box\pageoverlays\vss}\nointerlineskip\fi
201         \prevdepth=\pagebodydepth
202         \makefootline
203 }\advancepageno
204 \ifnum\outputpenalty>-\@MM \else\dosupereject\fi}
205
206 \def\wigglepage{\ifodd\pageno\else\advance\hoffset by \evenpageshift\fi}
207
208 % Make it easier to redefine footline font (also, fix it so that OFS won't change it unless asked)
209 \let\footfont=\tenrm
210 \footline={\hss\footfont\folio\hss}
211
212 %%% Itemization %%%
213
214 % Usage:
215 %
216 % \itemize\ibull        % or other marker
217 % \:first item
218 % \:second item
219 % \endlist
220 %
221 % \numlist\ndotted      % or other numbering style
222 % \:first
223 % \:second
224 % \endlist
225
226 % Default dimensions of itemized lists
227 \newdimen\itemindent            \itemindent=0.5in
228 \newdimen\itemnarrow            \itemnarrow=0.5in                       % make lines narrower by this amount
229 \newskip\itemmarkerskip         \itemmarkerskip=0.4em                   % between marker and the item
230 \newskip\preitemizeskip         \preitemizeskip=3pt plus 2pt minus 1pt  % before the list
231 \newskip\postitemizeskip        \postitemizeskip=3pt plus 2pt minus 1pt % after the list
232 \newskip\interitemskip          \interitemskip=2pt plus 1pt minus 0.5pt % between two items
233
234 % Analogues for nested lists
235 \newdimen\nesteditemindent      \nesteditemindent=0.25in
236 \newdimen\nesteditemnarrow      \nesteditemnarrow=0.25in
237 \newskip\prenesteditemizeskip   \prenesteditemizeskip=0pt
238 \newskip\postnesteditemizeskip  \postnesteditemizeskip=0pt
239
240 \newif\ifitems\itemsfalse
241 \newbox\itembox
242 \newcount\itemcount
243
244 % Penalties (changed at compatibility level 1)
245 \newcount\preitemizepenalty     \preitemizepenalty=0
246 \newcount\postitemizepenalty    \postitemizepenalty=0
247
248 \def\preitemize{
249         \ifitems
250                 \vskip\prenesteditemizeskip
251                 \advance\leftskip by \nesteditemindent
252                 \advance\rightskip by \nesteditemnarrow
253         \else
254                 \ifnum\preitemizepenalty=0\else\penalty\preitemizepenalty\fi
255                 \vskip\preitemizeskip
256                 \advance\leftskip by \itemindent
257                 \advance\rightskip by \itemnarrow
258         \fi
259         \parskip=\interitemskip
260 }
261
262 \def\postitemize{
263         \ifitems
264                 \vskip\postnesteditemizeskip
265         \else
266                 \ifnum\postitemizepenalty=0\else\penalty\postitemizepenalty\fi
267                 \vskip\postitemizeskip
268         \fi
269 }
270
271 \def\inititemize{\begingroup\preitemize\itemstrue\parindent=0pt}
272
273 \def\itemize#1{\inititemize\setbox\itembox\llap{#1\hskip\itemmarkerskip}%
274 \let\:=\singleitem}
275
276 \def\singleitem{\par\leavevmode\copy\itembox\ignorespaces}
277
278 \def\endlist{\par\endgroup\postitemize}
279
280 % Markers for \itemize
281 \def\ibull{\raise0.2ex\hbox{$\bullet$}}
282 \def\idot{\raise0.2ex\hbox{$\cdot$}}
283 \def\istar{\raise0.2ex\hbox{$\ast$}}
284
285 \def\numlist#1{\inititemize\itemcount=0\let\:=\numbereditem
286 \let\itemnumbering=#1}
287
288 \def\numbereditem{\par\leavevmode\advance\itemcount by 1
289 \llap{\itemnumbering\hskip\itemmarkerskip}\ignorespaces}
290
291 % Numbering styles for \numlist
292 \def\nnorm{\the\itemcount}
293 \def\ndotted{\nnorm.}
294 \def\nparen{\nnorm)}
295 \def\nparenp{(\nnorm)}
296 \def\nroman{\romannumeral\itemcount}
297 \def\nromanp{\nroman)}
298 \def\nalpha{\count@=96\advance\count@ by\itemcount\char\count@)}
299 \def\nAlpha{\count@=64\advance\count@ by\itemcount\char\count@)}
300 \def\ngreek{$\ifcase\itemcount\or\alpha\or\beta\or\gamma\or\delta\or\epsilon\or
301 \zeta\or\eta\or\theta\or\iota\or\kappa\or\lambda\or\mu\or\nu\or\xi\or\pi\or\rho
302 \or\sigma\or\tau\or\upsilon\or\phi\or\chi\or\psi\or\omega\fi$)}
303
304 %%% Miscellanea %%%
305
306 % {\I italic} with automatic italic correction
307 \def\I{\it\aftergroup\/}
308
309 % A breakable dash, to be repeated on the next line
310 \def\={\discretionary{-}{-}{-}}
311
312 % Non-breakable identifiers
313 \def\<#1>{\leavevmode\hbox{\I #1}}
314
315 % Handy shortcuts
316 \let\>=\noindent
317 \def\\{\hfil\break}
318
319 % Variants of \centerline, \leftline and \rightline, which are compatible with
320 % verbatim environments and other catcode hacks
321 \def\cline{\bgroup\def\linet@mp{\aftergroup\box\aftergroup0\aftergroup\egroup\hss\bgroup\aftergroup\hss\aftergroup\egroup}\afterassignment\linet@mp\setbox0\hbox to \hsize}
322 \def\lline{\bgroup\def\linet@mp{\aftergroup\box\aftergroup0\aftergroup\egroup\bgroup\aftergroup\hss\aftergroup\egroup}\afterassignment\linet@mp\setbox0\hbox to \hsize}
323 \def\rline{\bgroup\def\linet@mp{\aftergroup\box\aftergroup0\aftergroup\egroup\hss\bgroup\aftergroup\egroup}\afterassignment\linet@mp\setbox0\hbox to \hsize}
324
325 % Insert a PDF picture
326 % \putimage{width specification}{file}
327 \def\putimage#1#2{\hbox{\pdfximage #1{#2}\pdfrefximage\pdflastximage}}
328
329 %%% Colors %%%
330
331 % Use of pdfTeX color stack:
332 % \colorpush\rgb{1 0 0} puts a new color on the stack
333 % \colorset\rgb{1 0 0} replaces the top color on the stack
334 % \colorpop pops the top color
335 % \colorlocal\rgb{1 0 0} set a color locally until the end of the current group
336 \chardef\colorstk=\pdfcolorstackinit page direct{0 g 0 G}
337 \def\colorset#1{\pdfcolorstack\colorstk set #1}
338 \def\colorpush#1{\pdfcolorstack\colorstk push #1}
339 \def\colorpop{\pdfcolorstack\colorstk pop}
340 \def\colorlocal{\aftergroup\colorpop\colorpush}
341
342 % Different ways of describing colors: \rgb{R G B}, \gray{G}, \cmyk{C M Y K}
343 % (all components are real numbers between 0 and 1)
344 \def\rgb#1{{#1 rg #1 RG}}
345 \def\gray#1{{#1 g #1 G}}
346 \def\cmyk#1{{#1 k #1 K}}
347
348 %%% Localization %%%
349
350 % Current language
351 \def\localelang{en}
352
353 % Define a new localized string: \localedef{language}{identifier}{message}
354 \def\localedef#1#2{\expandafter\def\csname loc:#1:#2\endcsname}
355
356 % Expand a localized string in the current language: \localemsg{identifier}
357 \def\localestr#1{%
358         \ifcsname loc:\localelang:#1\endcsname
359                 \csname loc:\localelang:#1\endcsname
360         \else
361                 \ucwwarn{Localized string #1 not defined in language \localelang}%
362                 ???%
363         \fi
364 }
365
366 %%% Modules %%%
367
368 % Require a module: load it if it is not already loaded
369 \def\ucwmodule#1{
370         \ifcsname ucwmod:#1\endcsname
371         \else
372                 \input ucw-#1.tex
373         \fi
374 }
375
376 % Definition of a new module (to be placed at the beginning of its file)
377 % (Also guards against repeated loading if somebody uses \input instead of \ucwmodule.)
378 \def\ucwdefmodule#1{
379         \ifcsname ucwmod:#1\endcsname\endinput\fi
380         \expandafter\let\csname ucwmod:#1\endcsname=\relax
381 }
382
383 % Compatibility levels
384 % We try to be backwards compatible as much as we can, so all changes in behavior
385 % (except for addition of new control sequences) are versioned. By default, ucwmac
386 % starts in compatibility level 0, which should produce the same results as historic
387 % versions of ucwmac. Use \ucwcompat{level} to upgrade to a given level, or if you
388 % do not care about compatibility, \ucwcompat\ucwmaxcompat.
389
390 \chardef\ucwcurrentcompat=0     % Currently active compatibility level
391 \chardef\ucwmaxcompat=1         % Maximum supported compatibility level
392
393 \def\ucwcompat#1{
394         \ucwcurrentcompat=#1
395         \ifcase #1
396                 % Level 0 (default): old ucwmac
397         \or     % Level 1
398                 \preitemizepenalty=-500
399                 \postitemizepenalty=-500
400         \else\errmessage{Unsupported compatibility level #1 requested.}
401         \fi
402 }
403
404 %%% Epilog %%%
405
406 % Let's hide all internal macros
407 \catcode`@=12