2

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"?

1

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

  • Perfect! Hadn't thought of this. It also works to reset the counter after my appendices (there are some declarations following the appendix that again I want to appear in the TOC). Note that I need to use \addtocontents{toc}{\protect\setcounter{tocdepth}{...}} because I have a \usepackage{calc} in my document, which clashes with your originally proposed solution. – bers Mar 26 '15 at 9:11
  • @bers glad it helped and that you could fix the impact of package calc on \setcounter. – user4686 Mar 27 '15 at 8:26
2

Adding the following lines in the preamble makes all the things you want automatic:

\makeatletter
\g@addto@macro\appendix{%
\def\@schapter#1{%
  \begingroup
    \let\@mkboth\@gobbletwo
    \Hy@MakeCurrentHrefAuto{\Hy@chapapp*}%
    \Hy@raisedlink{%
      \hyper@anchorstart{\@currentHref}\hyper@anchorend
    }%
  \endgroup
  \addcontentsline{toc}{chapter}{#1}\H@old@schapter{#1}%
}
\def\Hy@org@chapter[#1]#2{\ifnum \c@secnumdepth >\m@ne
      \if@mainmatter
        \refstepcounter{chapter}%
        \typeout{\@chapapp\space\thechapter.}%
      \fi
   \fi
   \chaptermark{#1}%
   \if@twocolumn
     \@topnewpage[\@makechapterhead{#2}]%
   \else
     \@makechapterhead{#2}%
     \@afterheading
   \fi}
}
\makeatother

MWE:

\documentclass{book}
\usepackage{hyperref}

\makeatletter
\g@addto@macro\appendix{%
\def\@schapter#1{%
  \begingroup
    \let\@mkboth\@gobbletwo
    \Hy@MakeCurrentHrefAuto{\Hy@chapapp*}%
    \Hy@raisedlink{%
      \hyper@anchorstart{\@currentHref}\hyper@anchorend
    }%
  \endgroup
  \addcontentsline{toc}{chapter}{#1}\H@old@schapter{#1}%
}
\def\Hy@org@chapter[#1]#2{\ifnum \c@secnumdepth >\m@ne
      \if@mainmatter
        \refstepcounter{chapter}%
        \typeout{\@chapapp\space\thechapter.}%
      \fi
   \fi
   \chaptermark{#1}%
   \if@twocolumn
     \@topnewpage[\@makechapterhead{#2}]%
   \else
     \@makechapterhead{#2}%
     \@afterheading
   \fi}
}
\makeatother

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

Output:

enter image description here

  • Thanks! It does work, even in my 120 pages, 40 packages document. However, the answer by @jfbu, though later, is so much more straightforward. Thanks anyway! – bers Mar 26 '15 at 9:07

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.