[Tex/LaTex] Include an unnumbered appendix chapter in TOC and hide numbered appendices from TOC

appendicesnumberingtable of contents

I have a few appendices that I do not want to show in the TOC. Instead, in the TOC, I want a single entry named "AllAppendices" to be shown. This shall be an (unnumbered) chapter in the appendix. The real appendices shall be (numbered) chapters, but again, not in the TOC.

These two versions I have tried:

\documentclass{book}
\usepackage{hyperref}
\newcommand{\nocontentsline}[3]{}
\newcommand{\tocless}[2]{\bgroup\let\addcontentsline=\nocontentsline#1{#2}\egroup}
%\renewcommand{\tocless}[2]{\bgroup\let\addcontentsline=\nocontentsline#1{#2}\egroup\addtocounter{chapter}{-1}\refstepcounter{chapter}}
\begin{document}
\tableofcontents

I want to be able to reference FirstAppendix: \ref{FirstAppendix}, \nameref{FirstAppendix}
\chapter{LastRealChapter}
\label{LastRealChapter}
\chapter*{AllAppendices (can be added to TOC manually)}
\appendix
\label{AllAppendices}
\cleardoublepage
\tocless
\chapter{FirstAppendix (numbered, but not in TOC)}
\label{FirstAppendix}
\end{document}

What I get is this:

  • nameref never works correctly, it references "AllAppendices"
  • ref works in the second version, but not in the first
  • I get a warning in the second version: "destination with the same identifier (name{appendix.A}) has been already used, duplicate ignored"

So my questions are:

  • How to do it properly?
  • What are the implications of \addtocounter{chapter}{-1}\refstepcounter{chapter}?

The MWE can be simplified:

\documentclass{book}
\usepackage{hyperref}
\newcommand{\nocontentsline}[3]{}
\newcommand{\tocless}[2]{\bgroup\let\addcontentsline=\nocontentsline#1{#2}\egroup}
\renewcommand{\tocless}[2]{\bgroup\let\addcontentsline=\nocontentsline#1{#2}\egroup\addtocounter{chapter}{-1}\refstepcounter{chapter}}
\begin{document}
    \tableofcontents
    I want to be able to reference Appendix like this: \ref{Appendix}, \nameref{Appendix}
    \chapter*{SomethingElse}
    \cleardoublepage
    \appendix
    \tocless\chapter{Appendix (numbered, but not in TOC)}
    \label{Appendix}
\end{document}

Commenting out the \cleardoublepage seems to help, at least for \ref.

Edit: And it does work in my document. So is the bottom line "no \cleardoublepage before \tocless chapters"?

Best Answer

You don't need complicated patches. Just insert in the .toc file at suitable location a change to the tocdepth counter.

\documentclass{book}
\usepackage{hyperref}
\begin{document}
\tableofcontents

% added in edit: (hyperref takes into account the current value
% of tocdepth, it was set to -3 during execution of \tableofcontents
% hence we must reset it; alternative: the option bookmarksdepth
% of hyperref -- see its documentation in the README of hyperref !)

\setcounter{tocdepth}{2} % (downto subsections)

% 
\vspace{1cm} 

I want to be able to reference FirstAppendix: \ref{FirstAppendix},
\nameref{FirstAppendix} as well as SecondAppendix: \ref{SecondAppendix},
\nameref{SecondAppendix}.


\chapter{LastRealChapter}
\label{LastRealChapter}
\appendix

\chapter*{AllAppendices}
\addcontentsline{toc}{chapter}{AllAppendices}


\addtocontents{toc}{\setcounter{tocdepth}{-3}}

\chapter{FirstAppendix (numbered, but not in TOC)}
\label{FirstAppendix}


\chapter{SecondAppendix (numbered, but not in TOC)}
\label{SecondAppendix}
\end{document}

table of contents