5

My publisher asked me to work with a grid because the book will have 1000 pages and very thin paper, so that the text from the next page won't shine through making the page all grey. I read about the difficulties with TeX and grids. (I can not change to ConTeXt). I am stuck with spacing the chapter and section commands which should fall in line with the grid.

There are two problems, of which this question addresses one:

  1. When \addchap and \section follow each other the grid is not kept any more

  2. Sometimes the section title is longer than a single line. (Possible follow up, but I would be glad if the other thing worked)

When I use this MWE, the grid is intact after \addchap and \section:

\documentclass[fontsize=10pt,headings=openany]{scrbook}
\usepackage[usenames]{xcolor}
\usepackage{scrlayer-scrpage}
\usepackage{fontspec}
\usepackage{blindtext}
\usepackage{eso-pic}
\usepackage[ngerman]{babel}
\usepackage[babel]{microtype}
\usepackage{tikz}

% baseline grid
\AddToShipoutPicture{%
\AtTextLowerLeft{
    \begin{tikzpicture}[overlay,remember picture]%
        \draw[blue!90!white,very thin]%
             (current page.south west) grid[step=\baselineskip]%
             (current page.north east);%
    \end{tikzpicture}%
    }%
}

\RedeclareSectionCommand[
  beforeskip=22.89pt,
  afterskip=12pt]{chapter}
\RedeclareSectionCommand[
  beforeskip=17.99pt,
  afterskip=12pt]{section}


\begin{document}
\addchap{1 EL}
1EL\blindtext

\section{1 EL}
1EL\blindtext

\addchap{1 EL}
\section{1 EL}

1EL\blindtext

\blindtext

\end{document}

Unfortunately it is off whenever \addchap and \section are adjacent. (For unknown reasons the image uploader does not accept my screenshot. I'll try to add the image later.) Which length is in charge of the space between chapter and section?

9
  • Only the afterskip of the chapter headings are considered in the adjacent case.
    – Skillmon
    Commented Dec 19, 2017 at 9:13
  • Ah. This explains! So when I change the afterskip of \addchap to the same value as the beforeskip of \section then everything falls in line. Commented Dec 19, 2017 at 9:20
  • Not really. You have to consider both cases. If you just do that it won't work out in the other cases where text follows the chapter's title.
    – Skillmon
    Commented Dec 19, 2017 at 9:22
  • That is true. But fortunately in my case I always have sections after chapters Commented Dec 19, 2017 at 9:23
  • For the second part of this question: Do you want the second line of long section titles be on the grid, too, or just the following text?
    – Skillmon
    Commented Dec 19, 2017 at 9:33

1 Answer 1

2

You could create a command which handles the cases where the section title follows immediately after the chapter title. I called it \adjacent and it takes the following arguments:

\adjacent[<short chap>]{<chap>}[<short sec>]{<sec>}

This should fix your first issue (but as noted in the comments isn't necessary as they always are adjacent.

For your second issue I redefined the \section command. It now checks the length of the section title and if it would be longer than one \textwidth it changes the spacing after the title.

The following is the complete MWE

\documentclass[fontsize=10pt,headings=openany]{scrbook}
\usepackage[usenames]{xcolor}
\usepackage{scrlayer-scrpage}
%\usepackage{fontspec}
\usepackage{blindtext}
\usepackage{eso-pic}
\usepackage[ngerman]{babel}
\usepackage[babel]{microtype}
\usepackage{tikz}

% baseline grid
\AddToShipoutPicture{%
\AtTextLowerLeft{
    \begin{tikzpicture}[overlay,remember picture]%
        \draw[blue!90!white,very thin]%
             (current page.south west) grid[step=\baselineskip]%
             (current page.north east);%
    \end{tikzpicture}%
    }%
}

\RedeclareSectionCommand[
  beforeskip=22.89pt,
  afterskip=12pt
]{chapter}
\RedeclareSectionCommand[
  beforeskip=17.99pt,
  afterskip=12pt
]{section}

\newcommand\adjacent[2][]{%
  \bgroup
  \RedeclareSectionCommand[
    beforeskip=22.89pt,
    afterskip=17.99pt,
  ]{chapter}%
  \if\relax\detokenize{#1}\relax
    \addchap{#2}%
  \else
    \addchap[#1]{#2}%
  \fi
  \egroup
  \section
}

\let\sectionBAK\section
\newcounter{sectiontmp}
\renewcommand\section[2][]{%
  \bgroup
  \setcounter{sectiontmp}{\numexpr\value{section}+1\relax}%
  \setbox0\hbox{%
    \usekomafont{section}\sectionlinesformat{1}{0pt}{\thesectiontmp}{#2}}%
  \expandafter\ifodd\numexpr\wd0/\textwidth\relax
    \RedeclareSectionCommand[
      beforeskip=17.99pt,
      afterskip=17.99pt
    ]{section}%
  \fi
  \if\relax\detokenize{#1}\relax
    \sectionBAK{#2}%
  \else
    \sectionBAK[#1]{#2}%
  \fi
  \egroup
}

\begin{document}
\tableofcontents
\addchap{1 EL}
1EL\blindtext

\section{1 EL}
1EL\blindtext

\adjacent{1 EL}{1ELFoo bar bazg 1ELFoo bar baz 1ELFoo bar baz 1ELFoo bar baz 1EL}
1EL\blindtext

%\addchap{1 EL}
%\section{1 EL}


%1EL\blindtext

\blindtext

\section{1EL}%Foo bar bazg 1ELFoo bar baz 1ELFoo bar baz 1ELFoo bar baz 1E}%LFoo bar
%  baz 1EL}
1EL\blindtext

\end{document}

EDIT: I changed the \section command so it does work with an arbitrary amount of lines taken. There is a possibility to fail though, as the hanging indent of the section titles isn't considered for estimating the number of lines required.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .