# Background baseline grid with output routine

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{%


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).

-
Should this question de retagged {grid-typesetting}? It seems to me that this is what it is about, but since you are mainly asking how to draw the actual grid, I prefer to ask. –  ienissei Apr 7 '12 at 9:03
@ienissei I think that both tags are appropriate here. –  lockstep Apr 7 '12 at 9:52

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
\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
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
}
\special{color pop}
\vss % we want to have zero depth
}%
}

-
Thanks! I can get it to work with a minimal, but the actual document has ... something which makes the grid drift. Argghh, I can't figure out what it is. :-( –  morbusg Jan 15 '12 at 15:27
(oh I forgot to mention my \topskip is 0pt) –  morbusg Jan 15 '12 at 15:28
Where does the 2pt come from? –  morbusg Jan 15 '12 at 15:31
@morbusg Possibly the difference between \topskip and \baselineskip for a size of 10pt? –  lockstep Jan 15 '12 at 15:33
@morbusg If, by chance, the first line has no ascenders, its baseline will be higher, with \topskip=0pt. The height of the first line on the page is the maximum between the natural height of this line and the \topskip. For grid typesetting the \topskip has to be set at least to the maximum line height we are expecting. –  egreg Jan 15 '12 at 16:15

The eso-pic package lets you add material to each page. With TikZ you can draw a nice background grid.

\documentclass{article}

\usepackage{tikz}
\usepackage{eso-pic}

\begin{tikzpicture}[overlay,remember picture]
\draw[thick,red]
(current page.north east)
rectangle (current page.south west);
\draw[red!30!white,thin]
(current page.south west) grid[step=2mm]
(current page.north east);
\draw[red!30!white,dotted]
(current page.south west) grid[step=1cm]
(current page.north east);
\end{tikzpicture}%
}

\begin{document}

Hello world.
\clearpage
Hello moon.

\end{document}

-
That's a LaTeX solution, which might work with plain but then again might not. –  Joseph Wright Jan 15 '12 at 13:51
@Joseph Thanks. I didn't notice the XeTeX comment. I tried with XeLaTeX, which seems to work. –  Marc van Dongen Jan 15 '12 at 13:55
If you work with xetex it is more comfortable to use pstricks. –  Marco Daniel Jan 15 '12 at 14:53
@Marco Thanks. I don't use TeX/XeTeX really. I've been trying to google a solution for the equivalent of \AddToShipoutPicture but I couldn't find anyhting (google is so friendly' it automatically turns assumes you mean text when you write TeX and adding -text to the query usually doesn't help). –  Marc van Dongen Jan 15 '12 at 15:00
Thank you Marc for your input! And while, as Joseph commented, your answer might not be helpful for plain-tex users, it will certainly be helpful for LaTeX users! Thanks again! :-)` –  morbusg Jan 15 '12 at 20:00