Plain Tex Grids – Creating a Background Baseline Grid with Output Routine

grid-typesettinggridsoutput-routineplain-tex

I am trying to get a baseline grid in the background of every page. I am using the plain format with XeTeX. This is what I have tried:

\baselineskip=13pt
\topskip=0pt \parskip=0pt
\newbox\gridbox
\setbox\gridbox\line{%
  \special{color push rgb .8 .8 1}%
  \vrule height\baselineskip width0pt \hrulefill%
  \special{color pop}}
\def\grid{\vbox to0pt{%
    \vtop to\vsize{\leaders\copy\gridbox\vfil}}}

Now, the above works just fine if I want to have a grid on a single page, by just calling \grid. However, I'd like it to be in every page, so I tried redefining plain's \pagebody:

\def\pagebody{\vbox to\vsize{\boxmaxdepth=\maxdepth \grid\pagecontents}}

but the grid keeps drifting from page to page even though I've removed every vertical stretchability/shrinkability.

What am I doing wrong?

UPDATE

Finally I managed to figure out what was causing the weird behaviour in my actual document vs. the minimal: it was \lineskiplimit=-\maxdimen. No-one could have guessed it of course. I don't know why it affects anything; the way I've understood, its only function is to figure out which one to use, \baselineskip or \lineskip, and since I have them both set the same, I had the \lineskiplimit the way it was. But it seems it's doing something besides that (I've already removed it from the \displ@y macro, for example).

Best Answer

Some corrections are necessary (you're forgetting \topskip)

\newbox\gridbox
\setbox\gridbox\line{%
  \special{color push rgb .8 .8 1}%
  \vrule height\baselineskip width0pt \hrulefill
  \special{color pop}}
\def\grid{\vtop to0pt{\hrule height0pt\kern-\dimexpr\baselineskip-\topskip\relax
    \vbox to\dimexpr\vsize+2pt\relax{\leaders\copy\gridbox\vfil}\vss}}
\def\pagebody{\vbox to\vsize{\boxmaxdepth=\maxdepth \grid\pagecontents}}

\parskip=0pt \vsize=\dimexpr\topskip+44\baselineskip\relax % 45 lines per page

\def\1{As any dedicated reader can clearly see, the Ideal of
practical reason is a representation of, as far as I know, the things
in themselves; as I have shown elsewhere, the phenomena should only be
used as a canon for our understanding. The paralogisms of practical
reason are what first give rise to the architectonic of practical
reason. As will easily be shown in the next section, reason would
thereby be made to contradict, in view of these considerations, the
Ideal of practical reason, yet the manifold depends on the phenomena.
Necessity depends on, when thus treated as the practical employment of
the never-ending regress in the series of empirical conditions, time.
Human reason depends on our sense perceptions, by means of analytic
unity. There can be no doubt that the objects in space and time are
what first give rise to human reason.

Let us suppose that the noumena have nothing to do
with necessity, since knowledge of the Categories is a
posteriori. Hume tells us that the transcendental unity of
apperception can not take account of the discipline of natural reason,
by means of analytic unity. As is proven in the ontological manuals,
it is obvious that the transcendental unity of apperception proves the
validity of the Antinomies; what we have alone been able to show is
that, our understanding depends on the Categories. It remains a
mystery why the Ideal stands in need of reason. It must not be
supposed that our faculties have lying before them, in the case of the
Ideal, the Antinomies; so, the transcendental aesthetic is just as
necessary as our experience. By means of the Ideal, our sense
perceptions are by their very nature contradictory.

}

\1\1\1\1\1\1\1\1\1

\bye

EDIT

A different definition of \grid that better shows what happens:

\def\grid{%
  \vtop to 0pt{
    \hrule height 0pt % set the reference point
    \special{color push rgb .8 .8 1}
    \vbox to \vsize{
      \vskip\topskip\vskip-0.4pt % backup because of the first rule
      \hrule
      \cleaders\vbox to\baselineskip{\vfill\hrule width \hsize}\vfill
    }
    \special{color pop}
    \vss % we want to have zero depth
  }%
}
Related Question