1

I tried to use this solution to make partial ToC in my document, but when I added my appendix, it changed the behavior of the appendix package. In spite of putting \usepackage[page,toc,titletoc,title]{appendix}, "Appendices" and "Appendix A" do not show at the ToC.

The suggested solution is not providing the desired output, as I would like the Appendices to show in the partial ToC and not in the general ToC.

I think the problem is related with \tocbasic@addxcontentsline or \listoftoc{\ext@parttoc}. The .aux file does differentiate between .toc and .tcpI, but it discriminates the \contentsline {chapter}{Appendices}{9}{}\protected@file@percent and \contentsline {chapter}{Appendix \numberline {A}Chapter appendix}{11}{}\protected@file@percent, leaving the first one without entry in the .tcpI and it does not display what is before \numberline in the second.

Is there any way to change this behavior? TIA.

MWE

\documentclass{scrbook}
\usepackage[page,toc,titletoc,title]{appendix}
\usepackage{xpatch}

\usepackage{blindtext}

% Partial TOC
\makeatletter
\newif\ifuseparttoc
\newcommand*{\parttoc}[1][\thepart]{% new command to generate and show a chapter toc
\useparttoctrue% switch on part-toc-entries
\edef\ext@parttoc{tcp#1}% extension of the part-toc-file, e.g., tcpI
\DeclareNewTOC[
    listname=My Content,
    %unset=onecolumn% if the part toc should use twocolumn
]{\ext@parttoc}% declare a new toc file
\begingroup
    \value{tocdepth}=\chaptertocdepth% we want entries down to chapter
    \listoftoc{\ext@parttoc}% show the toc with header
\endgroup
}
\xapptocmd\addtocentrydefault{% patch the KOMA-Script's generic toc entry generator
\ifuseparttoc% if part toc entries should be generated
    \expandafter\tocbasic@addxcontentsline\expandafter{\ext@parttoc}{#1}{#2}{#3}% do it
\fi
}{}{}
\xpretocmd\part{\useparttocfalse}{}{}% automatically switch of part toc entries at start of every \part
\makeatother

\setcounter{tocdepth}{\partnumdepth}% depth of TOC


\begin{document}
    \tableofcontents
    \part{PART 1}
    \parttoc
    \chapter{Something}
    \blindtext
    \begin{appendices}
        \chapter{Chapter appendix}
        \blindtext
    \end{appendices}
    \chapter{Non appendix chapter}
\end{document}

After some research, I've found this other solution which approaches the same problem from a different perspective. Despite trying to understand the code, I have failed to make the appropriate changes to make it work as a partial ToC for each \part. This is another MWE

\documentclass{scrbook}
\usepackage[page,toc,titletoc,title]{appendix}
\usepackage{etoolbox}
\usepackage{catchfile}
\usepackage{blindtext}

% Works with or without hyperref.
\usepackage{hyperref}

\makeatletter
  % Macros for resetting and appending to the variables that store the part tocs.
  \def\@parttoc@toc@reset{%
    \expandafter\gdef\csname @parttoc@toc@\@parttoc@part@num\endcsname{}%
  }
  \def\@parttoc@toc@append{%
    \expandafter\gappto\csname @parttoc@toc@\@parttoc@part@num\endcsname
  }
  % The parsing macro, that goes through the toc file and creates the part toc macros.
  \def\@parttoc@parse@tocfile{%
    % We only need to do this once.
    \let\@part@parse@tocfile\relax
    \begingroup
      \def\@parttoc@part@num{0}%
      \@parttoc@toc@reset
      \makeatletter
      \IfFileExists{\jobname.toc}{%
        \CatchFileDef\@tmpa{\jobname.toc}{}%
        \expandafter\@parttoc@parse@head
        \@tmpa
        \contentsline\@parttoc@parse@stop%
      }{}%
    \endgroup
  }
  \def\@parttoc@part@string{part}
  \def\@parttoc@parse@stop{\@parttoc@parse@stop}%
  \long\def\@parttoc@parse@head#1\contentsline#2{%
    \@parttoc@toc@append{#1}%
    \ifx\@parttoc@parse@stop#2%
      \expandafter\@gobble
    \else
      \begingroup
      \def\@tmpa{#2}%
      \ifx\@tmpa\@parttoc@part@string
        \endgroup
        \expandafter\expandafter
        \expandafter\@parttoc@parse@cline@part
      \else
        \endgroup
        \expandafter\expandafter
        \expandafter\@parttoc@parse@cline@other
      \fi
    \fi
    {#2}%
  }
  \def\@parttoc@parse@cline@other{%
    \@parttoc@toc@append{\contentsline}%
    \@parttoc@parse@head
  }
  \def\@parttoc@parse@cline@part#1#2#3{%
    \begingroup
      \def\numberline##1##2\@nil{%
        \endgroup
        \def\@parttoc@part@num{##1}%
      }%
    #2\@nil
    \@parttoc@toc@reset
    \expandafter\@parttoc@parse@head\@parttoc@parse@gobble@hyperrefargs
  }
  % A macro for printing the current part toc.
  \def\@parttoc@toc@print{%
    \begingroup
      \setcounter{tocdepth}{\partocdepth}%
      \parindent 0pt%
      \chapter*{\contentsname}%
      \csname @parttoc@toc@\arabic{part}\endcsname
      \@parttoc@toc@afterhook
    \endgroup
  }
  \newcommand*\partocdepth{3}
  \def\@parttoc@toc@afterhook{}
  \newcommand\parttocafter[1]{%
    \def\@parttoc@toc@afterhook{#1}%
  }
  % Set everything up.
  \AtBeginDocument{%
    % When hyperref is loaded, \contentsline has one more argument.
    \@ifpackageloaded{hyperref}{%
      \protected@write\@auxout{}{\let\protect\@parttoc@parse@gobble@hyperrefargs\protect\@gobble}%
    }{%
      \protected@write\@auxout{}{\let\protect\@parttoc@parse@gobble@hyperrefargs\protect\@empty}%
    }%
    % When the aux file is read in the first time, the toc file will be parsed.
    \protected@write\@auxout{}{%
        \protect\@parttoc@parse@tocfile
    }%
    % Automatically print the part toc after each part title.
    \apptocmd\scr@@startpart{\@parttoc@toc@print}{}{}%
  }
\makeatother

\begin{document}
\tableofcontents
\part{PART 1}
\chapter{Something}
\blindtext
\begin{appendices}
  \chapter{Chapter appendix}
  \blindtext
\end{appendices}
\chapter{Non appendix chapter}
\end{document}
2
  • Try \setcounter{tocdepth}{3} Sep 11 at 20:53
  • This adds the appendix in the main ToC but I would like it in the partial ToC. Thank you anyways!
    – Ana
    Sep 11 at 20:59

1 Answer 1

1

Redefine \addappheadtotoc (provided by package appendix) to get the entry »Appendices« in the partial TOC:

\renewcommand{\addappheadtotoc}{% original definition see package appendix
  \phantomsection
  \addchaptertocentry{}{\appendixtocname}%
}

For the prefix »Appendix« in TOC redefine KOMA-Script command \addchaptertocentry inside the appendices environment:

\xapptocmd{\appendices}{\useappendixprefixintoc}{}{}
\newcommand{\useappendixprefixintoc}{
  \let\originaladdchaptertocentry\addchaptertocentry
  \renewcommand\addchaptertocentry[2]{%
    \IfArgIsEmpty{##1}
      {\originaladdchaptertocentry{##1}{##2}}
      {\originaladdchaptertocentry{}{\appendixname~##1\protect\autodot\ ##2}}
  }%
}

Example:

\documentclass{scrbook}
\usepackage[page,toc]{appendix}
\usepackage{xpatch}

\usepackage{blindtext}

% Partial TOC
\makeatletter
\newif\ifuseparttoc
\newcommand*{\parttoc}[1][\thepart]{% new command to generate and show a chapter toc
\useparttoctrue% switch on part-toc-entries
\edef\ext@parttoc{tcp#1}% extension of the part-toc-file, e.g., tcpI
\DeclareNewTOC[
    listname=My Content,
    %unset=onecolumn% if the part toc should use twocolumn
]{\ext@parttoc}% declare a new toc file
\begingroup
    \value{tocdepth}=\chaptertocdepth% we want entries down to chapter
    \listoftoc{\ext@parttoc}% show the toc with header
\endgroup
}
\xapptocmd\addtocentrydefault{% patch the KOMA-Script's generic toc entry generator
\ifuseparttoc% if part toc entries should be generated
    \expandafter\tocbasic@addxcontentsline\expandafter{\ext@parttoc}{#1}{#2}{#3}% do it
\fi
}{}{}
\xpretocmd\part{\useparttocfalse}{}{}% automatically switch of part toc entries at start of every \part
\makeatother

\setcounter{tocdepth}{\partnumdepth}% depth of TOC


\renewcommand{\addappheadtotoc}{% original definition see package appendix
  \phantomsection
  \addchaptertocentry{}{\appendixtocname}%
}
\xapptocmd{\appendices}{\useappendixprefixintoc}{}{}
\newcommand{\useappendixprefixintoc}{
  \let\originaladdchaptertocentry\addchaptertocentry
  \renewcommand\addchaptertocentry[2]{%
    \IfArgIsEmpty{##1}
      {\originaladdchaptertocentry{##1}{##2}}
      {\originaladdchaptertocentry{}{\appendixname~##1\protect\autodot\ ##2}}
  }%
}

\begin{document}
\tableofcontents
\part{PART 1}
\parttoc
\chapter{Something}
\blindtext
\begin{appendices}
  \chapter{Chapter appendix}
  \blindtext
\end{appendices}
\chapter{Non appendix chapter}
\end{document}

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.