1

Consider below MCWE:

\documentclass{memoir}
\usepackage{hyperref}
\hypersetup{colorlinks=true}
\hypersetup{linkcolor=[rgb]{0.2,0.4,0.7}}

\let\orgautoref\autoref
\providecommand{\Autoref}{\def{equationautorefname{Eq.}\orgautoref}
\renewcommand{\autoref}{\def\equationautorefname{eq.}\orgautoref}

\begin{document}
\frontmatter
\tableofcontents

\listoffigures

\mainmatter

\chapter{Bla}
\section{Bla}
\begin{figure}
 \caption{\Autoref{bla} \autoref{bla}\label{fig}}
\end{figure}
\autoref{fig}
\begin{equation}
bla\label{bla}
\end{equation}

\chapter{Bloo}
\section{Bloo}
\begin{figure}
 \caption{Bloo}
\end{figure}

\end{document}

If I comment out the \autoref redefinition above, everything works, but if I don't, a second run of pdflatex or latex gives this error:

! Missing control sequence inserted.
<inserted text> 
                \inaccessible 
l.2 ...quation{eq.}\autoref {bla}}}{1}{figure.1.1}

Please don't say `\def cs{...}', say `\def\cs{...}'.
I've inserted an inaccessible control sequence so that your
definition will be completed without mixing me up too badly.
You can recover graciously from this error, if you're
careful; see exercise 27.2 in The TeXbook.

The culprit is the \listoffigures, and the error refers to the .lof file, which contains the following:

\addvspace {10pt}
\contentsline {figure}{\numberline {1.1}{\ignorespaces Bla \def Equation{eq.}\autoref {bla}}}{1}{figure.1.1}
\addvspace {10pt}
\contentsline {figure}{\numberline {2.1}{\ignorespaces Bloo}}{3}{figure.2.1}

which seems wrong to me: the \autorefequationname ("Equation") has already been expanded instead of the \def command being executed. The documentclass doesn't really matter, it also occurs with e.g. book.

How can I solve this error? I have renamed all \autoref names and also introduced an \Autoref, so I really don't want to stop using it, unless there is no other way.

rubenvb
  • 3,883
  • 4
  • 26
  • 36
  • @Mico Yeah, that's the point, I need/want that bit (or equivalent). – rubenvb Apr 01 '15 at 13:57
  • Yeah, that's the point. I need that uncommented. – rubenvb Apr 01 '15 at 13:57
  • I guess I don't understand why you need to wrap the instruction \def\equationautorefname{eq.} inside a \renewcommand for \autoref. Why not just execute it by itself? – Mico Apr 01 '15 at 14:06
  • @Mico Right, because then the \Autoref command (which was missing from my original example) won't work anymore (see updated code). By writing both like this, it magically works. I understand it's a bit of a hack, but I didn't find any other way of doing this. – rubenvb Apr 01 '15 at 14:22
  • Hmm, it seems I can work around this issue by using \caption's optional argument for a short caption, which is then placed in the list of figures instead. If I avoid \autoref there, I should be fine. A better solution is still welcome though! – rubenvb Apr 01 '15 at 14:37
  • I've just posted a solution that uses the cross-referencing powers of the cleveref package. Hope it works for you. For (much) more information about the cross-referencing capabilities of the hyperref and cleveref packages see, e.g., the posting Cross-reference packages: which to use, which conflict? – Mico Apr 01 '15 at 14:40
  • 1
    The definition of \Autoref is fragile, it contains an assignment, thus \protect\Autoref is needed in moving arguments as in \caption. – Heiko Oberdiek Apr 01 '15 at 14:41

1 Answers1

3

Rather than hack the \autoref macro, I would recommend you load the cleveref package with the option nameinlink. That way, all call-outs generated by \cref and \Cref -- the former for lowercase names, the latter for upper case names -- will appear like the call-outs generated by \autoref. In your editing software, do a global change of \autoref to \cref and of \Autoref to \Cref, and you'll be all set.

The LoF from your example code would look like this:

enter image description here

\documentclass{memoir}
\usepackage{hyperref}
\hypersetup{colorlinks=true}
\hypersetup{linkcolor=[rgb]{0.2,0.4,0.7}}

\usepackage[nameinlink,noabbrev]{cleveref}
\crefname{equation}{eq.}{eqs.} % force abbreviated forms for equation "names"
\Crefname{equation}{Eq.}{Eqs.}

\begin{document}
\frontmatter
\tableofcontents

\listoffigures
\mainmatter

\chapter{Bla}
\section{Bla}
\begin{figure}
 \caption{\Cref{bla} \cref{bla} \label{fig}}
\end{figure}
\cref{fig}
\begin{equation}
bla\label{bla}
\end{equation}

\chapter{Bloo}
\section{Bloo}
\begin{figure}
 \caption{Bloo}
\end{figure}

\end{document}
Mico
  • 506,678