12

How does one apply a custom styling to the chaptertitle in KOMA-Script? I know about the existence of the titlesec package but I've read that it doesn't play very well with KOMA-Script which outputs a warning. Therefore my idea was to style the chapter page soley with the help of KOMA-Script functionality.

I've read some posts about styling chapters and came up with the following code:

\documentclass[english]{scrreprt}
\usepackage[utf8]{inputenc}
\usepackage{blindtext}

\KOMAoption{chapterprefix}{true}
% centering the chapter head
\let\raggedchapter\centering

\setkomafont{chapterprefix}{\normalsize\mdseries}

% This block has the two variables \chapapp and \thechapter. But where
% can I find a variable for the title of the chapter (eg. \chaptertile) to apply some styling? 
\renewcommand*{\chapterformat}{%
    \MakeUppercase{\chapapp}~\thechapter\\[-0.6\baselineskip]
    \noindent\makebox[\textwidth]{%
        \rule{0.9\textwidth}{.5pt}%
    }%
}

% If I use this line for styling the title of the chapter - I get an error
% \addtokomafont{chapter}{\MakeUppercase}

\RedeclareSectionCommand[beforeskip=0pt,afterskip=8\baselineskip]{chapter}

\begin{document}

\blinddocument

\end{document}

Using \renewcommand*{\chapterformat} does work for styling the \chapapp~\thechapter which correctly outputs "Chapter 1". What I have not found is a variable similar to \chapapp or \thechapter which holds the chapter title and can be used to style the title, like assigning uppercase a.s.o.

I have found \addtokomafont{chapter}{} but somehow this is not working with the MakeUppercase makro.

My question would be if a similar command to \renewcommand*{\chapterformat} exists where I can access a variable for the chaptertitle?

  • The line with MakeUppercase mainly throws an error because there is a brace too much. But there was indeed a bug in an older version. I don't really know what you are looking for with the header, but i think package scrlayer-scrpage will help you. – Johannes_B May 22 '15 at 15:25
  • What exactly do you want to have uppercased? And where is this line supposed to appear? – Johannes_B May 22 '15 at 15:30
  • @Johannes_B: Yes, the brace was too much, sorry for that. My plan was to make the chapter title uppercase. MakeUppercase still throws an error even without the useless brace removed. The title should appear under the line but in uppercase letters. I hope that explains it somehow. – hetsch May 22 '15 at 15:58
  • 1
    The commands that influence the font of the chapter naturally also influence the chapterformat, this all boils down to \documentclass{article} \begin{document} \centering\MakeUppercase{something\\else} \end{document} The recommenden interface for this would be chapterheadmidvskip, but currently this is also uder the influence of the command. I informed the author. – Johannes_B May 22 '15 at 16:28
  • 1
    Oh, very up to date, nice :-) Cross-link komascript.de/node/1926 – Johannes_B May 22 '15 at 16:47
14

Second update

Here is a new and in my opinion better suggestion that needs KOMA-Script Version 3.19 or newer. In version 3.19 there is a new command \chapterlineswithprefixformat that could be redefined to use \MakeUppercase for the chapter titles.

\documentclass[english]{scrreprt}[2015/09/29]% needs version 3.19 or newer
\usepackage[utf8]{inputenc}
\usepackage{babel}
\usepackage{blindtext}

\KOMAoption{chapterprefix}{true}
\renewcommand*\raggedchapter{\centering}
\RedeclareSectionCommand[beforeskip=0pt,afterskip=8\baselineskip]{chapter}
\setkomafont{chapterprefix}{\normalsize\mdseries}

\renewcommand*{\chapterformat}{%
  \chapappifchapterprefix{\nobreakspace}\thechapter\autodot%
  \IfUsePrefixLine{%
    \par\nobreak\vspace{-\parskip}\vspace{-.6\baselineskip}%
    \rule{0.9\textwidth}{.5pt}%
  }{\enskip}%
}

