18

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.

egreg
  • 1,121,712
PineApple
  • 641

4 Answers4

20

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.~}{}{}
egreg
  • 1,121,712
9

(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.

Andrew Stacey
  • 153,724
  • 43
  • 389
  • 751
  • 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. – Andrew Stacey Sep 19 '11 at 11:35
  • Added a screenshot to make it clearer (I hope). – Andrew Stacey 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
  • Ah, I see. No, I can't think of anything else! – Andrew Stacey Sep 19 '11 at 11:57
  • 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
  • @diabonas: I didn't know that - now I do! Though I notice that they don't separate the form in the equation from the form in the reference so this wouldn't work in this case. – Andrew Stacey Sep 20 '11 at 11:19
  • @AndrewStacey Yeah, it's just a wrapper around \tagform@/\maketag@@@ - no extra effort is put in e.g. to ensure the same same tag form is used in the equation and its reference. – diabonas Sep 20 '11 at 11:33
3

Instead of modifying \eqref you could use the cleveref package:

\documentclass{article}
\usepackage{amsmath}

\usepackage{cleveref}

\begin{document}

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

We refer to \cref{quack}. \end{document}

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)

user1999
  • 1,464