]> mj.ucw.cz Git - ucwmac.git/blob - ucwmac.tex
LuaTeX: Moved PK resolution hack to ucwmac.tex
[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 % 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
211
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
217
218 \newif\ifitems\itemsfalse
219 \newbox\itembox
220 \newcount\itemcount
221
222 \def\preitemize{
223         \ifitems
224                 \vskip\prenesteditemizeskip
225                 \advance\leftskip by \nesteditemindent
226                 \advance\rightskip by \nesteditemnarrow
227         \else
228                 \vskip\preitemizeskip
229                 \advance\leftskip by \itemindent
230                 \advance\rightskip by \itemnarrow
231         \fi
232         \parskip=\interitemskip
233 }
234
235 \def\postitemize{
236         \ifitems
237                 \vskip\postnesteditemizeskip
238         \else
239                 \vskip\postitemizeskip
240         \fi
241 }
242
243 \def\inititemize{\begingroup\preitemize\itemstrue\parindent=0pt}
244
245 \def\itemize#1{\inititemize\setbox\itembox\llap{#1\hskip\itemmarkerskip}%
246 \let\:=\singleitem}
247
248 \def\singleitem{\par\leavevmode\copy\itembox\ignorespaces}
249
250 \def\endlist{\par\endgroup\postitemize}
251
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$}}
255
256 \def\numlist#1{\inititemize\itemcount=0\let\:=\numbereditem
257 \let\itemnumbering=#1}
258
259 \def\numbereditem{\par\leavevmode\advance\itemcount by 1
260 \llap{\itemnumbering\hskip\itemmarkerskip}\ignorespaces}
261
262 \def\nnorm{\the\itemcount}
263 \def\ndotted{\nnorm.}
264 \def\nparen{\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$)}
273
274 %%% Miscellanea %%%
275
276 % {\I italic} with automatic italic correction
277 \def\I{\it\aftergroup\/}
278
279 % A breakable dash, to be repeated on the next line
280 \def\={\discretionary{-}{-}{-}}
281
282 % Non-breakable identifiers
283 \def\<#1>{\leavevmode\hbox{\I #1}}
284
285 % Handy shortcuts
286 \let\>=\noindent
287 \def\\{\hfil\break}
288
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}
294
295 % Insert a PDF picture
296 % \putimage{width specification}{file}
297 \def\putimage#1#2{\hbox{\pdfximage #1{#2}\pdfrefximage\pdflastximage}}
298
299 % Let ~ be protected
300 \let\plaintilde=~
301 \protected\def~{\plaintilde}
302
303 %%% Colors %%%
304
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}
310
311 %%% Localization %%%
312
313 % Current language
314 \def\localelang{en}
315
316 % Define a new localized string: \localedef{language}{identifier}{message}
317 \def\localedef#1#2{\expandafter\def\csname loc:#1:#2\endcsname}
318
319 % Expand a localized string in the current language: \localemsg{identifier}
320 \def\localestr#1{%
321         \ifcsname loc:\localelang:#1\endcsname
322                 \csname loc:\localelang:#1\endcsname
323         \else
324                 \ucwwarn{Localized string #1 not defined in language \localelang}%
325                 ???%
326         \fi
327 }
328
329 %%% Modules %%%
330
331 \def\ucwmodule#1{
332         \ifcsname ucwmod:#1\endcsname
333         \else
334                 \input ucw-#1.tex
335         \fi
336 }
337
338 \def\ucwdefmodule#1{
339         \ifcsname ucwmod:#1\endcsname\endinput\fi
340         \expandafter\let\csname ucwmod:#1\endcsname=\relax
341 }
342
343 %%% Epilog %%%
344
345 % Let's hide all internal macros
346 \catcode`@=12