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