4

I have

\newcommand*{\appendixmore}{%
\renewcommand*{\sectionformat}{%
\appendixname~\thesection\autodot\enskip}%
\renewcommand*{\sectionmarkformat}{%
\appendixname~\thesection\autodot\enskip}}

from the KOMA-script p. 517, causes the word "Appendix" to appear before the sections in the appendix.

How do I achieve this in the TOC?

1. A Section
Appendix A. A Section in the Appendix
            A.1. A Subsection in the Appendix 
Appendix B. Another Section in the Appendix

MWE:

enter image description here

\documentclass[english]{scrartcl} 
\usepackage{babel}

\newcommand*{\appendixmore}{%
\renewcommand*{\sectionformat}{%
\appendixname~\thesection\autodot\enskip}%
\renewcommand*{\sectionmarkformat}{%
\appendixname~\thesection\autodot\enskip}}

\begin{document}
\tableofcontents   
 \section{A Section} ...
 \appendix       
\section{A Section in the Appendix} ...
\subsection{A Subsection in the Appendix} ...
\section{Another Section in the Appendix} ...
\end{document}
haver
  • 353

3 Answers3

4
\documentclass[english]{scrartcl}
\usepackage{babel}
\usepackage{xparse}

\NewDocumentCommand\setappendixprefix{}
{% 
 \DeclareTOCStyleEntry[entrynumberformat=\appendixname~,dynnumwidth=true]{default}{section}
}

\newcommand*{\appendixmore}{%
   \renewcommand*{\sectionformat}{%
      \appendixname~\thesection\autodot\enskip}%
   \renewcommand*{\sectionmarkformat}{%
      \appendixname~\thesection\autodot\enskip}
   \addtocontents{toc}{\setappendixprefix}
   }


\begin{document}
\tableofcontents
 \section{A Section} ...
 \appendix
\section{A Section in the Appendix} ...
\subsection{A Subsection in the Appendix} ...
\section{Another Section in the Appendix} ...

\end{document}

enter image description here

Ulrike Fischer
  • 327,261
  • Ah, ok. I think, there is a problem with "A.1. ..." this should appear in under "A Section...", like the other toc-entries. I wrote down my ilustration wrong.

    1. A Section Appendix A. A Section in the Appendix A.1. A Subsection in the Appendix Appendix B. Another Section in the Appendix

    – haver Nov 08 '19 at 16:37
3

Update:

Since KOMA-Script version 3.31 option dynindent can be used in the optional argument of \DeclareTOCStyleEntries:

\documentclass[english]{scrartcl}[2020/07/22]% needs version 3.31 or newer
\usepackage{blindtext}% only for dummy text
\usepackage{babel}
\usepackage{xparse}
\usepackage{calc}

\newcommand\useprefix[2]{#1#2}

\NewDocumentCommand\appendixprefixintoc{} {% \DeclareTOCStyleEntry [% entrynumberformat=\useprefix{\appendixname~},% add the prefix before the entrynumber dynnumwidth ]{default}{section} \DeclareTOCStyleEntries[% dynindent ]{default}{subsection,subsubsection,paragraph,subparagraph} }

\newcommand{\appendixmore}{% \renewcommand{\sectionformat}{% \appendixname~\thesection\autodot\enskip}% \renewcommand*{\sectionmarkformat}{% \appendixname~\thesection\autodot\enskip}% \addtocontents{toc}{\appendixprefixintoc}% }

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

Run three times to get the same result as in my original answer (see below).


Original answer: Here is a suggestion that needs KOMA-Script version 3.27 or newer:

\documentclass[english]{scrartcl}[2019/10/13]% needs version 3.27 or newer
\usepackage{blindtext}% only for dummy text
\usepackage{babel}
\usepackage{xparse}
\usepackage{calc}

\newcommand\useprefix[2]{#1#2} \newlength\appendixprefixwidth

\NewDocumentCommand\appendixprefixintoc{} {% \setlength\appendixprefixwidth{% \widthof{\usekomafont{sectionentry}\appendixname~}}% measure needed additional space \DeclareTOCStyleEntry [% entrynumberformat=\useprefix{\appendixname~},% add the prefix before the entrynumber numwidth+=\appendixprefixwidth% enlarge numwidth for level section ]{default}{section} \DeclareTOCStyleEntries[% indent+=\appendixprefixwidth% enlarge indent for other levels ]{default}{subsection,subsubsection,paragraph,subparagraph} }

\newcommand{\appendixmore}{% \renewcommand{\sectionformat}{% \appendixname~\thesection\autodot\enskip}% \renewcommand*{\sectionmarkformat}{% \appendixname~\thesection\autodot\enskip}% \addtocontents{toc}{\appendixprefixintoc}% }

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

enter image description here

esdd
  • 85,675
  • Very nice! .................... – haver Nov 09 '19 at 10:13
  • How do I make it work for scrreport? I get the error Package scrkbase Error: font of elementsectionentry' can't be used. \appendixprefixintoc` – homocomputeris Nov 02 '20 at 14:05
  • 1
    @homocomputeris With scrreprt the appendices are chapters. scrreprt does not provide the elment sectionentry but chapterentry. You can also ask a new question including a MWE for scrreprt. – esdd Nov 04 '20 at 20:29
0

The add "Appendix" to chapters instead:

\newcommand\useprefix[2]{#1#2}

\NewDocumentCommand\appendixprefixintoc{} {% \DeclareTOCStyleEntry [% entrynumberformat=\useprefix{\appendixname~},% add the prefix before the entrynumber dynnumwidth ]{default}{chapter} \DeclareTOCStyleEntries[% dynindent ]{default}{chapter,subsection,subsubsection,paragraph,subparagraph} }

\newcommand{\appendixmore}{% \renewcommand{\chapterformat}{% \appendixname~\thechapter\autodot\enskip}% \renewcommand*{\chaptermarkformat}{% \appendixname~\thechapter\autodot\enskip}% \addtocontents{toc}{\appendixprefixintoc}% }

adelks
  • 1