5

My first question is how to add the chapter or appendix prefix in the table of contents of scrbook class instead of the its default appearance.

For example, in the figure below, I want to change the chapter entry in TOC to Chapter 1. Introduction.

and the same goes for the appendix entry in TOC, which means typesetting Appendix A. Electro... instead of the figure below.

Additionally, I have used \appendix before starting my appendices, so it treats all the subsequent chapters as chapters A, B, etc. However, I want to treat them as appendices to appear correctly in the page headings as "Appendix A. Electro..." instead of "Chapter A. Electro..." in the page heading as shown below.

enter image description here

Thanks in advance

Diaa
  • 9,599

3 Answers3

4

Here is a suggestion that also works if you use chapters in frontmatter or options like listof=totoc, listof=chapterentry or if you use \addchap.

\documentclass[
  chapterprefix,
  %numbers=noenddot,
  %toc=indentunnumbered,
  listof=totoc,
  %listof=chapterentry
]{scrbook}[2016/05/10]

\usepackage[english]{babel}

\usepackage{xpatch}
\xpatchcmd{\addchaptertocentry}{%
    \addtocentrydefault{chapter}{#1}{#2}%
  }{%
    \ifstr{#1}{}
      {\addtocentrydefault{chapter}{#1}{#2}}%<- unnumbered chapters in TOC
      {\addtocentrydefault{chapter}{\chapapp{} #1}{#2}}%<- numbered chapters in TOC
  }{}{\PatchFailed}

\RedeclareSectionCommand[
    tocdynnumwidth
]{chapter}

\usepackage{blindtext}
\begin{document}
\frontmatter
\tableofcontents
\listoftables
\chapter{This is a chapter in frontmatter}
\mainmatter
\blinddocument
\begin{table}%
  \begin{tabular}{l}
  \Huge X
  \end{tabular}
\caption{A table}
\end{table}
\addchap{This is an unnumbered chapter}
\addsec{This is an unnumbered section}
\appendix
\blinddocument
\end{document}

Result:

enter image description here

esdd
  • 85,675
4

Thanks to the answer of the question \chaptername is used even for appendix chapters in ToC

the syntax of

\usepackage{tocloft,calc}
\renewcommand{\cftchappresnum}{\chaptername\space}
\setlength{\cftchapnumwidth}{\widthof{\textbf{Appendix~999~}}}
\makeatletter
\g@addto@macro\appendix{%
  \addtocontents{toc}{%
    \protect\renewcommand{\protect\cftchappresnum}{\appendixname\space}%
  }%
}
\makeatother

has a successful result in prefixing the chapters entries in TOC with the word "Chapter" (same for appendices).

For the running headings in the in the appendices environment, the following syntax changes

Chapter to Appendix

\begin{document}

\appendix
\renewcommand{\chaptermark}[1]{\markboth{Appendix\ \thechapter.\ #1}{}}
\chapter{Appendix A}
\chapter{Appendix B}

\end{document}
Diaa
  • 9,599
  • Note: You should not use tocloft with KOMA-Script class scrbook. See the warning message you'd get. – cabohah Jan 15 '24 at 09:33
4

Your first question can be solved by using the tocstyle-package included in the KOMAscript-bundle. Try

 texdoc tocstyle

at an command prompt to see the documentation.

By redefining some internal variables, if I understand your question correctly, you will have the answer to your first question. Put this commands in your preamble:

\usepackage{tocstyle}
\renewcommand*{\addchaptertocentry}[2]{%
  \addtocentrydefault{chapter}{\chapapp\nobreakspace #1}{#2}%
}
\usetocstyle{KOMAlike}

Your second question was:

However, I want to treat them as appendices to appear correctly in the page headings as "Appendix A. Electro..."

This be solved by using the class-option chapterprefix=true or appendixprefix=true, if I understand your question correctly.

A complete MWE, which (hopefully) solve all questions:

\documentclass[appendixprefix=true]{scrbook}
\usepackage{blindtext}
\usepackage{tocstyle}
\renewcommand*{\addchaptertocentry}[2]{%
  \addtocentrydefault{chapter}{\chapapp\nobreakspace #1}{#2}%
}

\usetocstyle{KOMAlike}

\begin{document}
\tableofcontents
\blinddocument
\appendix

\blinddocument

\end{document}

If you are using \chapter in \fronmatter, remember to use the starred version to avoid a Chapter . with empty number in your Table of Contents. Also, if you are using the command \addchapin you document, use the starred version (\addchap*}.

For further reading, see page 80 in the English manual, and this discussion at the KOMAscript homepage.

NB! This code needs a recent version of komascript.

Sveinung
  • 20,355
  • Thanks for help, but the MWE doesnt work and I got this "! LaTeX Error: \addchaptertocentry undefined." – Diaa Jul 07 '12 at 01:32
  • Strange, The MWE works on my system (updated MikTeX on Win 7). What is your version of koma-script? – Sveinung Jul 07 '12 at 07:44
  • After updating the Miktex 2.9 packages, the same error appears. Regarding the Koma version, here is the line from log file: Document Class: scrbook 2010/09/17 v3.07 KOMA-Script document class (book – Diaa Jul 07 '12 at 18:12
  • @Diaa Current version of komascript is 3.11a. My scrbook reports: scrbook 2012/05/15 v3.11 KOMA-Script document class (book). If your miktex that does not update your packages correctly, or maybe you have a copy of scrbook in your texmf-local (or whatever you have named your private TeX directory). The code need a recent version of komascript. – Sveinung Jul 07 '12 at 20:22
  • You have inspired me with the solution. First, I uninstalled the Koma bundle. Second, I deleted the directory "C:\Users<UserName>\AppData\Roaming\MiKTeX\2.9\tex\latex\koma-script". In case of compiling the file again, the bundle will be installed on the fly. Your help and patience are highly appreciated. – Diaa Jul 07 '12 at 22:26
  • I have irrelevant question, how did you get this syntax of overwriting the built-in code of the package ? I know it may be trivial question but I am very beginner in latex and I am highly interested in learning these ways of editing. – Diaa Jul 07 '12 at 22:32
  • @DiaaAbidou I have referenced my sources in the last paragraph in the answer, Komascript’s manual page 80 and a tread at Komascript’s homepage. See the last paragraph in my answer and the links. – Sveinung Jul 08 '12 at 09:08
  • Thanks, but now after re-installing Miktex 2.9, I get the references entry in TOC as "Chapter. References" and I am using the book option of "bibliography=totoc". Can you help me in removing this prefix of "Chapter" in case of bibliography ? – Diaa Jul 08 '12 at 23:16
  • 1
    I fixed it by removing the option "bibliography=totoc" and inserting "\addcontentsline{toc}{chapter}{References}" after \bibliography{<bibfile.bib>}. If available, can you tell me why does this awkward entry appear in case of using the class option ? – Diaa Jul 09 '12 at 00:36
  • @DiaaAbidou It because the reference is defined as a chapter heading. I am not familiar with bibliographies, but you solution seems correct. You can also try \renewcommand*{\chaptername}{} just after the \bibliography{<bibfile>} – Sveinung Jul 09 '12 at 11:16
  • Your solution can be valid, but it is not in my case unfortunately. Highly grateful for your time and help. – Diaa Jul 11 '12 at 19:30
  • @DiaaAbidou Are you using bibtex or biblatex to produce the bibliograpies? – Sveinung Jul 12 '12 at 12:01
  • Bibtex. I am sorry, but what the difference between them ? – Diaa Jul 13 '12 at 15:23
  • If you use \chapapp instead \chaptername the command \appendix changes the prefix automatically. Note that if there is a \chapter in frontmatter or a \addchap command in the document you will get Chapter . in front of the unnumbered chapter toc entry. You have to control if the first argument of \addchaptertocentry is empty. – esdd Feb 27 '16 at 20:41
  • @esdd You are correct regarding the \chapapp instead of \chaptername. I have updated my MWE. Your second problem is solved by using the starred version of \chapter and \addchap, see page 92 ff. in KOMAscript’s user manual. – Sveinung Feb 29 '16 at 22:10
  • @Sveinung Insert \addchap{test} in your MWE, run three times and then have a look at the TOC ;-) There will be "Chapter . test" or "Appendix . test". Or use \frontmatter followed by a \chapter command. Or use bibliography=totoc as mentioned in a comment above. See page 358 ff. in the manual. Also option listof=chapterentry (see page 130) does not work with your code. – esdd Mar 01 '16 at 08:15
  • @esdd As I said: Use \addchap* and \chapter* instead of \chapter and \addchap. bilbliography=totoc and listof=chapterentry was not part of the original question. I think it is a good idea to put in a test for the first argument as you suggest. – Sveinung Mar 01 '16 at 09:38
  • Note: This answer is IMHO out-dated, because tocstyle is not longer part of the KOMA-Script bundle. It is declared as deprecated. BTW: The package was always declared to be experimental and never reached a non-experimental state. – cabohah Jan 15 '24 at 09:34