7

MWE

\documentclass{article}
\usepackage{appendix}
\usepackage{lipsum}
\usepackage[colorlinks]{hyperref}

\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}

Output

enter image description here

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.

  • 4
    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
6

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}
\usepackage[colorlinks]{hyperref}

\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}

\begin{equation}
a^2 + b^2 = c^2\label{eqn:main}
\end{equation}

\begin{subappendices}

\subsection{foofirstappendix}
\label{sub}

\begin{equation}
e = mc^2\label{eqn:sub}
\end{equation}

\end{subappendices}

\section{barfirst}
\label{aftersub}

\begin{equation}
g^2 + h^2 = i^2\label{eqn:aftersub}
\end{equation}

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

\end{document}

Output

  • Any way to achieve this without etoolbox? – Rhys Ulerich Jul 5 '14 at 0:02
  • 1
    @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

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.