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