0

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.

3
  • There are numbered chapters using the fancy style and numbered chapters using the default style? The unnumbered chapters get all the default style?
    – esdd
    Oct 23, 2019 at 14:50
  • The way it is intended, all numbered chapters will be the fancy style and all unnumbered chapters will be the default style. But from a puristic point of view, of course, the more general the solution, the better. :-)
    – ranguwud
    Oct 23, 2019 at 14:53
  • You would get an error if option headings=optiontohead or headings=optiontotoc is set.
    – esdd
    Oct 23, 2019 at 16:49

1 Answer 1

1

Here is a suggestion where the numbered chapters get the different layout automatically:

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

\definecolor{chapmark}{RGB}{148,148,148}
\newkomafont{chapternumber}{%
  \usekomafont{chapterprefix}%
  \normalfont\fontfamily{LinuxBiolinumT-TLF}\selectfont%
  \color{chapmark}%
}
\newcommand*\numberscale{5}

\renewcommand*\chapterformat{%
  \scalebox{\numberscale}{\usekomafont{chapternumber}\thechapter}\quad%
}

\let\originalchapterlinesformat\chapterlinesformat
\renewcommand*\chapterlinesformat[3]{%
  \Ifstr{#1}{chapter}
    {%
      \IfArgIsEmpty{#2}
        {\originalchapterlinesformat{#1}{#2}{#3}}
        {%
          #2%
          \parbox[t]{\textwidth-\widthof{#2}}{%
            \parbox[b]{\linewidth}{#3}%
              \par\vskip-\baselineskip%
              \makebox[0pt][l]{\normalsize\raisebox{-1.2\baselineskip}[0pt][0pt]{%
                \hspace{-1ex}%
                \rule[0pt]{\linewidth+1ex}{1pt}}}\par%
              \vskip\baselineskip%
          }%
        }%
    }
    {\originalchapterlinesformat{#1}{#2}{#3}}%
}

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

\chapter{That}
\blindtext
\section{Foo}
\blindtext
\section{Bar}
\blindtext

\tableofcontents
\end{document}

Result:

enter image description here


If only some numbered chapters should get the special format (do you really want to do so?):

\documentclass[chapterprefix=true]{scrreprt}
%\providecommand*\Ifstr{\ifstr}% needed up to and including KOMA-Script version 3.27, see https://komascript.de/faq_deprecatedif
\usepackage{calc}
\usepackage{xcolor}
%\usepackage{xparse}% not needed for this suggestion
\usepackage{graphicx}
\usepackage{blindtext}
%\KOMAoption{parskip}{false}% default

\definecolor{chapmark}{RGB}{148,148,148}
\newkomafont{chapternumber}{%
  \usekomafont{chapterprefix}%
  \normalfont\fontfamily{LinuxBiolinumT-TLF}\selectfont%
  \color{chapmark}%
}
\newcommand*\numberscale{5}
\newif\ifdecoratedchapter
\newif\ifdefaultchapterprefix
\makeatletter
\newcommand\usedecoratedchapters{%
  \decoratedchaptertrue%
  \if@chapterprefix\defaultchapterprefixtrue\else\defaultchapterprefixfalse\fi%
    \KOMAoption{chapterprefix}{false}%
}
\newcommand\usedefaultchapters{%
  \ifdecoratedchapter%
    \ifdefaultchapterprefix\@chapterprefixtrue\else\@chapterprefixfalse\fi%
  \fi%
  \decoratedchapterfalse%
}
\makeatletter
\let\originalchapterformat\chapterformat
\renewcommand*\chapterformat{%
  \ifdecoratedchapter%
    \scalebox{\numberscale}{\usekomafont{chapternumber}\thechapter}\quad%
  \else%
    \originalchapterformat%
  \fi%
}

\let\originalchapterlinesformat\chapterlinesformat
\renewcommand*\chapterlinesformat[3]{%
  \Ifstr{#1}{chapter}
    {%
      \IfArgIsEmpty{#2}
        {\originalchapterlinesformat{#1}{#2}{#3}}
        {%
          \ifdecoratedchapter%
            #2%
            \parbox[t]{\textwidth-\widthof{#2}}{%
              \parbox[b]{\linewidth}{#3}%
                \par\vskip-\baselineskip%
                \makebox[0pt][l]{\normalsize\raisebox{-1.2\baselineskip}[0pt][0pt]{%
                  \hspace{-1ex}%
                  \rule[0pt]{\linewidth+1ex}{1pt}}}\par%
                \vskip\baselineskip%
            }%
          \else%
            \originalchapterlinesformat{#1}{#2}{#3}%
          \fi%
        }%
    }
    {\originalchapterlinesformat{#1}{#2}{#3}}%
}

\begin{document}
\usedecoratedchapters
\chapter{This}
\usedefaultchapters
\blindtext
\section{Foo}
\blindtext
\section{Bar}
\blindtext

\chapter{That}
\blindtext
\section{Foo}
\blindtext
\section{Bar}
\blindtext

\tableofcontents
\end{document}

Result:

enter image description here

Warning: \if@chapterprefix is an internal command. So it can change in the future.

2
  • Wow, you have put a lot of effort into this in order to move all formatting to \chapterlinesformat, where you can control if the current chapter has a number or not. Great, this looks very promising. Of course only version 1 is of practical interest, but it's nice to see, how version 2 would work in principle. There is one issue still, that I cannot solve at the moment: In the appendix, chapterprefix is true by default, so \chapterlineswithprefixformat needs to be modified similarly. Then, however, \widthof{#2} throws Missing } inserted., probably because of a \setlength in #2.
    – ranguwud
    Oct 23, 2019 at 18:21
  • Okay, I just figured out that there is also an option appendixprefix, which does the trick when set to false. Then, also no redefinition of \chapterlineswithprefixformat is needed. Marking as solved.
    – ranguwud
    Oct 23, 2019 at 18:37

You must log in to answer this question.

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