# Grid typesetting

Question

Is there any way to manage that text stays on the grid given by a tikzpicture?

Screenshot

MWE

\documentclass{article}

\usepackage{tikz}

\usepackage{eso-pic}

\begin{tikzpicture}[remember picture, overlay]

\tikzset{normal lines/.style={black!20, very thin}}
\tikzset{margin lines/.style={black!20, thick}}

\node at (current page.south west){
\begin{tikzpicture}[remember picture, overlay]

\draw[style=normal lines,step=0.5cm] (0,0) grid +(210mm,297mm);
\end{tikzpicture}
};
\end{tikzpicture}
}

\usepackage{blindtext}

\begin{document}

\section{Lorem ipsum}
\blindtext[2]

\section{Lorem ipsum}
\blindtext[3]

\section{Lorem ipsum}
\blindtext[2]

\end{document}

• Not really, since you have a variety of box heights when looking at the horizontal lines as boxes that fill up the text block. For example, the \baselineskip of the sections (and surrounding spaces) are not the same as that of the paragraph text. – Werner Dec 8 '17 at 17:32
• I guess you'll have to ensure that the content conforms to a "\baselineskip interval." If you have only regular/paragraph text, this should be okay, but as soon as you add sectional units and floats, then things might not line up as expected. Two alternatives are possible: (1) Use LuaLaTeX to "process the input buffer" before content is set on the page; (2) Change this method to put in horizontal rules rather than line numbers after the document is already created. – Werner Dec 8 '17 at 17:42
• – John Kormylo Dec 8 '17 at 17:51
• If off-grid is ONLY caused by things such as section headings, try:: (1) Do not use automated sections. Instead, manually create them using (styled) text. (2) Enclose section headings in \smash{} which nullifies increased line height due to larger text. The enclosed text will not wrap, but you can break it into several lines, each with their own \smash{}. (3) Use \null before and after the section heading, to space an integer number of lines. (4) If your work is more like a novel than like an academic report, the novel document class has grid typesetting built in. Cannot use TiKz. – user139954 Dec 8 '17 at 18:04
• @RobtAll not always possible but a basic premise of latex is that as far as possible you separate the document markup (sections marked with \section) from details of layout implementation, so even if you define all the macros "by hand" it is best to call the resulting macro \section and keep the resulting document markup basically unchanged. – David Carlisle Dec 8 '17 at 22:42

It's not entirely trivial to get it to work for all input as TeX really wants to use stretchy glue to align things and that will always take you off grid, but basically you just need to ensure that any spaces added are multiples of your grid unit (I took .5cm here) so in this case I forced baselineskip, topskip, and the space before and after section headings to fit this grid

\documentclass{article}
\usepackage{tikz}
\usepackage{eso-pic}
\begin{tikzpicture}
[
remember picture,
overlay,
normal lines/.style={black!20, very thin},
]
\draw [style=normal lines, step=0.5cm]  (current page.south west) grid (current page.north east);
\end{tikzpicture}%
}
\usepackage{blindtext}
\makeatletter
\renewcommand\section{\@startsection {section}{1}{\z@}%
{-1cm}%
{1cm}%
{\normalfont\Large\baselineskip.5cm\relax\bfseries}}
\makeatother
\raggedbottom

\begin{document}

\lineskip0pt
\baselineskip.5cm
\normalbaselineskip=\baselineskip
\topskip\baselineskip

\section{Lorem ipsum}
\blindtext[2]

\section{Lorem ipsum}
\blindtext[3]

\section{Lorem ipsum}
\blindtext[2]

\end{document}

• tikzpicture environments ought not be nested. Doing so is unsupported. It may happen to work or it may not. But, sooner or later, it will probably break in mysterious ways. – cfr Dec 9 '17 at 3:07
• overlay inside overlay seems especially strange. – cfr Dec 9 '17 at 3:08
• Roll back if you object to my edit. (I only changed the TikZ code to be passably sane.) – cfr Dec 9 '17 at 3:19
• @cfr I never realised that it was possible to have passably sane tikz code:-) – David Carlisle Dec 9 '17 at 8:49
• Everything's relative. Except what is absolute. – cfr Dec 9 '17 at 16:35