我正在寻找一种在 LaTeX 中自动对可能多次插入的相同方程进行编号的方法(例如,以不同的形式)。输出将如下所示:
some text
x-y=0 (1)
some text
x=y (1a)
some text. Now with an align environment
x2-y2=0 (2)
y-x=0 (1b)
and with an align environment again
x2=y2 (2a)
x2+x=y2+y (3)
Equations (1a) and (1c) are just different forms of Eq. (1), the same for Eq. (2a) and Eq. (2)
完美的解决方案是:
- 字母自动增加
- 方程和对齐环境的可用性
- 简单标记和引用多个方程形式
- 兼容性
autoref
但这当然听起来像是乌托邦,所以欢迎任何解决方法。
我尝试使用包deleq
和tag
命令。我不想使用 deleq 环境,因为它们似乎依赖于类似的东西eqnarray
。这就是我所得到的:
\documentclass[11pt]{article}
\usepackage{deleq}
\usepackage{hyperref}
\usepackage{amsmath}
\begin{document}
Some text
\begin{equation} y-x=0 \label{Demo1} \end{equation}
Some text (the following is not compatible with equation)
$$ x=y \label{Demo1bis} \rndeqno{Demo1} $$ %Automatic ok but not equation and bad reference!
Some text, now with an align environment:
\begin{align}
x_2-y_2&=0 \label{Demo2} \\
y&=x \label{Demo1ter} \tag*{\ref{Demo1}b} %\rdeqno{Demo1}
\end{align}
Some text, with align
\begin{align}
x_2&=y_2 \label{Demo2bis} \tag*{\ref{Demo2}b} \\Not automatic !!! %\rndeqno{Demo2}\\
x_2+x&=y_2+y \label{Demo3}
\end{align}
\autoref{Demo1bis} and \autoref{Demo1ter} are different forms of \autoref{Demo1}, the same for \autoref{Demo2bis} and \autoref{Demo2}.
\end{document}
答案1
\documentclass[a4paper]{article}
\usepackage{amsmath}
\makeatletter
\newcommand{\defvariant}[1]{\label{#1}%
\ifmeasuring@\else
\expandafter\xdef\csname variant@#1\endcsname{\theequation}%
\expandafter\gdef\csname variant@#1@number\endcsname{0}
\fi}
\newcommand{\stepvariant}[2][]{%
\ifmeasuring@\else
\expandafter\xdef\csname variant@#2@number\endcsname{%
\number\numexpr\csname variant@#2@number\endcsname+1\relax}%
\tag{\ref{#2}\@alph{\number\csname variant@#2@number\endcsname}}%
\if\relax\detokenize{#1}\relax\else\label{#1}\fi
\fi}
\makeatother
\begin{document}
some text
\begin{equation}
x-y=0 \defvariant{equal}
\end{equation}
some text
\begin{equation}
x=y \stepvariant[ea]{equal}
\end{equation}
some text. Now with an align environment
\begin{align}
x^2-y^2&=0 \defvariant{squares} \\
y-x&=0 \stepvariant[eb]{equal}
\end{align}
and with an align environment again
\begin{align}
x^2&=y^2 \stepvariant[sa]{squares} \\
x^2+x&=y^2+y
\end{align}
Equations \eqref{ea}~and~\eqref{eb} are just different forms of
Equation~\eqref{equal}, the same for Equation~\eqref{sa} and
Equation~\eqref{squares}.
\end{document}
该命令\defvariant
定义了一个可能有变体形式的方程;它的参数也用于标记该方程。
该命令\stepvariant
将“父”方程的标签作为强制参数,将供以后参考的标签作为可选参数。
关键点在于避免在诸如和之\ifmeasuring@
类的环境中对“子”方程式进行两次伪计数器的步进,这些环境会对其内容进行两次排版,第一次用于测量方程式大小(当设置为 true 时)。align
gather
\ifmeasuring@
答案2
这不是一个完整的解决方案,而只是一些关于如何做到的提示。
您必须为“子方程”定义自己的计数器:
\newcounter{subequation}
定义如何显示此类计数器:
\renewcommand{\thesubequation}{\alph{subequation}}
然后,您可以使用以下命令为方程式创建引用和标签:
\refstepcounter{subequation} \label{someLabel}
现在,当您使用 时\ref{somLabel}
,您将获得该方程的编号。但是,存在一些问题,因为它不会显示第一个方程的编号,而只会显示 a.、b. 等,并且在步进主方程计数器时它不会自行“重置”。
您可以使用以下方法自动重置它:
\newcounter{subequation}[equation]
但这不适用于您的示例,因为您想在已增加到 eq2 之后为 eq1 插入新的子计数器。
无论如何,这些只是提示,供您参考。也许对计数器有更深入了解的人可以提供帮助。
答案3
如果您有 LuaLaTeX,我们可以对 egreg 的解决方案进行轻微改进(从某种意义上说,您只需要一个宏)。然后可以将大多数(但不是全部)宏代码移至 lua。我已经将单个宏实现为\tageqn[label]{number_category}
;名称很容易更改。
\documentclass[11pt]{article}
\usepackage{amsmath}
\usepackage{luacode}
\usepackage{xifthen}
\usepackage{hyperref}
\begin{luacode}
tagList = {} --maps number category to current value
function tagEqn(num)
letter = incrementLetter(num)
--default label (e.g. eqn:1b); can be configured by user
label = "eqn:".. num .. letter
outputEqn(num .. letter, label)
end
function labelEqn(num, label)
outputEqn(num .. incrementLetter(num), label)
end
--increments the current num's letter, returns the new letter
function incrementLetter(num)
if tagList[num] == nil then
tagList[num] = 0
else
tagList[num] = tagList[num] + 1
end
return string.char(string.byte("a") + tagList[num])
end
function outputEqn(tag,label)
tex.print( " \\tag{" .. tag .. "}" .. "\\label{"..label.."}")
end
\end{luacode}
\makeatletter
\newcommand{\tageqn}[2][]{\ifmeasuring@ \else \ifthenelse{\isempty{#1}}{\directlua{tagEqn(#2)} }{\directlua{labelEqn(#2,"#1")} } \fi}
\makeatother
\begin{document}
\section*{Basic, no specified labels}
Testing basic equations (including the equation environment) with no specified labels:\[999-99= \text{ modern math}\tageqn{17}\]
\begin{equation}
232332 = 912929192 \tageqn{17}
\end{equation}
\[323223=232332\tageqn{11}\]
Trying out default reference `eqn:17a' gives \ref{eqn:17a}, `eqn:17b' gives \ref{eqn:17b}, and `eqn:11a' gives \ref{eqn:11a}.
\section*{Basic, specified labels}
Testing basic equations with labels `dog', `cat', and `Time Lord'.\[999-99\tageqn[dog]{17}\]
\begin{equation}
232332 = 912929192 \tageqn[cat]{17}
\end{equation}
\[323223=232332\tageqn[Time Lord]{11}\]
Trying out references gives \ref{dog}, \ref{cat}, and \ref{Time Lord}.
\section*{Testing align (default labels)}
Finally, we test aligns. First we do so with default labels
\begin{align}
3 &= 4 + 7 \tageqn{17}
\\&= 8 + 9 \tageqn{17}
\\&> x \tageqn{11}
\end{align}
Trying out default reference `eqn:17e' gives \ref{eqn:17e}, `eqn:17f' gives \ref{eqn:17f}, and `eqn:11c' gives \ref{eqn:11c}.
\section*{Testing align (custom labels)}
Now with labels `rar', `ar', and `gaga'.
\begin{align}
3 &= 4 + 7 \tageqn[rar]{17}
\\&= 8 + 9 \tageqn[ar]{17}
\\&> x \tageqn[gaga]{11}
\end{align}
The references give us: \ref{rar}, \ref{ar}, and \ref{gaga}, respectively!
\section*{Autoref}
Finally, we try out autoref. `eqn17e' gives \autoref{eqn:17e}, `Time Lord' gives \autoref{Time Lord}, and `gaga' gives \autoref{gaga}!
\end{document}
答案4
你看过subequations
提供的环境了amsmath.sty
吗?它似乎完全符合你的要求。(请参阅 amsldoc.pdf 中的第 3.11.3 节,位于ftp://ftp.ams.org/ams/doc/amsmath/amsldoc.pdf
。例如,如果你输入
\documentclass{article}
\usepackage{amsmath}
\begin{document}
\thispagestyle{empty}
Here's some text.
\begin{subequations}
Here's even more text.
\begin{equation}
\label{eq:first}
x - y = 0
\end{equation}
Then again, we also have
\begin{equation}
\label{eq:second}
x = y
\end{equation}
Of course, equation \eqref{eq:second} is just another version of
equation \eqref{eq:first}.
\end{subequations}
Here's more text. Here's more text. Here's more text.
\begin{subequations}
Here's an align environment:
\begin{align}
x^{2} + y^{2} &= z^{2} \label{pythag}\\
\sin^{2}x + \cos^{2}x &= 1\label{trig}
\end{align}
Equation \eqref{pythag} isn't really the same as equation
\eqref{trig}, but they sort of look similar.
\end{subequations}
\end{document}
那么你会得到