15

I am writing my thesis and need to put 'Appendix' before the numbering (A, B, C, ...) in the table of contents (TOC), the chapter title and in the header. I'd also like for \autoref{} to output 'Appendix A'.

I am using the scrbook class and have tried several different ways to achieve this but nothing gives me the desired output.

I want the title to appear as below so that it reads 'Appendix A (Title)'. This is also how I want it to appear in the TOC and in my header.

Appendix Title

I've tried using the KOMA option appendixprefix=true but this splits Appendix A and More Information onto separate lines and doesn't put 'Appendix' before A in the TOC.

I've tried redefining \thechapter with \renewcommand{\thechapter}{Appendix \Alph{chapter} which gives me the desired output in the title and header of my document but the table of contents output is below and \autoref{}'s output is 'Appendix Appendix A'.

TOC Output

I've tried the appendix package too but this won't add Appendix to the title in the main body, only to the TOC.

I've also tried some other hacks which work in both the TOC and the title but then show the previous chapter in the header.

I set my headers using the following

\usepackage{fancyhdr}

\setlength{\headheight}{15pt}

\pagestyle{fancy}
\renewcommand{\chaptermark}[1]{ \markboth{\thechapter~#1}{} } % Adds chapter number name to right header
\renewcommand{\sectionmark}[1]{ \markright{\thesection~#1}{} } % Adds section name to left header

I'd really appreciate some help with this as I can't find a solution which works for my 3 main cases of title, TOC and header using the scrbook class.

MWE:

\documentclass[12pt,a4paper,oneside,pointlessnumbers]{scrbook}

\usepackage[english]{babel}
\usepackage{blindtext}
\usepackage{fancyhdr}
\usepackage[colorlinks]{hyperref}

\setlength{\headheight}{15pt}

\pagestyle{fancy}
\renewcommand{\chaptermark}[1]{ \markboth{\thechapter~#1}{} }
\renewcommand{\sectionmark}[1]{ \markright{\thesection~#1}{} } 

\begin{document}

\frontmatter
\tableofcontents

\mainmatter
\blinddocument

\appendix

\chapter{More Information} \label{app:first}
\section{Another Section}
\autoref{app:first} 
\Blindtext

\end{document}
  • Welcome to TeX.SX! Help us to help you: Please add a full minimal working example (MWE) that illustrates your problem. It will be much easier for us to reproduce your situation and find out what the issue is when we see compilable code, starting with \documentclass{...} and ending with \end{document}. – Speravir Mar 6 '14 at 2:25
  • 3
    Please read KOMA-Script documentation for class option chapterprefix and \KOMAoption/\KOMAoptions. Also consider using the KOMA-Script package scrlayer-scrpage instead of fancyhdr. The switch should not be that hard. – Speravir Mar 6 '14 at 2:32
  • Doesn't my answer work for you? – karlkoeller Mar 6 '14 at 11:06
  • @karlkoeller Sorry for the delay in commenting on your answer. It didn't work perfectly for me and I just wanted to see if I could add something to it to make it work for me. – IKavanagh Mar 6 '14 at 11:20
15

Add the following lines to your preamble:

\makeatletter
\g@addto@macro\appendix{%
  \renewcommand*{\chapterformat}{%
    {\chapapp\nobreakspace\thechapter\autodot\enskip}%
  }
  \renewcommand*{\chaptermarkformat}{%
    {\chapapp\nobreakspace\thechapter\autodot\enskip}%
  }
  \let\oldaddcontentsline\addcontentsline
  \newcommand\hackedaddcontentsline[3]{\oldaddcontentsline{#1}{#2}{\chapapp\nobreakspace#3}}
  \let\oldchapter\chapter
  \renewcommand*\chapter[1]{%
    \let\addcontentsline\hackedaddcontentsline%
    \oldchapter{#1}%
    \let\addcontentsline\oldaddcontentsline%
  }
}
\makeatother

With \g@addto@macro we add to the command \appendix the following modifications:

  1. We redefine \chapterformat so to add the word "Appendix" before the chapter number.
  2. We redefine \chaptermarkformat so to add the word "Appendix" before the chapter number in the header.
  3. The rest of the code redefines the meaning of \addcontentsline only for the \chapter command, so to add the word "Appendix" in the ToC also.

The command \autoref works as expected.

MWE:

\documentclass[numbers=noenddot]{scrbook}
\usepackage[colorlinks]{hyperref}

\makeatletter
\g@addto@macro\appendix{%
  \renewcommand*{\chapterformat}{%
    {\chapapp\nobreakspace\thechapter\autodot\enskip}%
  }
  \renewcommand*{\chaptermarkformat}{%
    {\chapapp\nobreakspace\thechapter\autodot\enskip}%
  }
  \let\oldaddcontentsline\addcontentsline
  \newcommand\hackedaddcontentsline[3]{\oldaddcontentsline{#1}{#2}{\chapapp\nobreakspace#3}}
  \let\oldchapter\chapter
  \renewcommand*\chapter[1]{%
    \let\addcontentsline\hackedaddcontentsline%
    \oldchapter{#1}%
    \let\addcontentsline\oldaddcontentsline%
  }
}
\makeatother

\begin{document}

\tableofcontents

\chapter{1st chapter}
\section{1st section}
\chapter{2nd chapter}
\section{2nd section}

\appendix

\chapter{1st appendix chapter}\label{app:first}
\section{1st appendix section}
\chapter{2nd appendix chapter}
\section{2nd appendix section}
\autoref{app:first}

\end{document} 

Output (ToC)

enter image description here

Output (An appendix)

enter image description here

  • Thanks for the great answer and explanation. That works perfectly for the TOC, the chapter title and \autoref but it doesn't show 'Appendix' before 'B 2nd appendix chapter' in my page title which I set using '\thechapter~#1'. Do I need to change how I'm setting my page headers? – IKavanagh Mar 6 '14 at 11:19
  • 2
    @karlkoeller i seem to have the problem that this solution affects the title of my bibliography (using biblatex/biber), is there an easy way to solve this? (if i simply place my bibliography before appendices it's fine, but that's not the place my bibliography has to go) – Charles-Alexandre Mattelaer Mar 10 '15 at 13:36
  • 3
    @Charles-AlexandreMattelaer It affects everything following the \appendix command, including a bibliography. Try adding in the preamble \usepackage{etoolbox} \pretocmd\thebibliography{% \let\chapter\oldchapter% }{}{}. A space in this code means a linebreak. – karlkoeller Mar 10 '15 at 20:04
  • 3
    @Charles-AlexandreMattelaer Are you using biblatex? If yes, replace \thebibliography with \printbibliography – karlkoeller Mar 12 '15 at 9:56
  • 1
    @karlkoeller Great answer; but like Charles-Alexandre, I had to make use of your further comments to really make this work. Perhaps it would be a good idea to incorporate these into the main body of the text? – Danu Sep 19 '17 at 17:40
2

For the formatting of title in the form Appendix A (title) and entries in the TOC, there is a 'quick-and-dirty' solution, by redefinition of the section command, however, not the \autoref issue.

\documentclass{scrbook}
\usepackage[manualmark]{scrpage2}
\usepackage{blindtext}   % Not really needed
\usepackage{hyperref}


\newcommand*{\CurrentSectionTitle}[1]{#1}%



\begin{document}

\tableofcontents

\chapter{Some Content}

\section{Hello}
In \autoref{Section::NumberTwo} it is shown that \[ E = m c^2 \]

\section{Hello Again}
\label{Section::NumberTwo}%
In \autoref{Appendix::B} it is shown again that \[ E = m c^2 \]
holds.

\cleardoublepage


%%%% Now appendix stuff

% Set counter format to letters

\renewcommand{\thesection}{\Alph{section}}


\let\LaTeXStandardSection\section

% Quick and dirty version of a `section wrapper`
\renewcommand*{\section}[1]{%  Does not work if optional argument is desired!
\renewcommand*{\CurrentSectionTitle}{#1}%
\refstepcounter{section}%  Needed, since starred version of section command (see below)
\addcontentsline{toc}{chapter}{\protect{\appendixname~\thesection~\CurrentSectionTitle}} % Change if not appropiate format
%Prevent entry with number in TOC, since starred version of standard section command
\LaTeXStandardSection*{\appendixname~\thesection~\CurrentSectionTitle}%  
}%

\pagestyle{scrheadings}


\appendix 
\section{Number One} 
\blindtext[5] % Some dummy text
\section{Number Two} 
\label{Appendix::B}
\blindtext
\section{Number Three} 
\blindtext

\end{document}

enter image description here Reduced screenshot of three appendices, one-column-format

As said above, this is a quick and dirty solution, perhaps there are better approaches than redefining the section command (which is in my case not completely done, since it does not allow for TOC short titles)

Concerning the \autoref, there is a \sectionautorefname command which can be redefined to

\renewcommand*{\sectionautorefname}{\appendixname}

however, this will be used at any occurence of a section autoreference afterwards. I have not found a solution so far to bypass this.

2

Here is a suggestion that needs KOMA-Script version 3.20 or newer:

\documentclass[
  oneside,
  numbers=noenddot,
  appendixprefix
]{scrbook}[2016/05/10]% needs version 3.20 or newer
\usepackage{blindtext}% dummy text

\usepackage{xpatch}
\xapptocmd{\appendix}{%
  \addtocontents{toc}{%
    \RedeclareSectionCommand[
      tocdynnumwidth,
      tocentrynumberformat=\tocappendixnumber
    ]{chapter}%
  }%
}{}{\PatchFailed}
\newcommand\tocappendixnumber[1]{\chapapp~#1}

\usepackage{hyperref}
\begin{document}
\tableofcontents
\blinddocument
\appendix
\chapter{First chapter in appendix}\label{app:first}
\section{Section in appendix}
See \autoref{app:first}

\Blindtext[10]
\blinddocument
\end{document}

Run three times to get:

enter image description here

enter image description here

enter image description here

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.