8

Regarding the following two examples my question is: Is it a bug or is it a feature (or do I miss something)?

\documentclass{scrbook}
\usepackage{xcolor}
% \addtokomafont{sectioning}{\color{green}}
 \addtokomafont{chapter}{\color{green}}
 \addtokomafont{section}{\color{green}}
 \addtokomafont{subsection}{\color{green}}
 \addtokomafont{subsubsection}{\color{green}}

\setlength{\textheight}{13\baselineskip}
\begin{document}
\chapter{test}

AAA

\section{test}

\subsection{subtest}

AAA
\end{document}

Please compare the results [sorry, I can not upload images] of the examples.

\documentclass{scrbook}
\usepackage{xcolor}
 \addtokomafont{sectioning}{\color{green}}
% \addtokomafont{chapter}{\color{green}}
% \addtokomafont{section}{\color{green}}
% \addtokomafont{subsection}{\color{green}}
% \addtokomafont{subsubsection}{\color{green}}

\setlength{\textheight}{13\baselineskip}
\begin{document}
\chapter{test}

AAA

\section{test}

\subsection{subtest}

AAA
\end{document}

The former one shows a page break between \section and \subsection.

Jürgen
  • 2,160
  • 2
    Unexpected, but not weird: you are issuing \color in vertical mode, which is always dangerous. The two examples differ in when the color change is performed. – egreg Mar 17 '17 at 07:21
  • Ok, but this is the procedure which is recommended. And on the first sight I would not expect any differences between \addtokomafont{sectioning} and \addtokomafont{section} and so on. – Jürgen Mar 17 '17 at 07:30
  • \leavevmode We already have a similar question. – Johannes_B Mar 17 '17 at 07:42
  • 3
    Which KOMA-Script Version do you use? With version 3.21 or 3.22 (current on CTAN and in the distributions) I do not get a page break between between \section and \subsection. But with version 3.20 I can reproduce the described behavior. See also http://tex.stackexchange.com/q/314235/43317 and http://www.komascript.de/release3.20 (German). – esdd Mar 17 '17 at 07:48
  • Many thanks, that's it! Would you like to put this into an answer or should we close this one with reference to 43317 or should I post the answer or ...? – Jürgen Mar 17 '17 at 08:04
  • BTW: Yes, I am still on Version 3.20. Perhaps I should update. – Jürgen Mar 17 '17 at 08:10

1 Answers1

6

With an uptodate KOMA-Script (at least version 3.21, current is 3.22) I do not get a page break between \section and \subsection.

Even if I use

\addtokomafont{section}{\color{blue}}
\addtokomafont{subsection}{\color{red}}

the result is

enter image description here

So you have to update your KOMA-Script version.


Workaround for version 3.20 or older:

You can add \nobreak after the \color command:

\documentclass{scrbook}
\usepackage{xcolor}
\addtokomafont{sectioning}{\color{green}\nobreak}
\addtokomafont{section}{\color{blue}\nobreak}
\addtokomafont{subsection}{\color{red}\nobreak}

\setlength{\textheight}{13\baselineskip}
\begin{document}
\chapter{test}
\KOMAScriptVersion
\section{test}
\subsection{subtest}
AAA
\end{document}

or \leavevmode before \color (see also this question):

\documentclass{scrbook}
\usepackage{xcolor}
\addtokomafont{sectioning}{\leavevmode\color{green}}% see https://tex.stackexchange.com/a/314243/43317
\addtokomafont{section}{\leavevmode\color{blue}}
\addtokomafont{subsection}{\leavevmode\color{red}}

\setlength{\textheight}{13\baselineskip}
\begin{document}
\chapter{test}
\KOMAScriptVersion
\section{test}
\subsection{subtest}
AAA
\end{document}

Both results in

enter image description here

esdd
  • 85,675