# Spacing of sectioning commands for grid typesetting

I know questions about grid typesetting have been asked before, and some packages (grid, gridpos) exist. The scenario I have at the moment is simpler than that, and is mostly a question about elastic lengths. I'm not claiming the result is excellent typography, but I'm curious whether and how it could be done :)

In typesetting my thesis, I have a page where \textheight is an exact multiple of \baselineskip, so I can fit exactly 34 lines of text into the page. The section and subsection commands are defined by the style file as

\renewcommand\section{\@startsection {section}{1}{\z@}%
{-3.5ex \@plus -1ex \@minus -.2ex}%
{2.3ex \@plus.2ex}%
{\def\baselinestretch{1.0}\reset@font\normalsize\bfseries\slshape}}
\renewcommand\subsection{\@startsection{subsection}{2}{\z@}%
{-3.25ex\@plus -1ex \@minus -.2ex}%
{1.5ex \@plus .2ex}%
{\reset@font\normalsize\mdseries\itshape}}


(Which as far as I can tell have been copied and pasted from elsewhere on the web without any particular rhyme or reason. Why should the space around section headings have anything to do with the x-height of the font?)

On pages that have 0 or 2+ section headings, there is enough stretchability around the headings to account for their non-integral-multiple-of-\baselineskip heights. On pages that have 1 section heading in the middle of the page, there is again enough stretchability before and after the heading. But on pages where the section heading is the first thing in the page, the before-heading space is elided, along with its stretchability, and I get underfull \vboxes.

My question is, is there a way to change the spaces in these definitions so that when a section heading starts a page, it skips a full line, but when it is in the middle of the page, it uses these smaller stretchabilities? A manual solution is to insert \vspace*{0pt plus 0.5\baselineskip} at each problematic section heading, which lets TeX "round up" to the next nearest line, but if I simply insert that spacing everywhere, then TeX will use that stretch even when it doesn't need to... I tried looking at the internal definitions of \@startsection and \@ssect, but I don't see any way for them to detect when the before-space is elided from the page.

-

As you have noted, ex (and em) lengths are relative to the currently used fonts. The reason for this depends on the application. In your instance it makes sense using a font size-specific length, since the typesetting the sectional heading may be different depending on whether the document is processed using 10pt, 11pt or 12pt (or, for that matter, any other font size) as \normalfont. For example, using absolute lengths (like pt, bp or pc, for example) may cause the gap between section headings and text to seem too small, or too large, if the font size is changed. The example below a font-based skip of 1em between a pseudo-section heading is replaced by a fixed-width 11.5pt skip (in 10pt, 1em or roughly 11.5pt, see the TeX Book, p 60):

\documentclass{article}
\begin{document}
\bfseries% For bold font

{\normalfont Font-based skip of \verb!1em!} \par \medskip

\newcommand{\myskip}{\unskip\rule[0.5ex]{1em}{1pt}\ignorespaces}
{\normalsize 1 \myskip A section \par}%
{\large 1 \myskip A section \par}%
{\Large 1 \myskip A section \par}%
{\LARGE 1 \myskip A section \par}%
{\Huge 1 \myskip A section \par}%

\bigskip \hrulefill \bigskip

{\normalfont Fixed-width skip of \verb!11.5pt!} \par \medskip

\renewcommand{\myskip}{\unskip\rule[0.5ex]{11.5pt}{1pt}\ignorespaces}
{\normalsize 1 \myskip A section \par}%
{\large 1 \myskip A section \par}%
{\Large 1 \myskip A section \par}%
{\LARGE 1 \myskip A section \par}%
{\Huge 1 \myskip A section \par}%
\end{document}


Since I am not entirely sure of your sectional intent (that almost came out wrong), here's a suggestion when it comes to page-breaking woes. The needspace package provides \needspace{<len>} that issues a \break if there is less than <len> space available on the page. And, the code is not very complicated. Here is a modified version, now taking 3 arguments for the sake of this discussion:

\makeatletter
% \needspace{<len>}{<NOT enough space>}{<enough space>}
\newcommand{\needspace}[3]{\par \penalty-100\begingroup
\setlength{\dimen@}{#1}%
\ifdim \dimen@>\dimen@ii% execute the following if there IS NOT enough space on page
\ifdim \dimen@ii>\z@
\vfil
\fi
\break
#2% <NOT enough space>
\else% execute the following if there IS enough space on page
#3% <enough space>
\fi\endgroup}
\makeatother


It would be possible to redefine \section so as to use the above \needspace and condition on the type of \@startsection that should be called.

-
I completely agree that using a relative length makes sense; it just seems like using 3.5ex has too much of a magic-number feel, as opposed to just using the \baselineskip which tracks the font size directly... Using the \needspace command sounds pretty close to what I want, but LaTeX tries to keep a few lines of text after section commands before a page-break, right? So I wouldn't quite know what length to need, here. Also, does \needspace work with elastic lengths? Still, this is a pretty promising approach. –  Ben Lerner Nov 3 '11 at 14:33

Have a look at ConTeXt, it has a grid option or try the experimental package from KOMA-Script http://www.komascript.de/gridset

-
\vspace{0pt plus -0.5\baselineskip}

This way, at the beginning of the page the first \vspace disappears and you get the second one, but everywhere else they cancel each other.
Hmm, interesting, but not quite what I had in mind, in two ways: I can't put the vspaces directly into the call to \@startsection, but I could put them into the definitions of \section and \subsection before the call to @startsection. But more importantly, I wanted the text of the section heading to be flush at the top of the page, and extra space beneath it; I think your solution leaves extra space above the text. But a variant on your approach might work, using raisebox to push the section head up? Is there a cleaner way? –  Ben Lerner Jul 26 '11 at 5:06
1. I'd get rid of \@startsection and would did it myself (mostly copying and pasting from source2e; this would give you more freedom. 2. Then why don't you do \vspace{0pt plus -0.5\baselineskip} <section title, whatever> \vspace{0pt plus 0.5\baselineskip} or something similar? –  mbork Jul 27 '11 at 11:29