使用嵌套重新定义方程环境

使用嵌套重新定义方程环境

很多时候我写的方程式太长,所以我需要使用这个序列:

\begin{equation}\begin{gathered}
   ax+by=u\\
   cx+by=v
\end{gathered}\end{equation}

我想重新定义equation环境以便能够做这样的事情:

\begin{equation}
   ax+by=u\\
   cx+by=v
\end{equation}

我已经通过使用 \let这种方式实现了这一点

\let\EQUATION\equation
\let\endEQUATION\endequation
\renewenvironment{equation}{\begin{EQUATION}\begin{gathered}}{\end{gathered}\end{EQUATION}\ignorespacesafterend}

但是,我真正想要的是不使用中间变量来实现这种效果。我试过这个

\expandafter\def\expandafter\equation\expandafter{\equation\gathered}
\expandafter\def\expandafter\endequation\expandafter{\endgathered\endequation}

但它给我的唯一结果就是这个错误

Misplaced \crcr. \end{equation}
Extra }, or forgotten $. \end{equation}

有人能帮我解决这个问题吗?

答案1

正如 egreg 一样,我不建议以这种方式重新定义环境。

您需要另一个\expandafter,因此在定义开始之前 \endequation会扩展一次(而不是),因此该行应该是。\endgather\expandafter\def\expandafter\endequation\expandafter{\endgathered\endequation}\expandafter\def\expandafter\endequation\expandafter{\expandafter\endgathered\endequation}

答案2

我建议不是这样做,是因为你养成了坏习惯。

\documentclass{article}
\usepackage{amsmath}

%%%% not recommended
\NewCommandCopy{\latexequation}{\equation}
\NewCommandCopy{\latexendequation}{\endequation}
\ExpandArgs{nc}\NewCommandCopy{\latexequationstar}{equation*}
\ExpandArgs{nc}\NewCommandCopy{\latexendequationstar}{endequation*}

\RenewDocumentEnvironment{equation}{}
 {\latexequation\gathered}
 {\endgathered\latexendequation}
\RenewDocumentEnvironment{equation*}{}
 {\latexequationstar\gathered}
 {\endgathered\latexendequationstar}

\begin{document}

words before the equation words before the equation
words before the equation words before the equation
\begin{equation}
  ax+by=u\\
  cx+by=v
\end{equation}
words after the equation words after the equation
words after the equation words after the equation

words before the equation words before the equation
words before the equation words before the equation
\begin{equation*}
  ax+by=u\\
  cx+by=v
\end{equation*}
words after the equation words after the equation
words after the equation words after the equation

\end{document}

在此处输入图片描述

推荐方式:

\documentclass{article}
\usepackage{amsmath}

%%%% recommended

\NewDocumentEnvironment{longequation}{}
 {\equation\gathered}
 {\endgathered\endequation}
\NewDocumentEnvironment{longequation*}{}
 {\csname equation*\endcsname\gathered}
 {\endgathered\csname endequation*\endcsname}

\begin{document}

words before the equation words before the equation
words before the equation words before the equation
\begin{longequation}
  ax+by=u\\
  cx+by=v
\end{longequation}
words after the equation words after the equation
words after the equation words after the equation

words before the equation words before the equation
words before the equation words before the equation
\begin{longequation*}
  ax+by=u\\
  cx+by=v
\end{longequation*}
words after the equation words after the equation
words after the equation words after the equation

\end{document}

只是为了好玩而\expandafter狂欢,以避免定义命令副本。

\documentclass{article}
\usepackage{amsmath}

%%%% definitely not recommended
\expandafter\def\expandafter\equation\expandafter{\equation\gathered}
\expandafter\def\expandafter\endequation\expandafter{\expandafter\endgathered\endequation}

\expandafter\def\csname equation*\expandafter\expandafter\expandafter\endcsname
\expandafter\expandafter\expandafter{\csname equation*\endcsname\gathered}
\expandafter\def\csname endequation*\expandafter\expandafter\expandafter\endcsname
\expandafter\expandafter\expandafter{%
  \expandafter\expandafter\expandafter\endgathered\csname endequation*\endcsname
}

\begin{document}

words before the equation words before the equation
words before the equation words before the equation
\begin{equation}
  ax+by=u\\
  cx+by=v
\end{equation}
words after the equation words after the equation
words after the equation words after the equation

words before the equation words before the equation
words before the equation words before the equation
\begin{equation*}
  ax+by=u\\
  cx+by=v
\end{equation*}
words after the equation words after the equation
words after the equation words after the equation

\end{document}

狂欢可以稍微驯服一点

%%%% definitely not recommended
\edef\equation{\unexpanded\expandafter{\equation\gathered}}
\edef\endequation{\noexpand\endgathered\unexpanded\expandafter{\endequation}}

\expandafter\edef\csname equation*\endcsname{%
  \unexpanded\expandafter\expandafter\expandafter{\csname equation*\endcsname\gathered}%
}
\expandafter\edef\csname endequation*\endcsname{%
  \noexpand\endgathered
  \unexpanded\expandafter\expandafter\expandafter{\csname endequation*\endcsname}%
}

但这可能更好:

%%%% definitely not recommended
\ExplSyntaxOn
\cs_set:Npx \equation { \exp_not:o {\equation} \exp_not:N \gathered }
\cs_set:Npx \endequation { \exp_not:N \endgathered \exp_not:o {\endequation} }

\cs_set:cpx {equation*} { \exp_args:Nc \exp_not:o { equation* } \exp_not:N \gathered }
\cs_set:cpx {endequation*} { \exp_not:N \endgathered \exp_args:Nc \exp_not:o {endequation*} }
\ExplSyntaxOff

相关内容