2

I am trying to create the following, but haven't been able to find anything here or elsewhere online...

I want to create an equation with a custom counter and then call the value of the counter when it was used in that equation. For example consider this.

\newcounter{opt}
\[
    ax + b = c
\refstepcounter{opt}
\label{eq:1}
\]
\ref{eq:1}

This final \ref call produces the value 1, as I desire. However, I'd like to tag the equation with a non-standard tag, eg \tag*{$(P_1)$}. I can do this as follows.

\newcounter{opt}
\[
    ax + b = c
\refstepcounter{opt}
\label{eq:1}
\tag*{$(P_\theopt)$}
\]
\ref{eq:1}

This gives the correct tag, however now the \ref call produces (P_1), instead of just 1.

How can I adapts this so that I get the desired tag and I can reference the equation and get the value of the counter?


This, "Referring to label and value of counter at that point", is a highly related post. It's doing something a bit more in-depth than I am trying to do, and I can't understand what's going on well enough to adapt it...


If it helps, the context is the following. I have an optimisation problem, and I want to label two equations (P_1) and (C_1) using the same counter opt. I then want to call this as (P,C)_1; I was going to do this by writing (P,C)_\ref{eq:1}.

I suppose, all I actually want to be able to do is to write a different thing in the brackets () on the equation line to what I \ref produces.

If there's a better way of doing this, I'm all ears!

7
  • If the equation is labeled (P<sub>1</sub>), why should the reference be just 1?
    – egreg
    Sep 29, 2017 at 8:42
  • Sorry, if I wasn't clear. I'm not questioning why the above does what it does -- indeed, it's labelled (P_1), as you say. However, I want to be able to call from the label just the value 1. This is what I can't work out how to do. \\ Hopefully the context I've added makes it clear! :)
    – Sam OT
    Sep 29, 2017 at 8:45
  • Do you want, for certain equations, a counter independent from the general equation counter, or only change the way the general counter is displayed?
    – Bernard
    Sep 29, 2017 at 9:30
  • I'd like this whole part to be separate from the general equation counter
    – Sam OT
    Sep 29, 2017 at 9:33
  • But if the two equations are different, you can't refer them with the same value of the counter!
    – Bernard
    Sep 29, 2017 at 9:38

1 Answer 1

1

Use a protected command, so it will be written as such in the .aux file and you can define it to act differently in different contexts.

\documentclass{article}
\usepackage{amsmath,xparse}

\newcounter{xtag}
\NewDocumentCommand{\xtag}{sm}{%
  \IfBooleanF{#1}{\refstepcounter{xtag}}%
  \tag{\choosetag{#2}{\thextag}}%
}

\ExplSyntaxOn
\NewDocumentCommand{\choosetag}{mm}
 {
  \bool_if:NTF \l_samt_choosetag_ref_bool
   {
    #2
   }
   {
    $\mathrm{#1}\sb{#2}$
   }
 }
\NewDocumentCommand{\xref}{m}
 {
  \group_begin:
  \bool_set_true:N \l_samt_choosetag_ref_bool
  \ref{#1}
  \group_end:
 }
\NewDocumentCommand{\xeqref}{om}
 {
  \IfNoValueTF{#1}
   {
    \eqref{#2}
   }
   {
    \textnormal{(#1)}$\sb{\xref{#2}}$
   }
 }

\bool_new:N \l_samt_choosetag_ref_bool
\ExplSyntaxOff

\begin{document}

\begin{gather}
1=1 \xtag{P}  \label{a} \\
2=2 \xtag*{C} \label{b} \\
3=3 \xtag{P}  \label{c}
\end{gather}

\xeqref{a} or \xeqref[P,C]{a}

\xeqref{c}

\end{document}

The command \xtag advances the counter, but the variant \xtag* doesn't.

enter image description here

5
  • Grazie mille! That's exactly what I wanted :) -- I have no idea what the group and bool stuff means, but I understand the rest... mostly! There are a couple of other situations I wanted a similar thing for, and I should be able to adapt your answer to those, so thanks very much! :)
    – Sam OT
    Sep 29, 2017 at 11:42
  • The \choosetag command changes its behavior according to truth or falsehood of the boolean; in \xref the boolean is set to true, so only the second argument is used. I fixed a slip.
    – egreg
    Sep 29, 2017 at 11:51
  • Ok, thanks. And yeah, I noticed the change -- what difference did it make?
    – Sam OT
    Sep 29, 2017 at 11:57
  • @SamT You'd realize it if typesetting \xeqref in a theorem statement (or, in general, in italic context).
    – egreg
    Sep 29, 2017 at 11:59
  • Ah, because it was forcing it to be upright?
    – Sam OT
    Sep 29, 2017 at 12:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .