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