1

I created a new counter and new commands to label and refer to this counter (the latter looks redundant here because I stripped away some formatting I have in it) according to this answer.

As the title says, when I'm trying to use my new counter inside an amsmath's align environment - it doesn't work. When \nonumber is added at the end of the relevant line - no label is created whatsoever (I checked the .aux file). When this is not added (as in the code below) the labeled counter is align's one rather than my custom counter, although mine was the last counter that was incremented (as it is \refstepcounter-ed right before the \label inside the new command).

How can I label my counter as opposed to align's one while inside this environment?

\documentclass{article}

\usepackage{amsmath}

%equations' placeholders to allow breaking them in the middle and picking up from the same spot https://tex.stackexchange.com/a/21305/160156
\newcounter{eqplaceholdercounter}
\newcommand{\eqplaceholder}{\stepcounter{eqplaceholdercounter}\theeqplaceholdercounter}
\newcommand{\eqplaceholderlbl}[1]{{\refstepcounter{eqplaceholdercounter}\label{#1}\theeqplaceholdercounter}}
\newcommand{\eqplaceholderref}[1]{\ref{#1}}

\begin{document}

    \begin{align}
        eq1 \\
        eq2 \\
        eq3\quad \eqplaceholderlbl{plc:label} %\nonumber
    \end{align}

    But the ref is \eqplaceholderref{plc:label} instead of \theeqplaceholdercounter.
    While if I label outside of the align environment, like here: 
\eqplaceholderlbl{plc:label2}, all is well and I can reference it and get the correct number - \eqplaceholderref{plc:label2}.
\end{document}

The above code's output, as it is:

The above code's output

The above code's output, after uncommenting the \nonumber:

The above code's output after uncommenting the nonumber command

1

This results from align capturing \label for use at a later stage within the processing cycle. If you want an immediate use of \label that coincides with your \refstepcounter, use \ltx@label (saved as the traditional \label within align-and-friends):

enter image description here

\documentclass{article}

\usepackage{amsmath}

%equations' placeholders to allow breaking them in the middle and picking up from the same spot https://tex.stackexchange.com/a/21305/5764
\makeatletter
\newcounter{eqplaceholdercounter}
\newcommand{\eqplaceholder}{\stepcounter{eqplaceholdercounter}\theeqplaceholdercounter}
\newcommand{\eqplaceholderlbl}[1]{{\refstepcounter{eqplaceholdercounter}\ltx@label{#1}\theeqplaceholdercounter}}
\newcommand{\eqplaceholderref}[1]{\ref{#1}}
\makeatother

\begin{document}

\begin{align}
  eq1 \\
  eq2 \\
  eq3\quad \eqplaceholderlbl{plc:label} %\nonumber
\end{align}

But the ref is \eqplaceholderref{plc:label} instead of \theeqplaceholdercounter.
While if I label outside of the align environment, like here: 
\eqplaceholderlbl{plc:label2}, all is well and I can reference it and get the correct number - \eqplaceholderref{plc:label2}.

\end{document}
  • Wonderful, thank you very much! I also wanted to add a remark to make people notice the important lines in your code - \makeatletter before the \newcommand (that uses @ as a letter) and its opposite \makeatother after it, just in case. – et_l Aug 14 '18 at 17:00
  • 1
    @et_l: Indeed. See What do \makeatletter and \makeatother do? You can also make things a bit cleaner and define \makeatletter \newcommand{\ltxlabel}{\ltx@label} \makeatother, allowing you to use \ltxlabel within your main document. – Werner Aug 14 '18 at 17:11

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.