]> mj.ucw.cz Git - ucwmac.git/blob - ucwmac.tex
Comments and a couple of test cases
[ucwmac.git] / ucwmac.tex
1 % The UCW Macro Collection (a successor of mjmac.tex)
2 % Written by Martin Mares <mj@ucw.cz> in 2010 and placed into public domain
3 % -------------------------------------------------------------------------
4
5 %%% Prolog %%%
6
7 % We'll use internal macros of plain TeX
8 \catcode`@=11
9
10 %%% PDF output detection %%%
11
12 \newif\ifpdf
13 \pdffalse
14
15 \ifx\pdfoutput\undefined
16 \else\ifnum\pdfoutput>0
17         \pdftrue
18 \fi\fi
19
20 %%% Page size and margins %%%
21
22 % If you modify these registers, call \setuppage afterwards
23 \newdimen\pagewidth
24 \newdimen\pageheight
25 \newdimen\leftmargin
26 \newdimen\rightmargin
27 \newdimen\topmargin
28 \newdimen\bottommargin
29 \newdimen\evenpageshift
30
31 \def\setuppage{%
32         \hsize=\pagewidth
33         \advance\hsize by -\leftmargin
34         \advance\hsize by -\rightmargin
35         \vsize=\pageheight
36         \advance\vsize by -\topmargin
37         \advance\vsize by -\bottommargin
38         \hoffset=\leftmargin
39         \advance\hoffset by -1truein
40         \voffset=\topmargin
41         \advance\voffset by -1truein
42         \ifpdf
43                 \pdfpagewidth=\pagewidth
44                 \pdfpageheight=\pageheight
45         \fi
46 }
47
48 % Set multiple margins to the same value
49 \def\sethmargins#1{\leftmargin=#1\relax\rightmargin=#1\relax\evenpageshift=0pt\relax}
50 \def\setvmargins#1{\topmargin=#1\relax\bottommargin=#1\relax}
51 \def\setmargins#1{\sethmargins{#1}\setvmargins{#1}}
52
53 % Define inner/outer margin instead of left/right
54 \def\setinneroutermargin#1#2{\sethmargins{#1}{#2}\evenpageshift=\rightmargin\advance\evenpageshift by -\leftmargin}
55
56 % Use a predefined paper format, calls \setuppage automagically
57 \def\setpaper#1{%
58         \expandafter\let\expandafter\currentpaper\csname paper-#1\endcsname
59         \ifx\currentpaper\relax
60                 \errmessage{Undefined paper format #1}
61         \fi
62         \currentpaper
63 }
64
65 % Switch to landscape orientation, calls \setuppage automagically
66 \def\landscape{%
67         \dimen0=\pageheight
68         \pageheight=\pagewidth
69         \pagewidth=\dimen0
70         \setuppage
71 }
72
73 % Common paper sizes
74 \def\defpaper#1#2#3{\expandafter\def\csname paper-#1\endcsname{\pagewidth=#2\pageheight=#3\setuppage}}
75 \defpaper{a3}{297truemm}{420truemm}
76 \defpaper{a4}{210truemm}{297truemm}
77 \defpaper{a5}{148truemm}{210truemm}
78 \defpaper{a6}{105truemm}{148truemm}
79 \defpaper{letter}{8.5truein}{11truein}
80 \defpaper{legal}{8.5truein}{14truein}
81
82 % Default page parameters
83 \setmargins{1truein}
84 \setpaper{a4}
85
86 %%% Placing material at specified coordinates %%%
87
88 % Set all dimensions of a given box register to zero
89 \def\smashbox#1{\ht#1=0pt \dp#1=0pt \wd#1=0pt}
90 \def\smashedhbox#1{{\setbox0=\hbox{#1}\smashbox0\box0}}
91 \def\smashedvbox#1{{\setbox0=\vbox{#1}\smashbox0\box0}}
92
93 % Variants of \llap and \rlap working equally on both sides and/or vertically
94 \def\hlap#1{\hbox to 0pt{\hss #1\hss}}
95 \def\vlap#1{\vbox to 0pt{\vss #1\vss}}
96 \def\clap#1{\vlap{\hlap{#1}}}
97
98 % \placeat{right}{down}{hmaterial} places <hmaterial>, so that its
99 % reference point lies at the given position wrt. the current ref point
100 \long\def\placeat#1#2#3{\smashedhbox{\hskip #1\lower #2\hbox{#3}}}
101
102 % Like \vbox, but with reference point in the upper left corner
103 \def\vhang#1{\vtop{\hrule height 0pt\relax #1}}
104
105 % Like \vhang, but respecting interline skips
106 \def\vhanglines#1{\vtop{\hbox to 0pt{}#1}}
107
108 % Crosshair with reference point in its center
109 \def\crosshair#1{\clap{\vrule height 0.2pt width #1}\clap{\vrule height #1 width 0.2pt}}
110
111 %%% Output routine %%%
112
113 \newbox\pageunderlays
114 \newbox\pageoverlays
115 \newbox\commonunderlays
116 \newbox\commonoverlays
117
118 % In addition to the normal page contents, you can define page overlays
119 % and underlays, which are zero-size vboxes positioned absolutely in the
120 % front / in the back of the normal material. Also, there are global
121 % versions of both which are not reset after every page.
122 \def\addlay#1#2{\setbox#1=\vbox{\ifvbox#1\box#1\fi\smashedvbox{#2}}}
123 \def\pageunderlay{\addlay\pageunderlays}
124 \def\pageoverlay{\addlay\pageoverlays}
125 \def\commonunderlay{\addlay\commonoverlays}
126 \def\commonoverlay{\addlay\commonoverlays}
127
128 % Our variation on \plainoutput, which manages inner/outer margins and overlays
129 \output{\ucwoutput}
130 \def\ucwoutput{\wigglepage\shipout\vbox{%
131         \makeheadline
132         \ifvbox\commonunderlays\copy\commonunderlays\nointerlineskip\fi
133         \ifvbox\pageunderlays\box\pageunderlays\nointerlineskip\fi
134         \pagebody
135         \ifvbox\commonoverlays\vbox to 0pt{\vskip -\vsize\copy\commonoverlays}\fi
136         \ifvbox\pageoverlays\vbox to 0pt{\vskip -\vsize\box\pageoverlays}\fi
137         \makefootline
138 }\advancepageno
139 \ifnum\outputpenalty>-\@MM \else\dosupereject\fi}
140
141 \def\wigglepage{\ifodd\pageno\else\advance\hoffset by \evenpageshift\fi}
142
143 %%% Epilog %%%
144
145 % Let's hide all internal macros
146 \catcode`@=12