# How to typeset a vbox to a grid in plain TeX?

NOTE: update below

I know this is a heavily debated subject, which is almost impossible in plain TeX, but what I want is merely a vbox with a height that's a multiple of baselineskip. Searching the net, I found the code included in the following minimal working example (background grid taken from this answer):

\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

\font\rm=cmr17
\baselineskip=20pt
\rm
}

\dimen0=\ht0%                     \dimen0 = 11.80556pt
\divide\dimen0 by\baselineskip%   \dimen0 = 0.00002pt
\multiply\dimen0 by\baselineskip% \dimen0 = 12.0pt
\vbox to\dimen0{\vfil\box0}
\nobreak\vskip2\baselineskip\nobreak
\noindent
}

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

\bye


In head I create a vbox, then its height is calculated, baselineskip is added, the total is divided by baselineskip and then multiplied again, so I get a height rounded to baselineskip. After each operation I show the value of dimen0 using \showthe and added those in comments after each line.

The first thing that stroke me is that after the multiplication dimen0 is 12pt, not 24pt, as I would expect. However, this is easily circumvented by adding another baselineskip.

The second thing I wonder is that the output still doesn't register, i.e. the second block is clearly not in the same position relative to the grid as the first one. Why is this?

And why is grid-setting still so difficult in plain TeX? There's the grid package in LaTeX for years, now, and as LaTeX is merely a macro language above TeX, I don't understand why those features aren't converted to plain TeX, too.

For the record, I'm using XeTeX 3.14159265-2.6-0.99991 from TeX Live 2014.

UPDATE: after applying suggestions in wipet's answer I now have this example with a chapter name spanning multiple lines:

\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\topskip=10pt\relax
\def\pagebody{\vbox to\vsize{\boxmaxdepth=\maxdepth \grid\pagecontents}}

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

\font\rm=cmr17
\baselineskip=20pt
\rm\setbox\strutbox=\hbox{\vrule height.7\baselineskip depth.3\baselineskip width0pt}
}

\setbox\strutbox=\hbox{\vrule height.7\baselineskip depth.3\baselineskip width0pt}

\dimen0=\ht0%                     \dimen0 = 11.80556pt
\divide\dimen0 by\baselineskip%   \dimen0 = 0.00002pt
\multiply\dimen0 by\baselineskip% \dimen0 = 12.0pt
\vskip\dimen0
\ht0=0pt \dp0=0pt \box0
\nobreak\vskip2\baselineskip\nobreak
\noindent
}

\def\1{\head Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec a diam lectus.
Sed sit amet ipsum mauris

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

\bye


However, this doesn't produce the output I want:

as you can see, the \null (an empty \hbox) creates an empty line, which is not what I want at the top of a page (the \vskip doesn't have this side effect). Leaving out the \null also doesn't give the desired result, as can be seen here:

Now the chapter title ‘walks’ into the top margin.

Adding a negative \vskip solves the extra gap at the top of the page, but leaves too little space between the body text and a chapter(/section) title in the middle of a page.

• "I know this is a heavily debated subject, which is almost impossible in plain TeX..." Why do think so? Plain TeX is a macro collection built on top of TeX just like LaTeX is. IIRC, the grid package has a fair amount of issues. IMHO, when going for (baseline-)grid, Plain should be way easier than LaTeX. Anyway, things that spring to mind are that your \topskip shouldn't have glue, and that \vboxes need \struts at start and end, and they discard \prevdepth when \unvboxing (if that's relevant here). – morbusg Oct 8 '14 at 17:12
• Oh, and \strut needs to be redefined (maybe best is like in LaTeX, i.e., height of .7\baselineskip and depth of .3\baselineskip.) – morbusg Oct 8 '14 at 17:16
• By the way, if you just want a \vbox of a certain multiple of \baselineskip, there is a really simple command for that: \vbox to 5\baselineskip{...} (naturally change the number 5 to anything you like). – morbusg Oct 8 '14 at 17:27
• @morbusg Of course, that's actually what I do in \vbox to\dimen0{…}, it's just that I don't know exactly what the height of the original vbox is, as the head (or picture, etc.) might span multiple lines. Even if I change that line to an exact height, e.g. \vbox to 2\baselineskip{…}, the text clearly doesn't register. – Marcel Korpel Oct 8 '14 at 17:35
• Ah, yes, sorry I missed that. On a more general note, plain.tex has many things "hard coded" to use 10/12 (that is, ten point font with twelve point leading), so in order to have consistent baseline grid, one would need to go through every instance of those. Some examples include the definition of \makeheadline/-footline used in the output routine, \ragged-/normalbottom, and various things in the math mode, just to name a few. – morbusg Oct 8 '14 at 19:01

I recommend to insert the box with zero height and depth to the grid instead of the vbox of calculated width. Your solution (\vbox to\dimen0) breaks the intelineskip calculation by \baselineskip because lineskip plus lineskiplimit plays the role here. This is a reason why your solution doesn't keep the grid.

 \def\head#1 \par{\null\nobreak\vskip\baselineskip
\dimen0=\ht0%                     \dimen0 = 11.80556pt
\divide\dimen0 by\baselineskip%   \dimen0 = 0.00002pt
\multiply\dimen0 by\baselineskip% \dimen0 = 12.0pt
\vskip\dimen0
\ht0=0pt \dp0=0pt \box0
\nobreak\vskip2\baselineskip
\noindent
}


Note, that the following material is in the vertical list:

\null ...    this iserts one line. If this material is on the top of the page then
this \null is placed on the first baseline.
\nobreak ... the following skips are protected
\vskip n\baselineskip  ... (as your wish)
\vskip\dimen0          ... the skip calculated from original width of the box
rounded to the multiply of \baselineskip
\box with height and depth set to zero
\nobreak
\vskip k\baselineskip  ... (as your wish)


...And why is grid-setting still so difficult in plain TeX?'' IMHO, grid setting is simple in plainTeX. But you have to set many vskip-like registers (\abovedisplayskip, \parskip etc.) to the appropriate value.

• Almost there, though now there's an extra gap inserted at the top of the page, which is not what I want. Please have a look at the update in my question. – Marcel Korpel Oct 8 '14 at 19:40
• Use \vskip\baselineskip\null\nobreak\vskip-\baselineskip.... The first skip will discard at the top of the page. – wipet Oct 8 '14 at 19:47