# Setting titles to be a multiple of \baselineskip fo grid typesetting

I want to typeset my document on grid, so I use the following commands for titles:

\titleformat{\section}
{\fontsize{20}{2\baselineskip}\selectfont\bfseries\sffamily}{}{0em}{#1}
\titlespacing*{\section}{0pt}{0pt}{0pt}


But something fails

How can I fix this?

Full MWE:

\documentclass[10pt,twocolumn]{book}
\usepackage{calc}
\usepackage{geometry}
\usepackage{ragged2e}
\usepackage[explicit]{titlesec}
\usepackage{lipsum}

\usepackage{fontspec}
\setmainfont{Cambria}
\setsansfont{Calibri}

\pagestyle{empty}

\geometry{showframe}

\setlength{\parskip}{0pt}

\titleformat{\section}
{\fontsize{20}{2\baselineskip}\selectfont\bfseries\sffamily}{}{0em}{#1}
\titlespacing*{\section}{0pt}{0pt}{0pt}

\begin{document}

\raggedbottom\RaggedRight

\section{First section}

\lipsum

\end{document}


The macro \inspace takes something that normally doesn't fit into an fixed number of lines and overlays it over a fixed number of lines. You can specify the number of lines, or it can compute them based on the size of the box. The options t/c/b align the tops/centers/bottoms of the box and the reserved space. (Anything except t and b is treated as c.)

It should be noted that the first \section was much larger than the second.

\documentclass[10pt,twocolumn]{book}
\usepackage{calc}
\usepackage{geometry}
%\usepackage{ragged2e}
\usepackage[explicit]{titlesec}
\usepackage{lipsum}

\usepackage{fontspec}
\setmainfont{Cambria}
\setsansfont{Calibri}

\usepackage{everypage}
\usepackage{tikzpagenodes}
\begin{tikzpicture}[overlay,remember picture]
\draw[color=red,very thin]
(current page text area.south west) grid[step=\normalbaselineskip]
(current page text area.north east);
\end{tikzpicture}%
}

\pagestyle{empty}

\geometry{showframe}

\setlength{\parskip}{0pt}

\newcommand{\inspace}[3][c]% #1 = t/c/b (optional), #2 = number of lines (0=compute), #3 = contents
{\bgroup
\setbox0=\vbox{#3}%
\count1=#2\relax
\def\test{#1}%
\ifnum\count1<1
\dimen0=\dimexpr \ht0+\dp0\relax
\count1=\numexpr \dimen0/\baselineskip\relax
\fi
\dimen0=\baselineskip
\multiply\dimen0 by \count1
\ifvmode
\noindent
\let\terminate=\par
\else
\newline
\let\terminate=\newline
\fi
\def\opt{t}% align to top
\ifx\test\opt\relax
\strut\rlap{\raisebox{\dimexpr \ht\strutbox-\ht0}[0pt][0pt]{\usebox0}}\terminate
\else
\def\opt{b}% align to bottom
\ifx\test\opt\relax
\strut\rlap{\raisebox{\dimexpr \ht\strutbox+\dp0-\dimen0}[0pt][0pt]{\usebox0}}\terminate
\else% anything else is centered
\strut\rlap{\raisebox{\dimexpr \ht\strutbox-0.5\ht0+0.5\dp0-0.5\dimen0}[0pt][0pt]{\usebox0}}\terminate
\fi
\fi
\loop\ifnum\count1>1
\strut\terminate
\repeat
\egroup}

\begin{document}

\raggedbottom%\raggedright

\inspace[b]{3}{\section{First section}}

\lipsum[1-4]

\inspace[b]{3}{\section{Second section}}

\lipsum[5-7]

\end{document}

• Unfortunately, this doesn't work for me. If you add \usepackage{eso-pic} \usepackage{xcolor} \usepackage{tikz} \AddToShipoutPicture{% \AtTextLowerLeft{ \begin{tikzpicture}[overlay,remember picture]% \draw[red!10!white,very thin]% (current page.south west) grid[step=\baselineskip]% (current page.north east);% \end{tikzpicture}% }% } to see the grid, you can notice a little difference Nov 17 '17 at 16:23
• I forgot about \topskip. Also, when the section is NOT at the top of the page is adds additional space. Nov 17 '17 at 18:36
• Still not working. I also want something automated, without the need to add \strut every time. Nov 17 '17 at 20:06
• It occurred to me that instead of trying to make a box act like several lines of text, one could OVERLAY a box over several lines of (invisible) text. Nov 18 '17 at 1:34
• what if the section heading requires more than one line? (not tested.) Nov 20 '17 at 2:20