Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question

3 Answers 3

up vote 14 down vote accepted

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

share|improve this answer
    
I take your point. I wanted to prevent from having references to equations in both forms (1) and 1 . I think it's better to use \eqref for references now. –  Honza Zubáč Jul 14 '13 at 0:58

The macro \p@equation can be used to change the equation references of \ref. Usually \p@<counter> is used to add a prefix to the reference of counter <counter> and does not take an argument. But with an additional \expandafter, the counter value can be catched as simple argument:

\documentclass{article}
\usepackage{amsmath}

\makeatletter
\renewcommand*{\p@equation}{%
  \expandafter\@p@equation
}
\newcommand*{\@p@equation}[1]{%
  \textup{\tagform@{#1}}% see definition of `\eqref`
}
\makeatother

\begin{document}
\section{Hello}\label{sec:hello}
\begin{equation}
  \label{eq:einstein}
  E=mc^2
\end{equation}
References: Section \ref{sec:hello} and equation \ref{eq:einstein}.
\end{document}

Result

Drawback: The reference \ref should not be used inside \tag, otherwise the result is duplicated parentheses.

share|improve this answer

You can selectively define the format of \ref using the fncylab package, which is equivalent to (but more packaged than) Heiko's answer. For example:

\usepackage{fncylab}
\labelformat{equation}{(#1)}
\section{A section}\label{s:section}
\[ \label{eq:eqn} ... \]
\ref{s:section}\ref{eq:eqn}

will print the equation number in parentheses but the section number normally. I have a standing preamble to define \labelformat{section}{Section~#1} and so on.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.