我想创建一个类似于 AMS 证明环境的命令,\qedhere
当它到达结束时会自动调用命令(如 AMS 的),但允许您在需要时提前调用该命令。就我而言,我希望该命令执行一些代码并放置一个带有已保存参数的标签。当命令以文本结尾时,标签位于正确的位置。但是当命令以显示的方程式结束时,标签太靠后了。在这些情况下,我想手动将标签移动到正确的位置。在我的上一个问题,我找到了在大多数情况下如何做到这一点的方法。但事实证明,我的方法不适用于 AMS 显示方程环境(gather、align 和 flalign),即使它可以适用于 LaTeX 方程环境。
\documentclass{amsart}
\newcommand{\currentLabel}{\nosuchcommand}
\newcounter{exampleends}
\newif\ifendencountered
\newcommand{\exampleendhere}{\ifendencountered%
\vspace{-\baselineskip}%
\else%
\stepcounter{exampleends}%
\label{e\currentLabel}%
\textbf{End of \currentLabel.}%
\global\endencounteredtrue%
% \renewcommand{\currentLabel}{\nosuchcommand}%
\fi%
}
\newcommand{\myexample}[2]{%
\renewcommand{\currentLabel}{#1}%
\endencounteredfalse%
\textbf{Start of \currentLabel.}\label{#1}\\
#2\exampleendhere}
\begin{document}
\myexample{labelDefault}{This is the default.
\begin{equation*}1+1=2\end{equation*}}
\bigskip\bigskip
\myexample{labelUsesEEH}{This uses exampleendhere.
\begin{equation*}1+1=2\exampleendhere\end{equation*}}
\bigskip\bigskip
\myexample{labelGather}{This should have ``End of labelGather'', but doesn't.
\begin{gather*}1+1=2\exampleendhere\end{gather*}}
\bigskip\bigskip
example end called: \arabic{exampleends} time(s) (should be 3)
\end{document}
结果是:
我查看了 amsthm.sty 的源代码,但我不明白它是如何工作的,以及如何修改它以满足我的需求。为什么它的gather
行为与本例不同equation
?我怎样才能显示“labelGather 结束”?
答案1
您只想\global\endencounteredtrue
在“排版阶段”执行此操作,而不是在测量阶段。为此,您可以使用。如果您希望它在 中工作,\ifmeasuring@
您还需要。\ltx@label
align
\documentclass{amsart}
\newcommand{\currentLabel}{\nosuchcommand}
\newcounter{exampleends}
\newif\ifendencountered
\makeatletter
\newcommand{\exampleendhere}{%
\ifendencountered
\vspace{-\baselineskip}%
\else
\stepcounter{exampleends}%
\ltx@label{e\currentLabel}%
\textbf{End of \currentLabel.}%
\ifmeasuring@\else\global\endencounteredtrue\fi
% \renewcommand{\currentLabel}{\nosuchcommand}%
\fi
}
\newcommand{\myexample}[2]{%
\renewcommand{\currentLabel}{#1}%
\global\endencounteredfalse
\textbf{Start of \currentLabel.}\label{#1}\\
#2\exampleendhere
}
\begin{document}
\myexample{labelDefault}{This is the default.
\begin{equation*}1+1=2\end{equation*}}
\myexample{labelUsesEEH}{This uses exampleendhere.
\begin{equation*}1+1=2\exampleendhere\end{equation*}}
\myexample{labelGather}{This should have ``End of labelGather''
\begin{gather*}1+1=2\exampleendhere\end{gather*}}
\myexample{labelAlign1}{This should have ``End of labelAlign1''
\begin{align*}1+1=2\exampleendhere\end{align*}}
\myexample{labelAlign2}{This should have ``End of labelAlign2''
\begin{align*}1+1=2\exampleendhere\end{align*}}
example end called: \arabic{exampleends} time(s) (should be 3)
\end{document}