# Adding space around section title for in-register in KOMA-script context

I saw recepts from answers on this question, and can I do this without titlesec, only with KOMA tools, f.e. via \RedeclareSectionCommand[afterskip=<amount>,beforeskip=<amount>]{section} ?
titlesec is fine, but it redefine some KOMA's features.

My MWE:

\documentclass{scrbook}

\usepackage{blindtext}
\usepackage{atbegshi,picture,xcolor}
%\usepackage{gridset}

% baseline grid
\newcommand{\printbaselines}{%
\AtBeginShipout{%
\AtBeginShipoutUpperLeft{%
\color{red}%
\put(\dimexpr 1in+\oddsidemargin,
{%
\vtop to\dimexpr\vsize+\baselineskip{
\hrule
}%
}%
}%
}%
}%

\printbaselines

\begin{document}

\chapter{First}
\section{Lorem I}
\Blindtext[1]
\section{Lorem II}
\Blindtext[1]
\section{Lorem III}
\Blindtext[1]

\end{document}


I try set

\RedeclareSectionCommand[
beforeskip=-2\baselineskip,
afterskip=1\baselineskip]{section}


but it work only for first paragraph (and without \chapter), - on second paragraph syncing is lost.

I try use \vskipnextgrid from gridset after \section but it not work with \chapter on some page with \section.

It is possible? (pdflatex)

• Shall the headings also be in-register? – TeXnician Oct 29 '17 at 18:29
• Hmm, may be enough some minimal (but fix?) distance between header (or last line if header is multiline) and text? (I try to imagine in my mind). – Elisey Oct 29 '17 at 18:38

What you want isn't any problem, if all headings are in \normalsize and the vertical spaces before and after headings are integer multiples of \baselineskip:

\documentclass[parskip=never]{scrbook}% never stretch the paragraph skips

\usepackage{blindtext}
\usepackage{atbegshi,picture,xcolor}

% baseline grid
\newcommand{\printbaselines}{%
\AtBeginShipout{%
\AtBeginShipoutUpperLeft{%
\color{red}%
\put(\dimexpr 1in+\ifodd\value{page}\oddsidemargin\else\evensidemargin\fi,
{%
\vtop to\dimexpr\vsize+\baselineskip{
\hrule
}%
}%
}%
}%
}%

\printbaselines

\setkomafont{chapter}{\normalsize}
\setkomafont{section}{\normalsize}
\RedeclareSectionCommand[beforeskip=2\baselineskip,afterskip=1\baselineskip,afterindent=false]{chapter}
\RedeclareSectionCommand[beforeskip=2\baselineskip,afterskip=\baselineskip,runin=false,afterindent=false]{section}

\begin{document}

\chapter{First}
\section{Lorem I}
\Blindtext[1]
\section{Lorem II}
\Blindtext[1]
\section{Lorem III}
\Blindtext[2]
\section{Lorm IV}
\Blindtext[1]

\end{document}


But if other font sizes of the headings should be allowed, extra effort is needed to make the height of each heading an integer multiple of the normal \baselineskip again. In this case you have to put each heading into a box, measure the height and depth of this box, change the height and depth if not a multiple of the normal \baselineskip and then print the changed box. This can be done by a redefinition of \chapterlinesformat and \sectionlinesformat, e.g.,

\documentclass[parskip=never]{scrbook}

\usepackage{blindtext}
\usepackage{atbegshi,picture,xcolor}

% baseline grid
\newcommand{\printbaselines}{%
\AtBeginShipout{%
\AtBeginShipoutUpperLeft{%
\color{red}%
\put(\dimexpr 1in+\ifodd\value{page}\oddsidemargin\else\evensidemargin\fi,
{%
\vtop to\dimexpr\vsize+\baselineskip{
\hrule
}%
}%
}%
}%
}%

\printbaselines

\newlength{\gridbaselineskip}
\newlength{\gridstrutheight}
\newlength{\gridstrutdepth}
\AtBeginDocument{%
\setlength{\gridbaselineskip}{\baselineskip}
\setlength{\gridstrutheight}{\ht\strutbox}
\setlength{\gridstrutdepth}{\dp\strutbox}
\RedeclareSectionCommand[beforeskip=2\gridbaselineskip,afterskip=1\gridbaselineskip,afterindent=false]{chapter}
\RedeclareSectionCommand[beforeskip=2\gridbaselineskip,afterskip=1\gridbaselineskip,runin=false,afterindent=false]{section}
}

\newcommand*{\usegridbox}[1]{%
% independent from the real height and depth use the height an depth of one grid-line
\ht#1\gridstrutheight
\dp#1\gridstrutdepth
\normalsize\usebox#1\par
}
\makeatletter
\renewcommand*{\chapterlinesformat}[3]{%
\sbox\@tempboxa{\@hangfrom{#2}{#3}}%
\usegridbox\@tempboxa
}
\renewcommand*{\sectionlinesformat}[4]{%
\sbox\@tempboxa{\@hangfrom{\hskip#2#3}{#4}}%
\usegridbox\@tempboxa
}
\makeatother

\begin{document}

\chapter{First}
\section{Lorem I}
\Blindtext[1]
\section{Lorem II}
\Blindtext[1]
\section{Lorem III}
\Blindtext[2]
\section{Lorm IV}
\Blindtext[1]

\end{document}


Note, here I've used a very simple change of the box size: setting the height and depth of the heading always to the height and depth of one grid line. If you have headings with multiple lines, you have to use a more intelligent approach with balancing the needed extra space before and after the heading. And then you also have to think about grid setting of the multiple lines of the heading. With the shown simple solution the one line of heading always is grid aligned.

• Yes, I have multiline headers often. My language have many long words :-) I'll try to find solution for this case. And thank you for good code!! M.b. it is time for including this feature into the koma-packet in next versions? :-) – Elisey Apr 15 at 15:40