在文档的较长部分中交替对子方程组进行编号

在文档的较长部分中交替对子方程组进行编号

我想要两组不同的子方程,交替出现在文档的较长部分中,中间插入文本。一组标记为 1.1、1.2、...,另一组标记为 2.1、2.2、... 等等。

我通过将它们全部视为独立方程equation*并使用\tag手动对方程进行编号来实现这一点。

然而,这是一个非常繁琐且容易出错的过程,一定有更好的方法来实现相同的目标。

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[]{geometry}
\geometry{a4paper,inner=28mm,outer=18mm,top=20mm,bottom=20mm,footskip=10mm,headsep=5mm,headheight=15mm,}
\usepackage{amsmath}

\begin{document}

\begin{equation*} 
    \textrm{I am a subequation of group one} \tag{1.1}
\end{equation*}

Some random text.

\begin{equation*} 
    \textrm{I am a subequation of group two} \tag{2.1}
\end{equation*}

Some random text.

\begin{equation*} 
    \textrm{I am a subequation of group one} \tag{1.2}
\end{equation*}

Some random text.

\begin{equation*} 
    \textrm{I am a subequation of group two} \tag{2.2}
\end{equation*}

Some random text.

\begin{equation*} 
    \textrm{I am a subequation of group one} \tag{1.3}
\end{equation*}

Some random text.

\begin{equation*} 
    \textrm{I am a subequation of group two} \tag{2.3}
\end{equation*}

\end{document}

在此处输入图片描述

答案1

以下设置以与 类似的方式自动执行您所追求的操作subequations。您可以使用环境包装您想要具有唯一表示的方程组equationseries,并指定一个附带的系列作为强制参数。如果该系列之前没有定义,则会自动定义。如果它已经存在,则提取历史值,以便该系列可以像以前一样继续。它也可以与其他方程式交错,这些方程式应默认为通常的方程式格式。

在此处输入图片描述

\documentclass{article}

\usepackage{amsmath}

