[Tex/LaTex] Spacing of sectioning commands for grid typesetting

grid-typesettingline-spacingsectioningtypography

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.

Best Answer

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):

enter image description here

\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}%
  \dimen@ii\pagegoal \advance\dimen@ii-\pagetotal
  \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.

Related Question