在对齐环境中标记自定义计数器

在对齐环境中标记自定义计数器

我创建了一个新的计数器和新的命令来标记和引用这个计数器(后者在这里看起来是多余的,因为我去掉了其中的一些格式)根据这个答案

正如标题所述,当我尝试在 amsmath 的 align 环境中使用我的新计数器时 - 它不起作用。当\nonumber在相关行的末尾添加时 - 不会创建任何标签(我检查了 .aux 文件)。当没有添加它时(如下面的代码所示),带标签的计数器是 align 的计数器,而不是我的自定义计数器,尽管我的计数器是最后一个递增的计数器(因为它在新命令内部\refstepcounter之前被添加)。\label

在这种环境下,我怎样标记我的计数器而不是对齐的计数器?

\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}

上述代码的输出如下:

上述代码的输出

取消注释后,上述代码的输出为\nonumber

取消注释 nonumber 命令后上述代码的输出

答案1

align这是捕获的结果,\label以便在处理周期的稍后阶段使用。如果您希望立即使用\label与您的相符的\refstepcounter,请使用\ltx@label(保存为传统的\labelwithin align-and-friends):

在此处输入图片描述

\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}

相关内容