1

I would like my table of contents to show both sections and subsections for the main content but only sections for the appendices. I can achieve this with

\setcounter{tocdepth}{2}

and then using a new command specific for the subsections in the appendix:

\newcommand{\appendixsubsection}[1]{
  \stepcounter{subsection}
  \subsection*{\Alph{section}.\arabic{subsection}\hspace{1em}{#1}}
}

Based on this post. This gives the desired table of contents but when I want to reference a hidden appendix subsection, the section number is shown, instead of the subsection number. The following example might clarify what I mean.

\documentclass[a4paper]{article}
\usepackage{appendix}

\newcommand{\appendixsubsection}[1]{
    \stepcounter{subsection}
    \subsection*{\Alph{section}.\arabic{subsection}\hspace{1em}{#1}}
}

\begin{document}
\setcounter{tocdepth}{2}
\tableofcontents
\section{First section}
This is a reference to the appendix section: \ref{appendix_section}.
    \subsection{First subsection}
    This subsection should be shown in the table of contents.
    This is a reference to the first appendix subsection: \ref{appendix_subsection}. 
    This reference should be A.1.
        \subsubsection{First subsubsection}
        This is a refereece to the appendix subsubsection: \ref{appendix_subsubsection}
    
\appendix
\appendixpage
\addappheadtotoc
\section{Appendix section}
\label{appendix_section}
This is the appendix section
    \appendixsubsection{Appendix subsection}
    \label{appendix_subsection}
    This is the appendix subsection, it should not be shown in the table of contents.
        \subsubsection{Appendix subsubsection}
        \label{appendix_subsubsection}
        This is the appendix subsubsection
\end{document}

output of the code

I found multiple questions asking how to hide the subsections from the toc but I couldn't find a solution for the referencing.

Thank you in advance.

2
  • Here is one solution that could do the trick: stackoverflow.com/questions/3791950/… May 3, 2021 at 9:50
  • @ManuelPopp thanks for your response, that is the post that I referenced to and use in my example, except for changing \hiddensubsection in \appendixsubsection for clarity. It gives the referencing error as well.
    – debsim
    May 3, 2021 at 9:54

2 Answers 2

1

tocdepth is used by \contentsline (actually \@dottedtocline), which means it can be changed midway through the TOC using \addtocontents.

\documentclass[a4paper]{article}
\usepackage{appendix}

\begin{document}
\setcounter{tocdepth}{2}
\tableofcontents
\section{First section}
This is a reference to the appendix section: \ref{appendix_section}.
    \subsection{First subsection}
    This subsection should be shown in the table of contents.
    This is a reference to the first appendix subsection: \ref{appendix_subsection}. 
    This reference should be A.1.
        \subsubsection{First subsubsection}
        This is a refereece to the appendix subsubsection: \ref{appendix_subsubsection}
    
\appendix
\appendixpage
\addappheadtotoc
\addtocontents{toc}{\protect\setcounter{tocdepth}{1}}% This turns off subsections
\section{Appendix section}
\label{appendix_section}
This is the appendix section
    \subsection{Appendix subsection}
    \label{appendix_subsection}
    This is the appendix subsection, it should not be shown in the table of contents.
        \subsubsection{Appendix subsubsection}
        \label{appendix_subsubsection}
        This is the appendix subsubsection
\end{document}
1
  • That is a great solution, thanks!
    – debsim
    May 5, 2021 at 14:15
1

One solution would be to create a custom reference command \appref to reference those subsections, using \hyperref[sec:appendixsubsection]{\Alph{section}.\arabic{subsection}}. Note that you would have to add \usepackage{hyperref} to your preamble for this command.

The implementation could look like this:

% Add to preamble
\usepackage{hyperref}

\newcommand{\appendixsubsection}[1]{
    \stepcounter{subsection}
    \subsection*{\Alph{section}.\arabic{subsection}\hspace{1em}{#1}}
}
\newcommand{\appref}[1]{
\hyperref[#1]{\Alph{section}.\arabic{subsection}}
}

Then use \appref{appendix_subsection} to reference the subsection.

EDIT: This is probably a way better solution: Just use \refstepcounter{subsection} instead of \stepcounter{subsection} in the definition of the command, since \ref will then recognize a subsection was added. Like this:

\newcommand{\appendixsubsection}[1]{
    \refstepcounter{subsection}
    \subsection*{\Alph{section}.\arabic{subsection}\hspace{1em}{#1}}
}

Just add these 3 letters to the example that you've posted above and it will work.

1
  • Thanks for your answer, it does exactly what I wanted. I accepted John's answer since it doesn't require changing any of the existing references but yours works just as well.
    – debsim
    May 5, 2021 at 14:17

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.