3

In this previous question, we defined a new command \owntag to combine the equation number with a user-defined tag:

\documentclass{article}

\usepackage{amsmath}

\newcommand{\owntag}[1]{\stepcounter{equation}\tag{\theequation, #2}}

\begin{document}

\begin{align}
    F=ma \owntag[eq:newton]{Newton}
\end{align}

My equation is \eqref{eq:newton}.

\end{document}

enter image description here

As you can see, we can reference the equation getting the format "1, Newton" as a reference. I would like to extend this command such that I can

  • reference to the equation using only the number (i.e. "eq. (1)")
  • reference to the equation using only the text (i.e. "eq. (Newton)")

To implement the first goal, I tried to add an additional counter, set it to the value of the equation counter, do \refstepcounter and put the \label right after it, but still get "(1, Newton)" as a reference:

\documentclass{article}

\usepackage{amsmath}
\usepackage{xifthen}

\newcounter{equationwotag}

\newcommand{\owntag}[2][]{\ifthenelse{\equal{#1}{}}{
    \stepcounter{equation}\tag{\theequation, #2}
}{
    \setcounter{equationwotag}{\value{equation}}\label{#1}\stepcounter{equation}\stepcounter{equation}\tag{\theequation, #2}
}}

\begin{document}

\begin{align}
    F=ma \owntag[eq:newton]{Newton}
\end{align}

My equation is \eqref{eq:newton}.

\end{document}

How can I achieve these two methods of referencing?

1

When you you add a \label{eq:Newton} what happens is that the current "value" of \@currentlabel is stored in the aux file as the associated label. So one way to do what you want is to set \@currentlabel accordingly and then add the \label. There is an added complication in that the amsmath package redefines \label so that it does a little bit of error checking and, in particular, gives an error if you try to define two labels inside an equation environment like align. To get around this we have to use \ltx@label instead of \label, which is the "normal unguarded" latex \label.

Finally, I misread your MWE and thought that you wanted an optional argument that gives a short version of the label. This seems like a good feature to have but it requires a little more trickery as we need to insert a "variable" inside the label name.

I have set it up so that after \owntag[<short label>]{<tag>}:

  • \ref{Eq:<label>} gives the "full label" with the equation number and the "own tag"
  • \ref{eq:<label>} gives only the equation number
  • \ref{tag:<label>} gives only the "own tag"

The upshot is that you can now produce this:

enter image description here

using the code

\documentclass{article}

\usepackage{amsmath}
\makeatletter
\newcommand{\owntag}[2][\relax]{% \owntag[short label]{tag}
  \ifx#1\relax\relax\def\owntag@name{#2}\else\def\owntag@name{#1}\fi% base label
  \refstepcounter{equation}\tag{\theequation, #2}%
  \expandafter\ltx@label\expandafter{eq:\owntag@name}%
  \edef\@currentlabel{\theequation, #2}\expandafter\ltx@label\expandafter{Eq:\owntag@name}%
  \def\@currentlabel{#2}\expandafter\ltx@label\expandafter{tag:\owntag@name}%
}
\makeatother

\begin{document}

\begin{align}
    F=ma \owntag[newton]{Newton}% with optional argument
\end{align}

My equation is \eqref{Eq:newton}. The equation number by itself is
\eqref{eq:newton} and the tag is \eqref{tag:newton}.

\begin{align}
    E=mc^2\owntag{Einstein}% no optional argument
\end{align}

My equation is \eqref{Eq:Einstein}. The equation number by itself is
\eqref{eq:Einstein} and the tag is \eqref{tag:Einstein}.

\end{document}

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.