This is a follow-up to my previous question with more details. In my document there are some chapters with a number and some chapters without a number (like introduction or acknowledgements). I have set up a command \fancychapter
to style the chapters with number differently.
My problem is that I modify different things like KOMA options, fonts or styling commands, so I want to use scoping to go back to the document default settings after any \fancychapter
command. If, however, the first chapter in the document is within a different scope, this messes up spacing in the document. All this is described with a MWE in the linked question. Here, I am posting my full code as requested in a comment to this answer.
\documentclass{scrreprt}
\usepackage{calc}
\usepackage{xcolor}
\usepackage{xparse}
\usepackage{graphicx}
\usepackage{blindtext}
\KOMAoption{parskip}{false}
\makeatletter
\definecolor{chapmark}{RGB}{148,148,148}
\newcommand*\numberscale{}
\newlength{\chapter@mark@width}
% https://tex.stackexchange.com/a/406025
\gdef\gsetlength#1#2{%
\begingroup
\setlength\skip@{#2}% Local assignment to a scratch register.
\global#1=\skip@ % Global assignement to #1;
% \relax is not necessary because of the following \endgroup.
\endgroup % \skip@ is restored by end of group.
}
\newcommand*{\chapter@mark}{%
\scalebox{\numberscale}{\usekomafont{chapterprefix}\thechapter}\quad%
}
\let\chapterformat@original\chapterformat
\newcommand*{\chapterformat@decorated}{%
\begin{minipage}[b]{\widthof{\chapter@mark}}
\chapter@mark
\end{minipage}%
}
% Add line below chapter title
\let\chapterheadendvskip@original\chapterheadendvskip
\newcommand*{\chapterheadendvskip@decorated}{%
{%
\setlength{\parskip}{0pt}%
\setlength{\parindent}{0pt}%
%\setlength{\chapter@mark@width}{\widthof{\chapter@mark}}%
\par\noindent\vskip-\baselineskip%
\makebox[0pt][l]{\raisebox{-1.2\baselineskip}[0pt][0pt]{%
\hspace{\chapter@mark@width}\hspace{-1ex}%
\rule[0pt]{\linewidth-\chapter@mark@width+1ex}{1pt}}}\par%
\vskip\baselineskip
}%
\chapterheadendvskip@original%
}
\NewDocumentCommand\fancychapter{ o m }{%
\begingroup
\KOMAoption{chapterprefix}{false}
\addtokomafont{chapterprefix}{\normalfont\fontfamily{LinuxBiolinumT-TLF}\selectfont}
\renewcommand*\numberscale{5}
\addtokomafont{chapterprefix}{\color{chapmark}}
\let\chapterformat\chapterformat@decorated%
\let\chapterheadendvskip\chapterheadendvskip@decorated%
%
\IfValueTF{#1}%
{% with first optional argument:
\chapter[#1]{%
\gsetlength{\chapter@mark@width}{\widthof{\chapter@mark}}%
\begin{minipage}[b]{\linewidth-\chapter@mark@width}
#2\hbadness=10000
\end{minipage}}%
}{% without first optional argument:
\chapter[#2]{%
\gsetlength{\chapter@mark@width}{\widthof{\chapter@mark}}%
\begin{minipage}[b]{\linewidth-\chapter@mark@width}
#2\hbadness=10000
\end{minipage}}%
}
%
\let\chapterformat\chapterformat@original%
\let\chapterheadendvskip\chapterheadendvskip@original%
\endgroup
}
\makeatother
\begin{document}
\fancychapter{This}
\blindtext
\section{Foo}
\blindtext
\section{Bar}
\blindtext
\chapter{That}
\blindtext
\section{Foo}
\blindtext
\section{Bar}
\blindtext
\end{document}
My trouble is that it seems to be a bad idea to use a scope around a \chapter
command. At the same time I want a scope, because it seems that there is no way to save and later restore the current values of KOMA options or KOMA fonts, as discussed here. I would be happy for any suggestions how to properly set up a different formatting for some chapter titles only.
headings=optiontohead
orheadings=optiontotoc
is set.