# MWE

\documentclass{article}
\usepackage{appendix}
\usepackage{lipsum}

\def\sectionautorefname{Section}
\def\subsectionautorefname{Section}
\def\appendixautorefname{Appendix}

\begin{document}

First appendix section: \autoref{app sec}

First appendix subsection: \autoref{app sub}

\appendix
\appendixpage
\section{First appendix section}\label{app sec}
\subsection{First appendix subsection}\label{app sub}
\lipsum[1]

\end{document}


# Question

Is it possible to \autoref a subsection in an appendix in the form Appendix A.1, instead of Section A.1 (as shown in the MWE)?

I'm aware a cleveref solution given in this post. If possible, however, I'd like a solution that only involves the hyperref package.

• I'd go with cleveref. – egreg Dec 13 '13 at 9:00
• @egreg: Is there no way to (re)define \subsectionautorefname conditional on whether it's in the appendix or not? – Herr K. Dec 13 '13 at 9:56
• One should redefine how hyperref makes its notes in the .aux file. Try and see how it does for changing from section to appendix; one should extend this also for lower levels. – egreg Dec 13 '13 at 13:13

The following applies the appendix name to all section-type labels that sit under \appendix or in a subappendices environment. Part of the example code borrowed from “section level” appendices. I've also added some referenced equations to show that these should keep their usual names.

\documentclass{article}
\usepackage{appendix}
\usepackage{lipsum}
\usepackage{geometry}

\def\sectionautorefname{Section}
\def\subsectionautorefname{Section}
\def\appendixautorefname{Appendix}

% begin appendix autoref patch [\autoref subsections in appendix](https://tex.stackexchange.com/questions/149807/autoref-subsections-in-appendix)
\usepackage{etoolbox}
\makeatletter
\patchcmd{\hyper@makecurrent}{%
\ifx\Hy@param\Hy@chapterstring
\let\Hy@param\Hy@chapapp
\fi
}{%
\iftoggle{inappendix}{%true-branch
% list the names of all sectioning counters here
\@checkappendixparam{chapter}%
\@checkappendixparam{section}%
\@checkappendixparam{subsection}%
\@checkappendixparam{subsubsection}%
\@checkappendixparam{paragraph}%
\@checkappendixparam{subparagraph}%
}{}%
}{}{\errmessage{failed to patch}}

\newcommand*{\@checkappendixparam}[1]{%
\def\@checkappendixparamtmp{#1}%
\ifx\Hy@param\@checkappendixparamtmp
\let\Hy@param\Hy@appendixstring
\fi
}
\makeatletter

\newtoggle{inappendix}
\togglefalse{inappendix}

\apptocmd{\appendix}{\toggletrue{inappendix}}{}{\errmessage{failed to patch}}
\apptocmd{\subappendices}{\toggletrue{inappendix}}{}{\errmessage{failed to patch}}
% end appendix autoref patch

\begin{document}

First main section: \autoref{main sec} (sectionautorefname).

Subappendix: \autoref{sub}.

Second main section: \autoref{aftersub}.

First appendix section: \autoref{app sec} (appendixautorefname).

First appendix subsection: \autoref{app sub} (appendixautorefname).

Main equation: \autoref{eqn:main}.

Subappendix equation: \autoref{eqn:sub}.

Following equation: \autoref{eqn:aftersub}.

Appendix equation: \autoref{eqn:app}.

\section{foofirst}
\label{main sec}

$$a^2 + b^2 = c^2\label{eqn:main}$$

\begin{subappendices}

\subsection{foofirstappendix}
\label{sub}

$$e = mc^2\label{eqn:sub}$$

\end{subappendices}

\section{barfirst}
\label{aftersub}

$$g^2 + h^2 = i^2\label{eqn:aftersub}$$

\appendix
\appendixpage
\section{First appendix section}\label{app sec}
\subsection{First appendix subsection}\label{app sub}
\lipsum[1]
$$e=mc^2 \label{eqn:app}$$

\end{document}


• Any way to achieve this without etoolbox? – Rhys Ulerich Jul 5 '14 at 0:02
• @RhysUlerich: Yes, but it is not as robust to new releases of hyperref. Simply find the definition \def\hyper@makecurrent in hyperref.sty, copy and paste in place of the \patchcmd and perform the replacement yourself. If you get stuck, let me know. There are also other packages providing commands that can do the same job as \patchcmd. Perhaps you could explain why it is that you wish to avoid etoolbox, as I understand that to be a fairly common and robust package? – cyberSingularity Jul 5 '14 at 7:14
• Is there a way to make this work with memoir? – rubenvb Mar 4 '15 at 16:09
• @rubenvb: I don't immediately see any problem using this with memoir. Can you clarify what you have tried and what has gone wrong? – cyberSingularity Mar 4 '15 at 20:38
• @cyberSingularity You're right, it works just fine. I was using the appendices environment but forgot to add \appendix before that. Thanks for this great solution, btw! – rubenvb Mar 5 '15 at 10:32