13

First off, I'm very grateful of the help this community has provided me so far. Anyway, to my problem:

I feel like I'm missing something obvious here. I want...

\documentclass{article}

\usepackage{hyperref} 
\renewcommand{\ref}{\autoref}

\begin{document}
\section{Section Title} \label{anchor}
Blah Blah Blah. \ref{anchor}
\end{document}

to print as...

1 Section Title

Blah Blah Blah. section 1

but instead I get ...

1 Section Title

Blah Blah Blah. 1

... so \ref is not being replaced with \autoref. How can I correct this? Do I have no option but to do a find-and-replace to change \ref to \autoref?

2
  • Hmmm... Interesting.... I tried everything I know from \renewcommand, \def, \let, and \LetLtxMacro, but was not able to get \ref to produce the same output as \autoref. Dec 2, 2012 at 5:56
  • 1
    I suggest you not to try this; \ref and \autoref are semantically different and using one or the other changes how the text is laid out.
    – egreg
    Dec 2, 2012 at 10:43

1 Answer 1

18

Ok, figured out a solution:

enter image description here

Notes:

  • None of the usual tricks of using:

    1. \renewcommand{\ref}[1]{\autoref{#1}}
    2. \def\ref#1{\autoref{#1}}
    3. \let\ref\autoref
    4. \LetLtxMacro\ref\autoref

    seem to work in this case.

    I suspect it is because hyperref does an \AtBeginDocument, so that some things get defined at the the start of the document. So using \AtBeginDocument puts our redefinition at the end of queue of commands that get executed \AtBeginDocument.

    And since placing the \AtBeginDocument to be before \usepackage{hyperref} does not work, this confirms that the suspicion was correct.

Code:

\documentclass{article}

\usepackage{hyperref} 


\AtBeginDocument{\renewcommand{\ref}[1]{\autoref{#1}}}

\begin{document}
\section{Section Title} \label{anchor}
Blah Blah Blah.  \autoref{anchor}  \ref{anchor}
\end{document}
3
  • 1
    Or simply put the command after \begin{document}
    – user11232
    Dec 2, 2012 at 6:02
  • @HarishKumar: But then that would be required in each document. Putting in the preamble is better in my opinion. Dec 2, 2012 at 6:06
  • @PeterGrill: \ref is defined \Hy@AtBeginDocument - similar to \AtBeginDocument.
    – Werner
    Dec 2, 2012 at 6:22

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.