4

As the chapters are numbered in TOC (table of contents) the Appendices are lettered. I want to change that from a single letter "A" to "Appendix A".

A Minimal Working Example:

\documentclass{scrreprt}
\usepackage[utf8]{inputenc}
\title{MWE}
\author{John D.}
\date{February 2020}
\pagenumbering{arabic}

\include{sections/Introduction}
\begin{document}
\tableofcontents

\maketitle
\chapter{This is a section}
\section{This is a subsection}
Some random content
\appendix
\chapter{Numbers}
\section{Numbers 1}
\chapter{Code}
\section{Code 1}

\end{document}

This will Produce: MWE example

4
  • Probably tex.stackexchange.com/a/226497/134144 might be useful.
    – leandriis
    Feb 11, 2020 at 19:55
  • Update: I edited MWE: @leandriis I am using a different document class, {\documentclass{article}} but {\documentclass{scrreprt}}. Therefore, appendices are classified as chapters. I tried what it is in the link but did not work!
    – KrisQ
    Feb 11, 2020 at 20:09
  • Do you just want to change the appearance in the table of contents or also in the document itself?
    – leandriis
    Feb 11, 2020 at 20:15
  • @leandriis "Appendix A" should be shown only in the TOC. In the document "A. Appendix name".
    – KrisQ
    Feb 11, 2020 at 20:18

1 Answer 1

4

You can declare a new TOC style entry appendixchapter using the prefix:

\documentclass{scrreprt}[2019/10/12]% needs version 3.27 or newer
\newcommand\entrywithprefix[2]{#1~#2}
\DeclareTOCStyleEntry[
  level:=chapter,
  indent:=chapter,
  numwidth:=chapter,
  dynnumwidth,
  entrynumberformat=\entrywithprefix{\appendixname}
]{chapter}{appendixchapter}

\usepackage{xpatch}
\xapptocmd{\appendix}{%
  \xpatchcmd{\addchaptertocentry}
    {\addtocentrydefault{chapter}{#1}{#2}}
    {\addtocentrydefault{appendixchapter}{#1}{#2}}
    {}{\PatchFailedII}%
  }{}{\PatchFailedI}

\title{MWE}
\author{John D.}
\date{February 2020}

\begin{document}
\tableofcontents
\maketitle
\chapter{This is a chapter}
\section{This is a section}
Some random content
\appendix
\chapter{Numbers}
\section{Numbers 1}
\chapter{Code}
\section{Code 1}
\end{document}

Run three times to get

enter image description here


With older KOMA-Script versions use

\DeclareTOCStyleEntry[
  level=\chaptertocdepth,
  indent=0pt,
  numwidth=1.5em,
  dynnumwidth,
  entrynumberformat=\entrywithprefix{\appendixname}
]{chapter}{appendixchapter}

If you are using numbered bookmarks:

\documentclass{scrreprt}[2019/10/12]% needs version 3.27 or newer
%\providecommand*\Ifstr{\ifstr}% needed up to and including KOMA-Script version 3.27, see https://komascript.de/faq_deprecatedif

\DeclareTOCStyleEntry[
  level:=chapter,
  indent:=chapter,
  numwidth:=chapter,
  dynnumwidth,
]{chapter}{appendixchapter}

\usepackage{xpatch}
\xapptocmd{\appendix}{%
\xpatchcmd{\addchaptertocentry}
  {\addtocentrydefault{chapter}{#1}{#2}}
  {%
    \Ifstr{#1}{}
      {\addtocentrydefault{appendixchapter}{}{#2}}
      {\addtocentrydefault{appendixchapter}{\appendixname~#1}{#2}}%
  }{}{\PatchFailedII}%
}{}{\PatchFailedI}

\title{MWE}
\author{John D.}
\date{February 2020}

\usepackage[numbered]{bookmark}

\begin{document}
\tableofcontents
\maketitle
\chapter{This is a chapter}
\section{This is a section}
Some random content
\appendix
\chapter{Numbers}
\section{Numbers 1}
\chapter{Code}
\section{Code 1}
\end{document}

enter image description here

5
  • This is exactly what I wanted! Thank you very much. It was a smooth and easy solution! I added the new TOC style entry in my preamble, the first solution suggested, and it worked fine.
    – KrisQ
    Feb 12, 2020 at 12:13
  • I just noticed that: It is showed right in the TOC but, In the bookmarks of PDF it is shown as a regular chapter. Is there a way to show it some way in the bookmarks of PDF?
    – KrisQ
    Feb 12, 2020 at 12:23
  • 1
    See my updated answer. But there was neither package hyperref nor package bookmark in your MWE.
    – esdd
    Feb 12, 2020 at 12:44
  • I used your updated version. It is shown as I want in TOC and pdf bookmark section. However, when I use the bookmark package I get an error: Missing begin{document} in the preamble. Note: I had hyperref package in my preamble since in the beginning.
    – KrisQ
    Feb 12, 2020 at 15:26
  • Solved: First Thank you @esdd for the code. Everything is perfect in your code but I have different files for preamble, for main.tex and the chapters. finally, \usepackage[numbered]{bookmark} should be the last thing added to preamble... literally the last, just before \begin{document}
    – KrisQ
    Feb 12, 2020 at 15:37

You must log in to answer this question.

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