0

I've been using this code to change the appearance of references (enclosing the number within round brackets):

\let\oldref\ref
\renewcommand{\ref}[1]{(\oldref{#1})}

After that, I wanted to make references "clickable" so I added the package hyperref using the hidelinks option:

\usepackage[hidelinks]{hyperref}

The links work fine but I'm wondering why the brackets are no longer visible. Obviously hyperref is redefining \ref but \let should update my definition too. This is independent of the order in which those instructions appear in the code. Where's the problem?

Heiko Oberdiek
  • 271,626
  • 1
    Is your redefinition inside the preamble or the document. If I recall, hyperref redefines a lot of things \AtBeginDocument. – Steven B. Segletes Sep 13 '16 at 16:34
  • Welcome to TeX.SX! Please help us help you and add a minimal working example (MWE) that illustrates your problem. Reproducing the problem and finding out what the issue is will be much easier when we see compilable code, starting with \documentclass{...} and ending with \end{document}. – egreg Sep 13 '16 at 16:36
  • What Steven B. Segletes wrote is very important: hyperref does really a lot of changes. The redefinition of \ref is not really recommended unless it is very clear what is requested –  Sep 13 '16 at 16:52

1 Answers1

0

Many redefinitions around \ref are delayed in \begin{document} (packages hyperref, nameref). Thus, the user redefinition should take place quite late, e.g. after \begin{document} or at the end of the preamble, delayed by \AtBeginDocument. Also, because of \DeclareRobustCommand, the redefinition is a little more complex. Package letltxmacro helps:

\documentclass{article}
\usepackage[hidelinks]{hyperref}
\usepackage{letltxmacro}

\AtBeginDocument{%
  \LetLtxMacro\MyOldRef\ref
  \DeclareRobustCommand*{\ref}[1]{%
    (\MyOldRef{#1})%
  }%
}

\begin{document}
\section{First section}\label{sec:first}
Ref: \ref{sec:first}
\end{document}

Result

BTW, see package amsmath for \eqref, which adds parentheses around equation numbers.

Heiko Oberdiek
  • 271,626