4

Question

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

Screenshot

enter image description here

MWE

\documentclass{article}

\usepackage{tikz}

\usepackage{eso-pic}

\AddToShipoutPicture{%
  \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}
CarLaTeX
  • 62,716
  • 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 08 '17 at 17:32
  • So the only solution is to MANUALLY shift headings, adapt spacing of paragaphs etc.? – Andreas Schneider Dec 08 '17 at 17:34
  • 1
    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 08 '17 at 17:42
  • 2
    See https://tex.stackexchange.com/questions/401778/setting-titles-to-be-a-multiple-of-baselineskip-fo-grid-typesetting – John Kormylo Dec 08 '17 at 17:51
  • 2
    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. –  Dec 08 '17 at 18:04
  • @RobtAll why not just set the space used by the standard headings rather that use manually styled text? – David Carlisle Dec 08 '17 at 22:07
  • @DavidCarlisle Certainly that works. But I tend to favor brute force approaches that can be used under other circumstances. I can type and count to ten, but I can only count to five while typing, due to limited fingers. –  Dec 08 '17 at 22:36
  • 3
    @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 08 '17 at 22:42

1 Answers1

10

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

enter image description here

\documentclass{article}
\usepackage{tikz}
\usepackage{eso-pic}
\AddToShipoutPicture{%
  \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}
cfr
  • 198,882
David Carlisle
  • 757,742