# Hide appendix subsections from toc while keeping the reference

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 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
\section{Appendix section}
\label{appendix_section}
This is the appendix section
\appendixsubsection{Appendix subsection}
\label{appendix_subsection}
\subsubsection{Appendix subsubsection}
\label{appendix_subsubsection}
This is the appendix subsubsection
\end{document}


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

• 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. May 3, 2021 at 9:54

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 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
\section{Appendix section}
\label{appendix_section}
This is the appendix section
\subsection{Appendix subsection}
\label{appendix_subsection}
\subsubsection{Appendix subsubsection}
\label{appendix_subsubsection}
This is the appendix subsubsection
\end{document}

• That is a great solution, thanks! May 5, 2021 at 14:15

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.

• 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. May 5, 2021 at 14:17