1

I'm trying to create fancy chapter headers using KOMA-Script, and one thing that I've noticed is that the table of contents is always numbered 0 because I'm accessing the chapter counter. Is there a way to do this only for chapters 1 and up? i.e. if \thechapter > 0 then access it, otherwise don't.

Here's a MWE that hopefully describes what I mean:

\documentclass{scrreprt}
\KOMAoption{appendixprefix}{false}
\KOMAoption{chapterprefix}{false}
\KOMAoption{cleardoublepage}{empty}
\KOMAoption{DIV}{12}
\KOMAoption{draft}{false}
\KOMAoption{fontsize}{12pt}
\KOMAoption{headings}{big}
\KOMAoption{paper}{a4}
\KOMAoption{parskip}{half}
\KOMAoption{toc}{indent,bibliography}
\KOMAoption{twoside}{semi}
\KOMAoption{captions}{signature}
\KOMAoption{titlepage}{false}
\KOMAoption{abstract}{true}

% KOMA is complaining and apparently this fixes it =/
\usepackage{scrhack}
\usepackage{listings}

% Split document into multiple parts
\usepackage{subfiles}

% e-TeX tools
\usepackage{etoolbox}

% Colours - put this before typography so additional named colours can be defined.
\usepackage{xcolor}
\definecolor{rmitred}{HTML}{CF102F}

%% Typography Settings
\usepackage{microtype}
\usepackage{fontspec}
\defaultfontfeatures{Mapping=tex-text}
\usepackage{xunicode}
\usepackage{xltxtra}