\makeatletter
\NewDocumentEnvironment{equationseries}{ m }{%
  % Check whether the equation series has been used before
  \ifcsname c@eqnseries#1\endcsname
    % Series has been used before
    \expandafter\protected@edef\expandafter\eqnprefix\expandafter{\csname theeqnseries#1\endcsname}% Extract series prefix
    \setcounter{parentequation}{\value{equation}}% Store current equation counter
    \setcounter{equation}{\value{eqnserieseqn#1}}% Set equation counter
  \else
    % First time using this series
    \refstepcounter{equation}% Step equation counter
    \setcounter{parentequation}{\value{equation}}% Store current equation counter
    \protected@edef\eqnprefix{\theequation}% Define series prefix
    \newcounter{eqnseries#1}\setcounter{eqnseries#1}{\value{equation}}% Define series counter
    \newcounter{eqnserieseqn#1}% Define series equation counter
    \setcounter{equation}{0}% Reset equation counter
  \fi
  %
  \def\theequation{\eqnprefix.\arabic{equation}}% Define series equation number representation
  \ignorespaces
}{%
  \setcounter{eqnserieseqn#1}{\value{equation}}% Store equation series counter
  \setcounter{equation}{\value{parentequation}}% Restore equation counter
  \ignorespacesafterend
}
\makeatother

\begin{document}

\section{Equations}

A regular equation:
\begin{equation}
  f(x) = ax^2 + bx + c
\end{equation}

An \verb|align|ed set of equations:
\begin{align}
           f(x) &= ax^2 + bx + c \\
  ax^2 + bx + c &= g(x)
\end{align}

\section{Subequations}

\begin{subequations}
A regular equation:
\begin{equation}
  f(x) = ax^2 + bx + c
\end{equation}

An \verb|align|ed set of equations:
\begin{align}
           f(x) &= ax^2 + bx + c \\
  ax^2 + bx + c &= g(x)
\end{align}
\end{subequations}


\clearpage

\section{Equation series}

A regular equation set as part of \verb|first|:
\begin{equationseries}{first}
\begin{equation}
  f(x) = ax^2 + bx + c
\end{equation}

Some text between the equations and then
\begin{equation}
  g(x) = ax^2 + bx + c
\end{equation}
\end{equationseries}

Then we start a new equation series called \verb|second|:
\begin{equationseries}{second}
\begin{equation}
  f(x) = ax^2 + bx + c
\end{equation}

Some more text between the equations and then
\begin{equation}
  g(x) = ax^2 + bx + c
\end{equation}
\end{equationseries}

A regular equation inserted between:
\begin{equation}
  f(x) = ax^2 + bx + c
\end{equation}

Back to the \verb|first| equation series:
\begin{equationseries}{first}
\begin{equation}
  f(x) = ax^2 + bx + c
\end{equation}
\end{equationseries}

Back to the \verb|second| equation series:
\begin{equationseries}{second}
\begin{align}
           f(x) &= ax^2 + bx + c \\
  ax^2 + bx + c &= g(x)
\end{align}
\end{equationseries}

Then, a normal set of equations:
\begin{align}
           f(x) &= ax^2 + bx + c \\
  ax^2 + bx + c &= g(x)
\end{align}

\end{document}

答案2

这是沃纳解决方案的一种变体,您可以在第一次使用系列时决定子方程数字的格式。

\documentclass{article}
\usepackage{amsmath}

%\usepackage{xparse} % not needed with LaTeX 2020-10-01 or later

\newcounter{savedequation}

\ExplSyntaxOn

\prop_new:N \g_jona_eqseries_prop

\NewDocumentEnvironment{eqseries}{mO{.\arabic{##1}}}
 {
  \prop_if_in:NnF \g_jona_eqseries_prop { #1 }
   {
    % step the equation number if new series
    \refstepcounter{equation}
    % store zero as starting point if new series
    \prop_gput:Nnn \g_jona_eqseries_prop { #1 } { 0 }
    % store the parent number if new series
    \prop_gput:Nnx \g_jona_eqseries_prop { #1-parent } { \theequation }
    % define the representation if new series
    \cs_gset:cn { __jona_eqseries_repr_#1_:n } { #2 }
   }
  \setcounter{savedequation}{\value{equation}}
  % set the parent number as found in the property list
  \setcounter{parentequation}{\prop_item:Nn \g_jona_eqseries_prop { #1-parent }}
  % set the starting number in the series as the last one used
  \setcounter{equation}{ \prop_item:Nn \g_jona_eqseries_prop { #1 } }
  % set the representation
  \cs_set:Npx \theequation 
   {
    \exp_not:N \theparentequation \exp_not:c {__jona_eqseries_repr_#1_:n} { equation }
   }
  \ignorespaces
 }
 {
  % save the last subequation number
  \prop_gput:Nnx \g_jona_eqseries_prop { #1 } { \int_eval:n { \value{equation} } }
  % reset to default
  \setcounter{equation}{\value{savedequation}}
  \ignorespacesafterend
 }

\ExplSyntaxOff

\begin{document}

\section{Equations}

A regular equation:
\begin{equation}
  f(x) = ax^2 + bx + c
\end{equation}

An \verb|align|ed set of equations:
\begin{align}
  f(x) &= ax^2 + bx + c \\
  g(x) &= \exp x + \log x - \sin x
\end{align}

\section{Subequations}

\begin{subequations}
A regular equation:
\begin{equation}
  f(x) = ax^2 + bx + c
\end{equation}

An \verb|align|ed set of equations:
\begin{align}
  f(x) &= ax^2 + bx + c \\
  g(x) &= \exp x + \log x - \sin x
\end{align}
\end{subequations}


\clearpage

\section{Equation series}

A regular equation set as part of \verb|first|:
\begin{eqseries}{first}
\begin{equation}
  f(x) = ax^2 + bx + c
\end{equation}

Some text between the equations and then
\begin{equation}
  g(x) = \exp x + \log x - \sin x
\end{equation}
\end{eqseries}

Then we start a new equation series called \verb|second|:
\begin{eqseries}{second}[--\alph{#1}]
\begin{equation}
  f(x) = ax^2 + bx + c
\end{equation}

Some more text between the equations and then
\begin{equation}
  g(x) = \exp x + \log x - \sin x
\end{equation}
\end{eqseries}

A regular equation inserted between:
\begin{equation}
  f(x) = ax^2 + bx + c
\end{equation}

Back to the \verb|first| equation series:
\begin{eqseries}{first}
\begin{equation}
  f(x) = ax^2 + bx + c
\end{equation}
\end{eqseries}

Back to the \verb|second| equation series:
\begin{eqseries}{second}
\begin{align}
  f(x) &= ax^2 + bx + c \\
  g(x) &= \exp x + \log x - \sin x
\end{align}
\end{eqseries}

Then, a normal set of equations:
\begin{align}
  f(x) &= ax^2 + bx + c \\
  g(x) &= \exp x + \log x - \sin x
\end{align}

\end{document}

(代码重复使用了 Werner 的答案。)

在此处输入图片描述

相关内容