环境subequations与parentequation柜台

环境subequations与parentequation柜台

subequations似乎只起作用外部环境,例如alignequation。如果我想使用 来保持等号的对齐,align同时还想subequations在 内进行编号,该怎么办?例如:

\begin{align}
   \begin{subequations}
    A &= B+1 \\
    B+1 &= C
   \end{subequations}
\intertext{therefore}
   \begin{subequations}
    A &= C \\
    B &= C-1
   \end{subequations}
\end{align}

这会产生大量错误:(

答案1

环境subequationsparentequation柜台

原始环境以独立于计数器的方式subequations重新定义:\theparentequationparentequation

\protected@edef\theparentequation{\theequation}%

但是我们可以重新定义环境,以便只保存subequations的值并再次使用计数器:equation\theparentequationparentequation

\def\theparentequation{\arabic{parentequation}}%

\theequation但这样一来,我们就会失去可能涉及\thechapter以下类的设置book。为此,我们可以再次手动将其更改为

\def\theparentequation{\thechapter.\arabic{parentequation}}%

etoolbox\patchcmd

另一种方法是使用etoolbox及其\patchcmd宏。

首先我们让\theparentequation相同于\theequation

\let\theparentequation\theequation

之后我们查找每一个出现的equation并将其更改为parentequation

\patchcmd{\theparentequation}{equation}{parentequation}{}{}

此时我们不再需要环境内部\def的初始化:\theparentequationsubequations

\usepackage{etoolbox}

% let \theparentequation use the same definition as equation
\let\theparentequation\theequation
% change every occurence of "equation" to "parentequation"
\patchcmd{\theparentequation}{equation}{parentequation}{}{}

\renewenvironment{subequations}{%
  \refstepcounter{equation}%
%  \def\theparentequation{\arabic{parentequation}}% we patched it already :)
  \setcounter{parentequation}{\value{equation}}%
  \setcounter{equation}{0}%
  \def\theequation{\theparentequation\alph{equation}}%
  \ignorespaces
}{%
  \setcounter{equation}{\value{parentequation}}%
  \ignorespacesafterend
}

\nextParentEquation

\nextParentEquation我还让我定义递增parentequation和重置的命令,equation这样我们就不必手动执行这些操作。

\newcommand*{\nextParentEquation}{%
  \stepcounter{parentequation}\setcounter{equation}{0}%
}

标签解决方法parentequation(由 OP莫比乌斯披萨

虽然\label直接在之后\begin{subequations}引用第一个父方程式,但为了使其能够在之后工作,\nextParentEquation有必要定义一个别名命令\parentlabel,例如,逃避通过以下方式执行的清理amsmath

\let\parentlabel\label

Qrrbrbirlbel 添加

我没有想到要标记父方程,但是随着 OP 的补充(参见上文),我甚至会更进一步,重新定义环境subequations\nextParentEquation以便它们采用可选参数,即标签的名称。

代码

这个 MWE 使用report\chapter证明\thechapter.在重新定义环境时不会迷失,当然,subequations这也适用于。article

\documentclass{report}
\usepackage{amsmath}
\usepackage{etoolbox}

% let \theparentequation use the same definition as equation
\let\theparentequation\theequation
% change every occurence of "equation" to "parentequation"
\patchcmd{\theparentequation}{equation}{parentequation}{}{}

\renewenvironment{subequations}[1][]{%              optional argument: label-name for (first) parent equation
  \refstepcounter{equation}%
%  \def\theparentequation{\arabic{parentequation}}% we patched it already :)
  \setcounter{parentequation}{\value{equation}}%    parentequation = equation
  \setcounter{equation}{0}%                         (sub)equation  = 0
  \def\theequation{\theparentequation\alph{equation}}% 
  \let\parentlabel\label%                           Evade sanitation performed by amsmath
  \ifx\\#1\\\relax\else\label{#1}\fi%               #1 given: \label{#1}, otherwise: nothing
  \ignorespaces
}{%
  \setcounter{equation}{\value{parentequation}}%    equation = subequation
  \ignorespacesafterend
}

\newcommand*{\nextParentEquation}[1][]{%            optional argument: label-name for (first) parent equation
  \refstepcounter{parentequation}%                  parentequation++
  \setcounter{equation}{0}%                         equation = 0
  \ifx\\#1\\\relax\else\parentlabel{#1}\fi%         #1 given: \label{#1}, otherwise: nothing
}

\begin{document}
\chapter{Test}
\begin{equation}
  0 \neq 1
\end{equation}
\begin{subequations}[eq:2]% or: \label{eq:2}
\begin{align}%              or: \parentlabel{eq:2}
    A & = B+1 \label{eq:2a} \\
  B+1 & = C   \label{eq:2b}
\intertext{therefore}\nextParentEquation[eq:3]% or: \nextParentEquation\parentlabel{eq:3}
    A & = C   \label{eq:3a} \\
    B & = C-1 \label{eq:3b}
\end{align}
\end{subequations}
\eqref{eq:2}: \eqref{eq:2a}, \eqref{eq:2b} \\ \eqref{eq:3}: \eqref{eq:3a}, \eqref{eq:3b}
\begin{equation}
  1 \neq 2
\end{equation}
\end{document}

输出

输出

答案2

正如你提到的,使用subequations 外部环境align并使用一些盒子操作来水平对齐方程式:

在此处输入图片描述

\documentclass{article}
\usepackage{amsmath}% http://ctan.org/pkg/amsmath
\begin{document}

\begin{subequations}
  \begin{align}
    A &= B+1 \\
    B+1 &= C
  \end{align}
\end{subequations}
\vspace*{-\belowdisplayshortskip}
\noindent therefore
\vspace*{-\abovedisplayshortskip}
\begin{subequations}
  \begin{align}
    \phantom{B+1}\llap{$A$} &= C \\
    B &= C-1
  \end{align}
\end{subequations}

\end{document}

上一组子方程的左侧的最大长度比下一组子方程的左侧的最大长度更长/更宽。因此,我们\phantom{<longest LHS>}\llap{$<LHS>$}在第二组子方程中插入一个,以弥补这种水平长度差异。

额外的垂直调整使得短文本的表达更加紧凑therefore

相关内容