\renewcommand\chapterlineswithprefixformat[3]{%
  \MakeUppercase{#2#3}
}

\begin{document}
\tableofcontents
\chapter{Chapter One}
\textbf{\KOMAScriptVersion}
\par
\Blindtext
\addchap{Chapter without number}
\Blindtext
\end{document}

Note: If you also want to use \MakeUppercase without the option chapterprefix=true you have to redefine the command \chapterlinesformat:

\makeatletter
\renewcommand{\chapterlinesformat}[3]{%
  \@hangfrom{#2}{\MakeUppercase{#3}}%
}
\makeatother

Update

My original answer (see below) works with both MiKTeX2.9 and TL 2015 but not with 2014 (but you can load fixltx2e to get it to work as @Johannes mentioned in a comment). So here is another suggestion.

\documentclass[english]{scrreprt}
\usepackage[utf8]{inputenc}
\usepackage{babel}
\usepackage{blindtext}

\KOMAoption{chapterprefix}{true}
\renewcommand*\raggedchapter{\centering}
\newif\ifmakeupper
\newcommand*\chaptertitleformat[1]{\ifmakeupper\MakeUppercase{#1}\else#1\fi}
\addtokomafont{chapter}{\makeuppertrue}
\setkomafont{chapterprefix}{\normalsize\mdseries}
\renewcommand*{\chapterformat}{%
    \MakeUppercase{\chapappifchapterprefix{\nobreakspace}}\thechapter\autodot%
    \IfUsePrefixLine{%
      \par\nobreak\vspace{-\parskip}\vspace{-.6\baselineskip}%
      \rule{0.9\textwidth}{.5pt}%
    }{\enskip}%
}
\RedeclareSectionCommand[beforeskip=0pt,afterskip=8\baselineskip]{chapter}

\renewcaptionname{english}{\contentsname}{\chaptertitleformat{Contents}}

\begin{document}
\tableofcontents
\chapter{\chaptertitleformat{Chapter One}}
\textbf{\KOMAScriptVersion}
\par
\Blindtext
\addchap{\chaptertitleformat{Chapter without number}}
\Blindtext
\end{document}

Note, you have to redefine \tablename, \listfigurename, \listtablename, ... so that they use \chaptertitleformat and you have to insert this in all \chapter commands. But as an advantage now there is only a switch in the \addtokomafont command.

enter image description here


Original answer (does not work with Version 3.19)

Here is a suggestion but note that \MakeUppercase inside \setkomafont or \addtokomafont can be problematic. In any case \MakeUppercase have to be the last command added to the komafont of a fontelement because it needs an argument.

\documentclass[english]{scrreprt}[2015/02/07]
\usepackage[utf8]{inputenc}
\usepackage{babel}
\usepackage{blindtext}

\KOMAoption{chapterprefix}{true}
\renewcommand*\raggedchapter{\centering}
\addtokomafont{chapter}{\MakeUppercase}
\setkomafont{chapterprefix}{\normalsize\mdseries}
\renewcommand*{\chapterformat}{%
    \chapapp~\thechapter%
    \par\nobreak\vspace{-\parskip}\vspace{-.6\baselineskip}%
    \rule{0.9\textwidth}{.5pt}%
}
\RedeclareSectionCommand[beforeskip=0pt,afterskip=8\baselineskip]{chapter}

\begin{document}
\tableofcontents
\blinddocument
\end{document}

enter image description here

  • I actually patched chapterformat as suggested by Markus :-) Too late. +1 – Johannes_B May 22 '15 at 20:40
  • Hello @esdd, thank you for your solution. Somehow this code still throws and error with the message: (./test.aux) (./test.toc) [1{/home/hetsch/.texlive2014/texmf-var/fonts/map/pdft ex/updmap/pdftex.map}] Chapter 1. ! Use of \@rule doesn't match its definition. <argument> \def l.19 \blinddocument If your solution works for you and the and @Johannes_B, I might have something totally messed up on my local pc. I have to check that out and afterwords I'm happy to accept your answer. Thank you so far! – hetsch May 23 '15 at 7:18
  • On my local pc there is still a problem with \MakeUppercase. If I comment out \addtokomafont{chapter}{\MakeUppercase} everything compiles fine Without the error message from the comment above. – hetsch May 23 '15 at 7:32
  • 1
    I haven't actually tried out your code, i know your skills with KOMA-script. But i actually get the same error as in my own tests, the same that was already mentioned on the linked post to komascript.de. \documentclass{article} \begin{document} \MakeUppercase{ test \rule{5cm}{1cm} test} \end{document} <- But this is fixed with the new Kernel. – Johannes_B May 23 '15 at 8:49
  • 1
    You could have just used fixltx2e ;-) – Johannes_B May 23 '15 at 10:35
4

A bit alternative approach:

\documentclass{scrreprt}
\let\raggedchapter\centering

\setkomafont{chapterprefix}{\normalsize\mdseries}
\KOMAoption{chapterprefix}{true}
\renewcommand{\chapterheadmidvskip}{%
    \par\nobreak\vskip -.1cm {\rule{.9\textwidth}{.5pt}}\par%
}
\RedeclareSectionCommand[beforeskip=0pt,afterskip=8\baselineskip]{chapter}
\usepackage{xpatch}
\tracingpatches
\xpatchcmd{\@@makeschapterhead}{%for the unnumbered
    \usekomafont{chapter}{#1}\par
}{%
    \usekomafont{chapter}{\MakeUppercase{#1}}\par
}{}{}
\xpatchcmd{\@@makechapterhead}{%for numbered
    \usekomafont{chapter}{#1}\par
}{%
    \usekomafont{chapter}{\MakeUppercase{#1}}\par
}{}{}
\renewcommand*{\chapterformat}{%
    \mbox{\MakeUppercase{\chapappifchapterprefix{\nobreakspace}}\thechapter\autodot
    \IfUsePrefixLine{}{\enskip}}%
}

\begin{document}
\tableofcontents
\listoffigures
\chapter*{Bussy Baboon}
\chapter{walzing Wombat}
\end{document}
  • I think this is a better solution than my suggestions. – esdd May 23 '15 at 12:53
  • @esdd Using the new kernel, there is no need to patch anything. Nevertheless, this feels quite patchy. – Johannes_B May 23 '15 at 12:54
  • Indeed, this approach makes it easy to apply different styles to the chapter elements. This was somehow part of my original question. If you don't mind @esdd, I accept this one as my favourite answer. – hetsch May 23 '15 at 13:11
  • @hetsch This is the very patchy approach. Markus stated, that the font switches are for font changes, which \MakeUppercase is not and it should be treated within chapterformat. Haking the chapterhead-command feels wrong, to be honest. As KOMA is growing more and more complex, the quick and dirty patch it approach will be impossible. – Johannes_B May 23 '15 at 13:14
  • @Johannes_B: True words. But can you think of another way, where it would be possible to separately style the chaptertile (Walzing Wombat) via \xpatchcmd{\@@makechapterhead} and the chapterprefix (Chapter 1) formatted via \renewcommand*{\chapterformat}. I'm thinking about applying \MakeUppercase{\textls{#1}} to the chaptertitle. My appologies if I'm terribly missing something! – hetsch May 23 '15 at 13:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.