Debian Codegraph @

texmf-dist/tex/generic/midnight/quire.tex

raw
1 \ifx\QUIRE\relax\endinput\else\let\QUIRE=\relax\fi % \input only once
2 %
3 % quire.tex: Macros for making booklets, printing double pages, and
4 % printing outlines and crop marks.
5 % version: 1.0 release: 19 January 1991
6 %
7 % copyright (c) 1991 Marcel R. van der Goot
8 % You can use these macros to typeset documents. You may
9 % distribute this file freely, provided that you also distribute
10 % the accompanying documentation.
11 % You may make changes to this file, or extract portions
12 % of it for inclusion in other files, provided that
13 % (1) you change the name of the file;
14 % (2) you give proper credit and include copyright
15 % information where applicable;
16 % (3) explain how an unchanged version can be obtained; and
17 % (4) document the usage of your macros/changes (if usage
18 % of your macros is not worth documenting, they must not
19 % be worth using).
20 % You are not allowed to use the name ``Midnight Macros'' for
21 % any changed files.
22 % The above rules for making changes do not apply where it
23 % is explicitly noted in this file that something can be changed
24 % to conform to your local installation.
25 %
26 % USAGE:
27 % See the file quire.doc
28 %
29 % original: csvax.cs.caltech.edu [131.215.131.131] in pub/tex
30 % (use anonymous ftp). Also in various archives.
31 %
32 % Caltech, Pasadena --- Marcel van der Goot
33 % [email protected]
34 % Caltech 256--80
35 % Pasadena, CA 91125
36 % USA
37 % (818) 356--4603
38 %
39
40 % update history:
41 % version 1.0: This one.
42 % release 19 Jan 1991: This one.
43 %
44
45 %%%%%% CODE: (you don't need to read this to use the macros)
46
47 %%%%%%%%%%%%%%% registers accessible by the user
48
49 % dimens related to pages:
50 \newdimen\vorigin
51 \newdimen\horigin
52 \newdimen\vtotal
53 \newdimen\htotal
54 % also used: dimens \voffset, \hoffset
55
56 % dimens related to sheets
57 \newdimen\shoutline
58 \newdimen\shstaplewidth
59 \newdimen\shstaplelength
60 \newdimen\shcrop
61 \newdimen\shvoffset
62 \newdimen\shhtotal
63 \newdimen\shthickness
64 \newdimen\shvcorrection
65 \newdimen\shhcorrection
66
67 % token register related to sheets
68 \newtoks\shfootline
69
70 %%%%%%%%%%%%%%% the implementation
71
72 % Private identifiers are prefixed with `q_'; in the comments below,
73 % this prefix is omitted for readability.
74
75 \shoutline=\catcode`_ sp % minor trick to remember the original catcode
76 \catcode`_=11 % letter
77
78 %%%%% The stack
79 % A stack of boxes is needed to store pages that cannot be printed yet.
80 % The stack is formed by \stackbox, an hbox storing the stacked boxes,
81 % and \stacktop, the top box on the stack. The stack top can be accessed
82 % by \copy\stacktop or \box\stacktop, but the latter should only be
83 % used if the next stack operation is \pop. There is no stackcounter
84 % implemented, because we don't need it here.
85
86 \newbox\q_stackbox % an hbox keeping the stacked boxes but one
87 \newbox\q_stacktop % the topmost box
88
89 \def\q_push#1% #1 = a box
90 {\global\setbox\q_stackbox=
91 \hbox{\unhbox\q_stackbox \box\q_stacktop}%
92 \global\setbox\q_stacktop=#1%
93 }
94
95 \def\q_pop
96 {\global\setbox\q_stackbox=
97 \hbox{\unhbox\q_stackbox \global\setbox\q_stacktop=\lastbox}%
98 }
99
100
101 %%%%% Handling pages
102 % \makepage takes a box representing the printed material (this box is the
103 % argument of \shipout), and makes it into a page. A page is represented
104 % by an hbox with height \vtotal, width \htotal, and depth 0.
105
106 \def\q_makepage#1% #1 = textpage, return = an hbox (\vtotal+0)*\htotal
107 {\hbox to\htotal
108 {\boxmaxdepth=0pt
109 \advance\vorigin by\voffset
110 \advance\horigin by\hoffset
111 \kern\horigin
112 \vbox to\vtotal
113 {\kern\vorigin
114 #1%
115 \vss
116 }%
117 \hss
118 }%
119 }
120
121 % \doublepage takes two pages produced by \makepage, and puts them next
122 % to each other in a vbox. Between the two pages there is two times the
123 % space specified by \thickoffset. \doublepage draws lines around the
124 % produced vbox, and draws `staples' in the center.
125
126 \newdimen\q_thickoffset
127
128 \def\q_doublepage#1#2% #1 and #2 are page boxes,
129 % return = vbox ((\vtotal+2\shoutline)+0)*
130 % (2(\htotal+\thickoffset+\shoutline))
131 {\vbox{\vbox to0pt{\vss\q_vcrop\kern3mm}%
132 \hrule height\shoutline depth0pt
133 \hbox{\hbox to0pt{\hss\q_hcrop\kern3mm}%
134 \vrule width\shoutline
135 #1%
136 \kern\q_thickoffset
137 \q_fold
138 \kern\q_thickoffset
139 #2%
140 \vrule width\shoutline
141 \hbox to0pt{\kern3mm\q_hcrop\hss}%
142 }%
143 \hrule height\shoutline depth0pt
144 \vbox to0pt{\kern3mm\q_vcrop\vss}%
145 }%
146 }
147
148 % \fold returns an hbox containing the two `staples'. The hbox has
149 % width 0 so that it does not interfere with the separation between
150 % double pages.
151
152 \def\q_fold % return = an hbox (\vtotal+0)*0
153 {\hbox to0pt
154 {\divide\vtotal by 4
155 \advance\vtotal by-0.5\shstaplelength
156 \hss
157 \vbox{\kern\vtotal
158 \hrule width\shstaplewidth height\shstaplelength depth0pt
159 \kern 2\vtotal
160 \hrule width\shstaplewidth height\shstaplelength depth0pt
161 \kern\vtotal
162 }%
163 \hss
164 }%
165 }
166
167 % \vcrop returns an hbox containing the three vertical crop marks,
168 % \hcrop a vbox with the two horizontal marks.
169
170 \def\q_vcrop % return = an hbox (15mm+0)*(2(\htotal+\thickoffset+\shoutline))
171 {\hbox{\kern\shoutline
172 \hbox to0pt{\vrule height15mm depth0pt width\shcrop\hss}%
173 \kern\htotal \kern\q_thickoffset
174 \hbox to0pt{\hss\vrule height15mm depth0pt width\shcrop\hss}%
175 \kern\q_thickoffset \kern\htotal
176 \hbox to0pt{\hss\vrule height15mm depth0pt width\shcrop}%
177 \kern\shoutline
178 }%
179 }
180
181 \def\q_hcrop % return = a vbox (vtotal+0)*15mm
182 {\vbox{\vbox to0pt{\hrule width15mm height\shcrop depth0pt\vss}%
183 \nointerlineskip
184 \kern\vtotal
185 \vbox to0pt{\vss\hrule width15mm height\shcrop depth0pt}%
186 }%
187 }
188
189
190 %%%%% Shipout routines
191 % Suppose Q is the number of pages per quire (rounded upwards to the
192 % nearest multiple of 4), then there are Q/2 sheets per quire. The first
193 % Q/2 pages have to be stacked, which is done by the shipout routine
194 % \collect. The next Q/2 pages have to be combined with the stacked ones
195 % into double pages, which are then printed. This is done by the shipout
196 % routine \combine. \goal is the number Q/2, \sheetnr is used to count the
197 % sheets of the current quire, \qnr counts sheets in completed quires
198 % (\count0 is set to the actual sheet number when a sheet is shipped out).
199 % The current shipout routine is held in \ship, \shipbox is the box
200 % that must be shipped out. \oldship is the original \shipout.
201 % In order to print partial quires if there is not enough memory to
202 % store Q/2 pages, the user can specify a range of sheets that should
203 % be printed. This range is tested with \ifrange; sheets that fall
204 % outside the range are counted, but otherwise ignored.
205 %
206 % Normally, \shipout resets \deadcycles to 0 and a call of the output
207 % routine (by TeX's page-breaking mechanism) increments it. We do not
208 % want to reset \deadcycles until enough pages for a quire have been
209 % collected, because as long as \deadcycles is not 0, TeX will produce
210 % more pages. We cannot allow it to be continuously incremented either,
211 % since TeX gives an error message if it becomes too high. Therefore,
212 % we set it to 1 if we need more pages. (Actually, the algorithm below
213 % always requires a full quire, even if only a partial one is printed.)
214 %
215 % In \shfootline we want to be able to print the page number of the
216 % actual pages. To save the page number, we use a ``dirty trick''
217 % (the only one in these macros, as far as I can tell): we push a box
218 % with the pagenumber as width on the stack. We recover the pagenumber by
219 % using that width as a count, which replaces the dimen (\wd) by its
220 % value in sp. The pagenumbers are also shipped out as \count1 and 2.
221
222 \newcount\q_sheetnr
223 \newcount\q_qnr
224
225 \newif\ifq_range
226
227 \newbox\q_shipbox
228
229 \def\q_collect
230 {\message{[(\the\count0)]}%
231 \global\advance\q_sheetnr by1 \q_checkrange
232 \ifq_range
233 \q_push{\q_makepage{\box\q_shipbox}}%
234 \q_push{\hbox to\count0 sp{}}% dirty trick
235 \fi
236 \global\deadcycles=1
237 \ifnum\q_sheetnr=\q_goal
238 \global\q_thickoffset=0pt
239 \global\let\q_ship=\q_combine
240 \fi
241 }
242
243 \def\q_combine
244 {{\ifq_range
245 \count1=\wd\q_stacktop \q_pop % \firstpageno
246 \count2=\count0 % \secondpageno
247 \ifodd\q_sheetnr % stacktop is an odd page
248 \q_print{\q_makepage{\box\q_shipbox}}{\box\q_stacktop}%
249 \else\q_print{\box\q_stacktop}{\q_makepage{\box\q_shipbox}}%
250 \fi
251 \q_pop
252 \else\message{[(\the\count0)]}%
253 \fi
254 \ifodd\q_sheetnr\global\advance\q_thickoffset by\shthickness\fi
255 \global\advance\q_sheetnr by-1 \q_checkrange
256 \ifnum\q_sheetnr=0
257 \global\advance\q_qnr by\q_goal
258 \global\let\q_ship=\q_collect
259 \global\deadcycles=\q_cycles
260 \else\global\deadcycles=1
261 \fi
262 }}
263
264 \def\q_print#1#2% two page boxes
265 {{\count0=\q_qnr \advance\count0 by\q_sheetnr
266 \setbox0=\hbox to\shhtotal
267 {\hss
268 \boxmaxdepth=0pt
269 \vbox{\offinterlineskip
270 \q_doublepage{#1}{#2}%
271 \kern1pc
272 \hbox{\kern\q_thickoffset
273 \hbox to 2\htotal{\the\shfootline}%
274 }%
275 }%
276 \hss
277 }%
278 \hoffset=-1truein % defeat the annoying 1 inch offset
279 \voffset=\shvoffset
280 \ifodd\count0
281 \advance\hoffset by\shhcorrection
282 \advance\voffset by\shvcorrection
283 \fi
284 \q_oldship\box0
285 \xdef\q_cycles{\the\deadcycles}%
286 }}
287
288
289 %%%%% shipout
290 % This is perhaps the most obscure part, although it actually has little
291 % to do with the problem at hand.
292 % What I would have liked to do is define the above \collect and \combine
293 % as taking a parameter #1 and then use #1 instead of \shipbox. Then
294 % we could just say \let\shipout=\collect etc. Unfortunately, that doesn't
295 % work: The actual \shipout is not a macro but a built-in command that
296 % must be followed by a <box>. As a result, \shipout\box0 works
297 % fine, but \shipout{\box0} generates an error (because the first `{' is
298 % not the start of a <box>). On the other hand, \collect\box0 would make
299 % #1 -> \box instead of \box0, and therefore wouldn't work; only
300 % \collect{\box0} would work. It really seemed highly preferrable to make
301 % the shipout replacements work in the same way as the original. Therefore,
302 % \boat will read a box and put it in \shipbox, and then call \ship.
303 % This has the additional advantage that the box is actually constructed
304 % before the shipout replacement does anything (if you do
305 % \collect{\hbox {...}} the box is only constructed when #1 is used).
306 % The routine \boat must be followed by a <box> because of the \setbox.
307 % Again, I would have liked to do
308 % \def\boat{\afterassignment\ship\setbox\shipbox=}
309 % but \afterassignment behaves different for a <box> of the form
310 % \hbox {...} than for one of the form \box0. \tstship tests whether
311 % the assignment has actually taken place, or whether we are at the
312 % beginning of an \hbox {<here> ...} etc. This way of testing
313 % is not perfect, it doesn't work if you try to ship out a void box. However,
314 % that is a strange thing to do, and this is by far the easiest solution
315 % I could come up with.
316
317 \def\q_boat
318 {{\setbox255=\box\q_shipbox}% makes \shipbox void
319 \afterassignment\q_tstship
320 \global\setbox\q_shipbox= % \boat must be followed by a <box>
321 }
322
323 \def\q_tstship
324 {\ifvoid\q_shipbox\aftergroup\q_ship
325 \else\q_ship
326 \fi
327 }
328
329 %%%%% Public routines for making quires
330 % The routine that sets up printing quires is \makequire. It takes 3
331 % arguments, the number of pages per quire (Q before rounding), the
332 % number of the first sheet in the printing range, and the extend
333 % (in sheet numbers) of the printing range. It uses these numbers to
334 % define \checkrange, used by the output routines.
335 % If there is sufficient memory to print a full quire, then \quire
336 % can be used instead of \makequire. \quire only takes the number of
337 % pages.
338
339 \def\q_init
340 {\global\q_sheetnr=0\global\q_qnr=0
341 \global\let\q_oldship=\shipout
342 \global\let\shipout=\q_boat
343 \xdef\q_cycles{\the\deadcycles}%
344 \global\let\endquire=\q_endquire
345 }
346
347 \def\makequire#1#2#3% #1 = nr of pages, #2 = first sheet, #3 = size of range
348 {{\count0=#1%
349 \advance\count0 by3 \divide\count0 by4 \multiply\count0 by2
350 \xdef\q_goal{\the\count0}%
351 \count0=#2%
352 \count1=#3\advance\count1 by\count0 \advance\count1 by-1
353 \let\nox=\noexpand
354 \xdef\q_checkrange
355 {\nox\ifnum \q_sheetnr < \the\count0
356 \nox\global\nox\q_rangefalse
357 \nox\else\nox\ifnum \the\count1 < \q_sheetnr
358 \nox\global\nox\q_rangefalse
359 \nox\else \nox\global\nox\q_rangetrue
360 \nox\q_parity
361 \nox\fi\nox\fi
362 }%
363 \q_init
364 \global\let\q_ship=\q_collect
365 }}
366
367 \def\quire#1% #1 = nr of pages
368 {\makequire{#1}{0}{1000}%
369 }
370
371 % \endquire can be used to finish the quire before the end of the
372 % input is reached. It ships out the current page, then ships out empty
373 % boxes until the quire is finished.
374
375 \let\endquire=\relax % if \endquire is used when \quire is not
376
377 \def\q_endquire
378 {{\vfill\supereject
379 \loop
380 \ifnum\q_sheetnr>0 \shipout\vbox{}%
381 \repeat
382 \global\let\shipout=\q_oldship
383 }}
384
385 % Normally both even and odd sheets are printed. However, one can surpress
386 % one class by using \shonly#1. If #1 = 0, both are printed. Otherwise
387 % only sheets with the same parity as #1 are printed.
388
389 \def\shonly#1% a number
390 {\ifnum #1=0
391 \gdef\q_parity{}%
392 \else\ifodd#1%
393 \gdef\q_parity{\ifodd\q_sheetnr\else\global\q_rangefalse\fi}%
394 \else\gdef\q_parity{\ifodd\q_sheetnr\global\q_rangefalse\fi}%
395 \fi\fi
396 }
397
398 % In LaTeX, \end is redefined to set \deadcycles to 0 before it does the
399 % actual \end. This means that the quire is not actually finished if the
400 % text doesn't have enough pages (see the earlier discussion about
401 % \deadcycles). You cannot solve this by using \endquire, because then
402 % not all output is actually finished (also because of various LaTeX
403 % definitions). Therefore, in \latexquire, we redefine \@@end, which is
404 % what LaTeX thinks is the original \end, to do an \endquire just before
405 % the actual \end.
406
407 {
408 \catcode`@=11
409 \gdef\latexquire
410 {\global\let\q_end=\@@end
411 \gdef\@@end{\endquire\q_end}%
412 }
413 }
414
415 %%%%% Some extras
416 % Quires are not so easy to preview. Therefore we also define
417 % \qtwopages for printing double pages (without reordening them) and
418 % \qonepage for printing single pages (with outlines etc.). Most of
419 % the needed routines were already there anyway.
420 % \two is the shipout routine used for \qtwopages. It uses \qnr to
421 % count pages, \sheetnr to distinguish between the first and the second
422 % page of a pair. (We count with \qnr because \endquire wants \sheetnr
423 % to be 0 at the end. But note that \parity looks at \sheetnr to determine
424 % whether the page must be printed.
425
426 \def\q_two
427 {{\ifodd\q_sheetnr % second page
428 \global\q_sheetnr=0
429 \ifq_range
430 \count1=\wd\q_stacktop \q_pop % \firstpageno
431 \count2=\count0 % \secondpageno
432 \q_print{\box\q_stacktop}{\q_makepage{\box\q_shipbox}}%
433 \q_pop
434 \else\message{[(\the\count0)]}%
435 \fi
436 \global\deadcycles=\q_cycles
437 \else % first page
438 \message{[(\the\count0)]}%
439 \global\advance\q_qnr by1
440 \global\q_sheetnr=\q_qnr
441 \global\q_rangetrue \q_parity
442 \ifq_range
443 \q_push{\q_makepage{\box\q_shipbox}}%
444 \q_push{\hbox to\count0 sp{}}% that same dirty trick again
445 \fi
446 \global\deadcycles=1
447 \global\q_sheetnr=1
448 \fi
449 }}
450
451 \def\qtwopages
452 {\q_init
453 \global\let\q_ship=\q_two
454 \global\q_thickoffset=0pt
455 }
456
457 % \one is the output routine for \qonepage. \qnr is used to count
458 % the pages (again because of \endquire).
459
460 \def\q_one
461 {{\global\advance\q_qnr by1
462 \global\q_sheetnr=\q_qnr
463 \global\q_rangetrue \q_parity
464 \global\q_sheetnr=0
465 \ifq_range
466 \count1=\count0 \count2=0
467 \divide\htotal by2
468 \q_print{\q_makepage{\box\q_shipbox}}{\q_makepage{}}%
469 \else\message{[(\the\count0)]}%
470 \fi
471 \global\deadcycles=\q_cycles
472 }}
473
474 \def\qonepage
475 {\q_init
476 \global\let\q_ship=\q_one
477 \global\q_thickoffset=0pt
478 \global\let\q_fold=\relax
479 }
480
481 % restore \catcode`_ to whatever it was
482
483 \catcode`_=\shoutline
484
485 \countdef\firstpageno=1
486 \countdef\secondpageno=2
487
488 \shonly0 % to make sure that \parity is defined
489
490 %%%%% Initial values for user parameters
491
492 \shfootline={\tenrm\qquad\jobname\qquad\number\count0{} (\number\count1,
493 \number\count2)\hfil}
494
495 \shhtotal=22in
496 \shthickness=.2mm
497 \shvcorrection=0pt
498 \shhcorrection=0pt
499
500 \shvoffset=0pt
501 \shoutline=1.5pt
502 \shstaplewidth=1.5pt
503 \shstaplelength=2cm
504 \shcrop=3pt
505
506 \vorigin=1in
507 \horigin=1in
508
509 \htotal=8.5in
510 \vtotal=11in
511
512 %%%%% Local definitions
513 % The above is the standard file quire.tex.
514 % CHANGE: What follows are local definitions; you should replace them with
515 % appropriate values for your own installation.
516
517 \def\Pmaser
518 {\shhtotal=220.02true mm
519 \shthickness=0.21true mm
520 \message{maser: flip paper so that short sides remain in place, %
521 align closest right-hand corner in tray.}%
522 }
523
524 \def\Pmaserlandscape
525 {\shhtotal=281true mm
526 \shthickness=0.21true mm
527 \message{maser landscape: flip paper so that long sides remain in place, %
528 align closest right-hand corner in tray.}%
529 }
530
531 \def\Phaser
532 {\shhtotal=216.4truemm
533 \shthickness=0.21truemm
534 \message{haser: rotate (but do not flip) pages 180 degrees, and reorder.}
535 }
536
537 \def\Phaserlandscape
538 {\shhtotal=283truemm
539 \shthickness=0.21truemm
540 \message{haser landscape: do not flip or rotate pages, but reorder them.}
541 }