[Tex/LaTex] How does \headline work

formattingheader-footerplain-tex

I understand that TeX uses \headline to place headers. If \headline did not exist, how would one implement a simple version of the same? Additionally, where might one find simple documentation of how \headline is implemented beyond The TeXbook? Of one particular interest is the use of the commands used to position the header text (or the box in which the header is set).

Best Answer

\headline is a named token register in plain.tex, assigned with \newtoks\headline, and set to \headline={\hfil} (blank). Its content is then used in the output routine via the \makeheadline macro (simplified, and written "open"):

\def\makeheadline{\vbox to 0pt{\vskip-22.5pt
  \hbox to \hsize{\vbox to 8.5pt{}\the\headline}\vss}\nointerlineskip}

That is, the default contains "hard constants" for use with a 10pt setting. And the content of the register is used in horizontal mode (so cannot have vertical mode material if not inside a \vbox), spanning the h orizontal size of the textblock. That whole box is set so that it doesn't take any vertical space, and backed up by the constant of 22.5pt above the actual textblock.

Knuth writes in the TeXbook regarding that 22.5pt constant:

The magic constant -22.5pt is equal to (topskip - height of strut - 2(baselineskip)), i.e., 10pt - 8.5pt - 24pt; this places the reference point of the headline exactly 24pt above the reference point of the top line on the page, unless the headline or the top line are excessively large.

The macro is also refraining inserting the normal interlineskip between the headline and the typeblock.

So setting for example:

\headline={Testing headline\hfill \folio}

would have "Testing headline" to the left, then the glue pushing the page number to the right.

Now, this would keep the same for every page, but say you want to have different head for verso and recto you could do something like

\newtoks\versohead \versohead={\folio\hfill Testing headline}
\newtoks\rectohead \rectohead={Testing headline\hfill \folio}
\headline={\ifodd\pageno\the\versohead\else\the\rectohead\fi}
Related Question