\makeatletter
\setkomafont{chapter}{\let\@hangfrom\chapterhang\let\raggedsection\raggedright\fontsize{28}{28}\mdseries}
\makeatother
\newbox\chapternumberbox
\newbox\chapternamebox
\newcommand{\chapterhang}[2]{%
  \savebox\chapternumberbox{\fontsize{96}{96}\sffamily\color{rmitred}\arabic{chapter}}
  \raisebox{0em}{\usebox\chapternumberbox}
  \parbox[b][1\height][b]{\dimexpr\linewidth-\wd\chapternumberbox}{\raggedright #2}
}

\begin{document}

\tableofcontents

\chapter{Introduction}
\label{ch:introduction}

\section{A Section}

This is an introduction

\chapter{Improving Chapter Styling}

\section{Why}

Why do we want to do this thing? Because fancy.

\section{How}

How do we do this thing?

\chapter{The Second Chapter}

Some important things

\chapter{The Third Chapter}

Lorem ipsum etc

\chapter{Conclusion}

Lorem ipsum etc

\end{document}
2
  • Your code does not compile
    – user31729
    Mar 15 '16 at 9:36
  • I forgot to remove the font loading code from the chapter definition. This was a stripped version of the document I'm working on.
    – Robbie
    Mar 15 '16 at 10:00
1

Apart from the missing font feature, the code

\ifnum\value{chapter} > 0
  \savebox{...}% with \arabic{chapter}
\else
  \savebox{...}% Without \arabic{chapter}
\fi

works for me.

Conditionals about counter values can be done with \ifnum\value{countername} .... \if

\documentclass{scrreprt}
\KOMAoption{appendixprefix}{false}
\KOMAoption{chapterprefix}{false}
\KOMAoption{cleardoublepage}{empty}
\KOMAoption{DIV}{12}
\KOMAoption{draft}{false}
\KOMAoption{fontsize}{12pt}
\KOMAoption{headings}{big}
\KOMAoption{paper}{a4}
\KOMAoption{parskip}{half}
\KOMAoption{toc}{indent,bibliography}
\KOMAoption{twoside}{semi}
\KOMAoption{captions}{signature}
\KOMAoption{titlepage}{false}
\KOMAoption{abstract}{true}

% KOMA is complaining and apparently this fixes it =/
\usepackage{scrhack}
\usepackage{listings}

% Split document into multiple parts
\usepackage{subfiles}

% e-TeX tools
\usepackage{etoolbox}

% Colours - put this before typography so additional named colours can be defined.
\usepackage{xcolor}
\definecolor{rmitred}{HTML}{CF102F}

%% Typography Settings
\usepackage{microtype}
\usepackage{fontspec}
\defaultfontfeatures{Mapping=tex-text}
\usepackage{xunicode}
\usepackage{xltxtra}

\makeatletter
\setkomafont{chapter}{\let\@hangfrom\chapterhang\let\raggedsection\raggedright\fontsize{28}{28}\mdseries}
\makeatother
\newbox\chapternumberbox
\newbox\chapternamebox

\newcommand{\chapterhang}[2]{%
%  \savebox\chapternumberbox{\fontsize{96}{96}\rmitfont\color{rmitred}\arabic{chapter}} 
  \ifnum\value{chapter}>0 
  \savebox\chapternumberbox{\fontsize{96}{96}\color{rmitred}\arabic{chapter}}%
  \raisebox{0em}{\usebox\chapternumberbox} 
  \else
  \savebox\chapternumberbox{\fontsize{96}{96}\color{rmitred}}
  \raisebox{0em}{\usebox\chapternumberbox}% 
  \fi
  \parbox[b][1\height][b]{\dimexpr\linewidth-\wd\chapternumberbox}{\raggedright #2}%
}



\begin{document}

\tableofcontents

\chapter{Introduction}
\label{ch:introduction}

\section{A Section}

This is an introduction

\chapter{Improving Chapter Styling}

\section{Why}

Why do we want to do this thing? Because fancy.

\section{How}

How do we do this thing?

\chapter{The Second Chapter}

Some important things

\chapter{The Third Chapter}

Lorem ipsum etc

\chapter{Conclusion}

Lorem ipsum etc

\end{document}

enter image description here

1
  • @zoqaeski: Well, you spoke of ToC only, not of Appendix numbering -- that's another question actually! And your example does have appendices at all!
    – user31729
    Mar 15 '16 at 9:50
1

Note that \addtokomafont and \setkomafont should only be used to switch font attributes.

Here is a suggestion redefining \chapterformat and \chapterlinesformat (needs KOMA-Script version 3.19a or newer) that also works for unnumbered chapters inside the document.

\addtokomafont{chapter}{\fontsize{28}{28}\selectfont\mdseries}
\renewcommand\chapterformat{%
  {\fontsize{96}{96}\selectfont\textcolor{rmitred}{\thechapter}}\ }

\newbox\chapternumberbox
\renewcommand\chapterlinesformat[3]{%
  \ifstr{#2}{}{
      \parbox[b]{\textwidth}{\raggedright#3}%
    }{%
      \savebox\chapternumberbox{#2}%
      \usebox\chapternumberbox%
      \parbox[b]{\dimexpr\textwidth-\wd\chapternumberbox\relax}{\raggedright#3}%
  }\par\nobreak
}

enter image description here

Code:

\documentclass{scrreprt}[2015/10/03]
\KOMAoption{appendixprefix}{false}
\KOMAoption{chapterprefix}{false}
\KOMAoption{cleardoublepage}{empty}
\KOMAoption{DIV}{12}
\KOMAoption{draft}{false}
\KOMAoption{fontsize}{12pt}
\KOMAoption{headings}{big}
\KOMAoption{paper}{a4}
\KOMAoption{parskip}{half}
\KOMAoption{toc}{indent,bibliography}
\KOMAoption{twoside}{semi}
\KOMAoption{captions}{signature}
\KOMAoption{titlepage}{false}
\KOMAoption{abstract}{true}

% KOMA is complaining and apparently this fixes it =/
\usepackage{scrhack}
\usepackage{listings}

% Split document into multiple parts
\usepackage{subfiles}

% e-TeX tools
\usepackage{etoolbox}

% Colours - put this before typography so additional named colours can be defined.
\usepackage{xcolor}
\definecolor{rmitred}{HTML}{CF102F}

%% Typography Settings
\usepackage{microtype}
\usepackage{fontspec}
\defaultfontfeatures{Mapping=tex-text}
\usepackage{xunicode}
\usepackage{xltxtra}

\addtokomafont{chapter}{\fontsize{28}{28}\selectfont\mdseries}
\renewcommand\chapterformat{%
  {\fontsize{96}{96}\selectfont\textcolor{rmitred}{\thechapter}}\ }

\newbox\chapternumberbox
\renewcommand\chapterlinesformat[3]{%
  \ifstr{#2}{}{
      \parbox[b]{\textwidth}{\raggedright#3}%
    }{%
      \savebox\chapternumberbox{#2}%
      \usebox\chapternumberbox%
      \parbox[b]{\dimexpr\textwidth-\wd\chapternumberbox\relax}{\raggedright#3}%
  }\par\nobreak
}

\begin{document}
\tableofcontents
\chapter{Introduction}
\label{ch:introduction}
\section{A Section}
This is an introduction
\chapter{Improving Chapter Styling}
\section{Why}
Why do we want to do this thing? Because fancy.
\section{How}
How do we do this thing?
\chapter{The Second Chapter}
Some important things
\chapter{The Third Chapter}
Lorem ipsum etc
\chapter{Conclusion}
Lorem ipsum etc
\addchap{An unnumbered chapter}
\end{document}
3
  • I kept reading the KOMA documentation and found advice along those lines. I also achieved the same result by using a whole lot of boxes in the \chapterformat command, which let me play with moving various bits around, but I assume that approach is the more naïve one?
    – Robbie
    Mar 15 '16 at 15:27
  • With scrreprt or scrbook redefining \chapterformat is the suggested way to format the chapter number. Since version 3.19 it is possible to change the order and/or the layout of the already formated chapter number and chapter title by redefining \chapterlinesformat or \chapterlineswithprefixformat. In your example the redefinition of \chapterlinesformat is only needed to ensure that the chapter number is bottom aligned with the last line of a longer chapter heading. You could remove it if all chapter headings are one-line.
    – esdd
    Mar 15 '16 at 16:01
  • Oh, ok. Some of the ideas I was toying with included putting the chapter number above the chapter title, and possibly inserting a graphic in as well. I've seen some rather nice reports made with InDesign and I figured I could probably get the same results with LaTeX with some effort.
    – Robbie
    Mar 16 '16 at 0:09

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.