[Tex/LaTex] How to redefine \ref as \eqref

macros

I would like to redefine \ref as \eqref, but \renewcommand{\ref}{\eqref} doesn't work. I would guess this is hapenning because \eqref is internally defined as \newcommand{\eqref}{(\ref)}, but I'm not sure about it. Any advice?

Best Answer

If you really want to do this, in amsmath.sty, \eqref is defined as

\renewcommand{\eqref}[1]{\textup{\tagform@{\ref{#1}}}}

so you could say

\documentclass{article}
\usepackage{amsmath}

\makeatletter
\let\origref\ref
\renewcommand{\ref}[1]{\textup{\tagform@{\origref{#1}}}}
\makeatother

\begin{document}

\section{Test}\label{a}
Section~\ref{a}? Equation~\ref{b}
\begin{equation}\label{b}
a=b
\end{equation}

\end{document}

enter image description here

Notice the inconsistencies that the redefinition introduces; you are cross-referencing section (1), but your document has section 1; a similar problem will appear for floats, and other objects in your document.

Adendum

Perhaps, if you are interested in redefining the cross-reference format for a particular type of object, the cleveref package could be a better option; a little example in which I changed the formatting for cross-referencing figures, leaving all other cross.references unalatered:

\documentclass{article}
\usepackage{amsmath}
\usepackage{cleveref}

\crefformat{figure}{figure~(#2#1#3)}
\Crefformat{figure}{Figure~(#2#1#3)}

\begin{document}

\section{Test}\label{a}
Section~\ref{a}. Equation~\ref{b}. \Cref{c}? or \cref{c}?
\begin{equation}\label{b}
a=b
\end{equation}
\begin{figure}
\centering
A
\caption{test figure}
\label{c}
\end{figure}

\end{document}

enter image description here

Related Question