2

I would like to format only the first chapter heading in my document differently using KOMA script. I want to change both the font and the presence of the chapter prefix. As apparently KOMA does not offer any options to restore fonts or options, this answer suggests enclosing the corresponding \chapter command into a group for that purpose. That approach works fine, if I want to change the second chapter heading.

\documentclass{scrreprt}
\usepackage{blindtext}
\KOMAoption{parskip}{false}
\begin{document}

\chapter{This}
\blindtext

\section{Foo}
\blindtext

\section{Bar}
\blindtext

\begingroup
\KOMAoption{chapterprefix}{true}
\setkomafont{chapter}{\centering\large\rmfamily}
\chapter{That}
\endgroup
\blindtext

\section{Foo}
\blindtext

\section{Bar}
\blindtext

\end{document}

However, if I actually change the formatting of the first chapter heading, then things go wrong.

\documentclass{scrreprt}
\usepackage{blindtext}
\KOMAoption{parskip}{false}
\begin{document}

\begingroup
\KOMAoption{chapterprefix}{true}
\setkomafont{chapter}{\centering\large\rmfamily}
\chapter{This}
\endgroup
\blindtext

\section{Foo}
\blindtext

\section{Bar}
\blindtext

\chapter{That}
\blindtext

\section{Foo}
\blindtext

\section{Bar}
\blindtext

\end{document}

When I compile the latter document, then the spacing in the first paragraph is broken in two ways. Firstly, the first line is indentet, which it should not. Secondly, the vertical spacing to the first section is much smaller than it should.

It appears that \chapter is setting up some stuff the first time it appears in the document, but these things get lost at \endgroup. Unfortunately, I have no clue what it might set up. I would be happy for suggestions on how to proceed.

ranguwud
  • 367

1 Answers1

2

You can patch \chapterformat and \chapterlineswithprefixformat:

\documentclass{scrreprt}
%\providecommand*\Ifstr{\ifstr}% needed up to and including KOMA-Script version 3.27, see https://komascript.de/faq_deprecatedif
\usepackage{blindtext}
%\KOMAoption{parskip}{false}% default

\newif\ifspecialchapter
\newcommand\specialchapter{%
  \specialchaptertrue%
  \KOMAoption{chapterprefix}{true}%
}
\newcommand\defaultchapter{%
  \specialchapterfalse%
  \KOMAoption{chapterprefix}{false}%
}
\usepackage{xpatch}
\xpretocmd\chapterlineswithprefixformat
  {\Ifstr{#1}{chapter}{\ifspecialchapter\large\rmfamily\fi}{}}%
  {}{\PatchFailedI}
\xpretocmd\chapterformat
  {\ifspecialchapter\centering\large\rmfamily\fi}
  {}{\PatchFailedII}

\begin{document}
\specialchapter
\chapter{That}
\defaultchapter
\blindtext

\section{Foo}
\blindtext
\section{Bar}
\blindtext

\chapter{This}
\blindtext
\section{Foo}
\blindtext
\section{Bar}
\blindtext
\end{document}

Result:

enter image description here

Or maybe you want

enter image description here

Then use

\documentclass{scrreprt}
%\providecommand*\Ifstr{\ifstr}% needed up to and including KOMA-Script version 3.27, see https://komascript.de/faq_deprecatedif
\usepackage{blindtext}
%\KOMAoption{parskip}{false}% default

\newif\ifspecialchapter
\newcommand\specialchapter{%
  \specialchaptertrue%
  \KOMAoption{chapterprefix}{true}%
}
\newcommand\defaultchapter{%
  \specialchapterfalse%
  \KOMAoption{chapterprefix}{false}%
}
\usepackage{xpatch}
\xpretocmd\chapterlineswithprefixformat
  {\Ifstr{#1}{chapter}{\ifspecialchapter\centering\large\rmfamily\fi}{}}%
  {}{\PatchFailedI}
\xpretocmd\chapterformat
  {\ifspecialchapter\large\rmfamily\fi}
  {}{\PatchFailedII}

\begin{document}
\specialchapter
\chapter{That}
\defaultchapter
\blindtext

\section{Foo}
\blindtext
\section{Bar}
\blindtext

\chapter{This}
\blindtext
\section{Foo}
\blindtext
\section{Bar}
\blindtext
\end{document}

Additional remark: Do not use \centering in the argument of \setkomafont.


You could also use the internal command \if@chapterprefix. But note, internal commands can change in the future.

\documentclass{scrreprt}
%\providecommand*\Ifstr{\ifstr}% needed up to and including KOMA-Script version 3.27, see https://komascript.de/faq_deprecatedif
\usepackage{blindtext}
%\KOMAoption{parskip}{false}% default

\newif\ifspecialchapter
\newif\ifdefaultchapterprefix
\makeatletter
\newcommand\specialchapter{%
  \specialchaptertrue%
  \if@chapterprefix\defaultchapterprefixtrue\else\defaultchapterprefixfalse\fi%
  \KOMAoption{chapterprefix}{true}%
}
\newcommand\defaultchapter{%
  \ifspecialchapter%
    \ifdefaultchapterprefix\@chapterprefixtrue\else\@chapterprefixfalse\fi%
  \fi%
  \specialchapterfalse%
}
\makeatother
\usepackage{xpatch}
\xpretocmd\chapterlineswithprefixformat
  {\Ifstr{#1}{chapter}{\ifspecialchapter\centering\large\rmfamily\fi}{}}%
  {}{\PatchFailedI}
\xpretocmd\chapterformat
  {\ifspecialchapter\large\rmfamily\fi}
  {}{\PatchFailedII}

\begin{document}
\specialchapter
\chapter{That}
\defaultchapter
\blindtext

\section{Foo}
\blindtext
\section{Bar}
\blindtext

\chapter{This}
\blindtext
\section{Foo}
\blindtext
\section{Bar}
\blindtext
\end{document}

The result is the same as for the second example.

esdd
  • 85,675
  • Great idea to introduce a new boolean and to make all formatting dependent on its value. This does, however, not work for the \KOMAoption command. Your \defaultchapter always sets chapterprefix to false. So, if I ever decide to have chapterprefix=true as document default, the first call of \defaultchapter will break that setup for the rest of the document. My actual commands for the special chapter are much more complex and it's easy to lose track of what has been changed, so it would be great to also keep the KOMAoption setting local. Any idea, how to achieve that? – ranguwud Oct 23 '19 at 05:27
  • I have added an example without \KOMAoption in \defaultchapter. But maybe you should show what you really want to do for the first chapter title (in a new question). It could be better to declare an own sectioning command for this chapter. – esdd Oct 23 '19 at 07:37
  • I added the full code now here – ranguwud Oct 23 '19 at 09:59
  • Although this answer does not really solve my problem, I am marking as solved, since it addresses the specific problem in the MWE. A more general answer can be found in the linked question. – ranguwud Oct 23 '19 at 18:39