15

I want to edit the \eqref command so that \eqref will produce Eq. (1), for example, instead of (1). Of course, I could define

\newcommand{\reff}{Eq.~\eqref}

but then I lose my editor's ability to present a list of former labelled equations when encountering the \eqref command (I am using Inlage).

So I was wondering if it's possible to refer to a previous command definition, without re-defining it, and just append the changes. something in the form

\renewcommand{\eqref}{Eq.~[old_\eqref definition]}

Hope it's clear.

19

There's a "standard" method:

\usepackage{letltxmacro}
\LetLtxMacro{\originaleqref}{\eqref}
\renewcommand{\eqref}{Eq.~\originaleqref}

First of all you save a copy of the command to modify, then redefine it using the saved copy. It would be almost equivalent to say

\renewcommand*{\eqref}[1]{Eq.~\originaleqref{#1}}

but this is less efficient, as it reads the argument twice: one for the (new) \eqref and one for the \originaleqref.

There is also the etoolbox method:

\usepackage{etoolbox}
\pretocmd{\eqref}{Eq.~}{}{}

that doesn't require to keep a copy of the old command. However, this method will fail for commands that are defined with \DeclareRobustCommand or that take an optional argument, so it needs looking at the command's definition.

With xpatch one could do

\usepackage{xpatch}
\xpretocmd{\eqref}{Eq.~}{}{}
  • Great! this is exactly what I wanted. Also thanks for the explanation – PineApple Sep 19 '11 at 10:28
7

(I know that this isn't what you want to do, but it's very close so that others might find this question hoping to do the following.)

egreg's answer is what to do if you only want to change how the reference is typeset when it is referred to. If you want to change it everywhere, meaning when the label itself is typeset, then you need to change a macro called \tagform@. As far as I can see, amsmath doesn't include a facility for changing this easily so it needs to be redefined. Also, its definition involves a few commands that clearly "do something special" so its overall form should be preserved.

Its original definition is:

\def\tagform@#1{\maketag@@@{(\ignorespaces#1\unskip\@@italiccorr)}}

so to change it, you would put the following in your preamble:

\makeatletter
\def\tagform@#1{\maketag@@@{Eq.~(\ignorespaces#1\unskip\@@italiccorr)}}
\makeatother

For an explanation of the surrounding commands, see What do \makeatletter and \makeatother do?

The #1 becomes the equation number. Keep everything inside the \maketag@@@ macro, and keep the stuff surrounding the #1 where it is. Thus to change the format of the equation number (as I did above), you change the opening and closing parentheses and nothing else! (Unless you know what you're doing.)

So if you want your equation numbers to be green with purple spots, you might have:

\makeatletter
\def\tagform@#1{\maketag@@@{\GreenWithPurpleSpots{\ignorespaces#1\unskip\@@italiccorr}}}
\makeatother

Where, \GreenWithPurpleSpots is defined in the StrangeAndBizarreColours package[1].

More seriously, if you want square brackets instead of curved, you could put:

\makeatletter
\def\tagform@#1{\maketag@@@{[\ignorespaces#1\unskip\@@italiccorr]}}
\makeatother

Here's an example showing both this reformatting and egreg's redefinition of the \eqref command:

\documentclass{article}
\usepackage{amsmath}

\makeatletter
\def\tagform@#1{\maketag@@@{[\ignorespaces#1\unskip\@@italiccorr]}}
\makeatother

\let\originaleqref=\eqref
\renewcommand{\eqref}{Eq.~\originaleqref}

\begin{document}

\begin{equation}
\label{aneq}
x^2 + y^2 = z^2
\end{equation}

We refer to \eqref{aneq}.
\end{document}

And the output:

redefined equation labels


[1]: This is a joke, although I fully expect that package to exist on CTAN within about a week of posting.

  • Thanks. But in which situations besides referring to this equation would I use the label name? – PineApple Sep 19 '11 at 11:27
  • 1
    @PineApple: This changes the format of the label in the equation as well. That's the situation that it didn't seem that you yourself wanted, but which others who found your question might want. – Loop Space Sep 19 '11 at 11:35
  • Added a screenshot to make it clearer (I hope). – Loop Space Sep 19 '11 at 11:39
  • Yeah I understand that. But just wondering: in which more situations would I use that different labelling of the equation, so it would matter? Would a label in an equation be important for anything else besides the reference and it's look? – PineApple Sep 19 '11 at 11:40
  • 1
    The mathtools provides a user interface for redefinitions like this. If you load it, you can have square brackets with these commands: \newtagform{brackets}{[}{]}\usetagform{brackets} – diabonas Sep 20 '11 at 11:08
0

Try this:

\newcommand{\eq}[1]{\hyperref[#1]{Eq. \eqref{#1}}}

Of course, you need the hyperref package. Put this in the preamble:

\usepackage[colorlinks]{hyperref}
\hypersetup{linkcolor=DarkRed}

(the colorlinks option enables you to paint the referencing text)

  • nice, but is there an option to redefine the \eqref command with this package? – PineApple Sep 19 '11 at 13:26

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.