What is \box255?
Solution 1
When TeX decides it's time to eject a page (because one has been filled up or the user has issued a command to the same effect), it does some maintenance work and then packs the page's contents into the box register 255.
Next it calls the output routine, which is supposed to do several things: adding the header and the footer, incrementing the page number and so on.
The simplest output routine would be
\output{\shipout\box255}
which would do nothing to the page other than shipping it out. The standard output routine in Plain TeX is (in simplified form)
\output={\plainoutput}
\def\plainoutput{\shipout\vbox{\makeheadline\pagebody\makefootline}%
\advancepageno
\ifnum\outputpenalty>-20000 \else\dosupereject\fi}
\def\pagebody{\vbox to\vsize{\boxmaxdepth=\maxdepth \pagecontents}}
\def\makeheadline{\vbox to 0pt{\vskip-22.5pt
\line{\vbox to8.5pt{}\the\headline}\vss}\nointerlineskip}
\def\makefootline{\baselineskip=24pt \lineskiplimit=0pt
\line{\the\footline}}
\def\dosupereject{\ifnum\insertpenalties>0 % something is being held over
\line{}\kern-\topskip\nobreak\vfill\supereject\fi}
\def\pagecontents{\ifvoid\topins\else\unvbox\topins\fi
\dimen@=\dp255 \unvbox255
\ifvoid\footins\else % footnote info is present
\vskip\skip\footins \footnoterule \unvbox\footins\fi
\[email protected] \kern-\dimen@ \vfil \fi}
\def\footnoterule{\kern-3pt
\hrule width 2truein \kern 2.6pt} % the \hrule is .4pt high
where the usage of \box255
is more complicated because it keeps track of insertions such as floating figures and footnotes; the \shipout
operations is performed to another box which is in turn built using the contents of \box255
.
(I'm not even trying and confront the LaTeX output routine, which is vastly more complicated than this one, but the sense of the thing is just the same.)
This box register is automatically managed by TeX and it's quite dangerous to make use of it (if your name isn't Heiko Oberdiek).
The package atbegshi
does some trick by redefining \shipout
, adding hooks to it so the contents of the box being shipped out can be tweaked in various ways.
The first set of macros doing something of this can be found in quire.tex
, a remarkable piece of code that I believe is still usable even with LaTeX; but there are easier ways now to achieve the same effect.
% quire.tex: Macros for making booklets, printing double pages, and
% printing outlines and crop marks.
% version: 1.0 release: 19 January 1991
%
% copyright (c) 1991 Marcel R. van der Goot
that used a similar trick for producing quires, that is, sets of pages to be printed on the same sheet of paper and then cut and folded.
Solution 2
I found this: http://www.scribd.com/doc/91214700/444/Output-and-box255
Related videos on Youtube
sdaau
Updated on May 27, 2020Comments
-
sdaau almost 3 years
I'm just reading the everyshi manual, and it says:
This package provides the hooks
\EveryShipout
and\AtNextShipout
whose arguments are executed after the output routine has constructed\box255
, and before\shipout
is called.This seems to imply this
\box255
is somehow special; but I tried looking it up on the net and could find terribly much about it.So, can anyone explain why would
\box255
be special, and possibly point to a quick reading or tutorial involving it?-
Martin Scharrer almost 11 yearsPlease do not use a closing statement. Usually, we don't put a greeting or a "thank you" in our posts. While this might seem strange at first, it is not a sign of lack of politeness, but rather part of our trying to keep everything very concise. Upvoting is the preferred way here to say "thank you" to users who helped you.
-
Martin Scharrer almost 11 yearsAFAIK the whole page is stored in
\box255
.\box<number>
is just a way to reference one of TeX box registers. TeX (at least the non-extended version) has only 256 box registers (0-255) and 255 is simply the last one. For some reason it is reserved for the output routine. -
Martin Scharrer almost 11 yearsNote that the
atbegshi
package "is a modern reimplementation of packageeveryshi
" (sic) and uses\AtBeginShipoutBox
instead of\box255
. However it might use box #255 internally anyway. -
sdaau almost 11 yearsThanks for the note, @MartinScharrer - I'm aware of the closing statements policy, but I got used to adding them to sort of "warm up" the conversation
:)
Btw, would you mind putting your\box255
comments as an answer? I think they are a perfectly acceptable explanation (unless a more in-depth answer shows up). Many thanks - cheers!
-
-
sdaau almost 11 yearsMany thanks @egreg for the great answer, especially the explanation of the output routine - cheers!
-
Yiannis Lazarides almost 11 yearsThis is from TexByTopic which is available free.
-
sdaau almost 11 yearsThanks for pointing to that resource, walle1357 and @YiannisLazarides; here's TeX by Topic homepage - cheers!
-
Ryan Reich almost 11 yearsI keep finding answers of yours that I have mysteriously neglected to upvote during my hiatus.