自定义对齐环境,在 = 前自动换行并在 = 处对齐

自定义对齐环境,在 = 前自动换行并在 = 处对齐
\documentclass{article}
\usepackage{mathtools}
\DeclareMathOperator{\lcm}{lcm}

\begin{document}
\begin{align*}
   \lcm(13, 7) &= (13 \cdot 7):\gcd(13, 7) \\
               &= (13 \cdot 7):1 \\
               &= \overset{10+3}{13} \cdot 7 \\
               &= 10 \cdot 7+3 \cdot 7 \\
               &= 70+21 \\
               &= \boxed{91}
\end{align*}
\end{document}

在此处输入图片描述

是否可以定义一个自定义环境,比如customalign,通过像这样在一行上写下方程式来获得相同的输出?

\begin{customalign}
   \lcm(13, 7) = (13 \cdot 7):\gcd(13, 7) = (13 \cdot 7):1 = \overset{10+3}{13} \cdot 7 = 10 \cdot 7+3 \cdot 7 = 70+21 = \boxed{91}
\end{customalign}

环境customalign应该自动:

  • =从第二个符号开始,在每个符号前换行,即,它应该从第二个符号开始,\\在每个=符号前自动添加;
  • 在每个符号处对齐,即它应该在每个符号之前但在之后=自动添加。&=\\

答案1

使用customalign来定义newenviron以便利用\BODY环境。每个都由via=替换。然后第二个将第一个全局替换恢复为仅:\\ &=\xpatchcmd*\xpatchcmd&=

在此处输入图片描述

\documentclass{article}

\usepackage{mathtools,regexpatch,environ}
\DeclareMathOperator{\lcm}{lcm}

\NewEnviron{customalign}{%
  \xpatchcmd*{\BODY}{=}{\\ &=}{}{}% Global replacement "=" > "\\ &="
  \xpatchcmd{\BODY}{\\ &=}{&=}{}{}% First replacement "\\ &=" > "&="
  \begin{align*}
    \BODY% Set align with replacement
  \end{align*}
}

\begin{document}

\begin{align*}
  \lcm(13, 7) &= (13 \cdot 7) : \gcd(13, 7) \\
              &= (13 \cdot 7) : 1 \\
              &= \overset{10 + 3}{13} \cdot 7 \\
              &= 10 \cdot 7 + 3 \cdot 7 \\
              &= 70 + 21 \\
              &= \boxed{91}
\end{align*}

\begin{customalign}
   \lcm(13, 7) = (13 \cdot 7) : \gcd(13, 7) 
               = (13 \cdot 7) : 1 
               = \overset{10 + 3}{13} \cdot 7 
               = 10 \cdot 7 + 3 \cdot 7 
               = 70 + 21 
               = \boxed{91}
\end{customalign}

\end{document}

答案2

这种实现避免了繁琐的替换:

  1. 环境主体在 处分裂=
  2. 第一个项目被分开并传递给split然后是&=
  3. 剩余物品将被送达,并以 分隔\\ &=
\documentclass{article}
\usepackage{mathtools}
\usepackage{xparse}

\DeclareMathOperator{\lcm}{lcm}

\ExplSyntaxOn

\NewDocumentEnvironment{breakalign}{b}
 {
  \begin{equation}
  \soundwave_breakalign:nnn { #1 } { split } { }
  \end{equation}
 }
 {}
\NewDocumentEnvironment{breakalign*}{b}
 {
  \begin{equation*}
  \soundwave_breakalign:nnn { #1 } { split } { }
  \end{equation*}
 }
 {}
\NewDocumentEnvironment{breakaligned}{O{c} b}
 {
  \soundwave_breakalign:nnn { #2 } { aligned } { #1 }
 }
 {}

\tl_new:N \l__soundwave_breakalign_left_tl
\seq_new:N \l__soundwave_breakalign_body_seq

\cs_new_protected:Nn \soundwave_breakalign:nnn
 {
  \seq_set_split:Nnn \l__soundwave_breakalign_body_seq { = } { #1 }
  \seq_pop_left:NN \l__soundwave_breakalign_body_seq \l__soundwave_breakalign_left_tl
  \tl_if_empty:nTF { #3 } { \begin{#2} } { \begin{#2}[#3] }
  \l__soundwave_breakalign_left_tl &=
  \seq_use:Nn \l__soundwave_breakalign_body_seq { \\ &= }
  \end{#2}
 }

\ExplSyntaxOff

\begin{document}

\begin{breakalign*}
   \lcm(13, 7) = (13 \cdot 7):\gcd(13, 7)
               = (13 \cdot 7):1
               = \overset{10+3}{13} \cdot 7
               = 10 \cdot 7+3 \cdot 7
               = 70+21
               = \boxed{91}
\end{breakalign*}

\begin{breakalign}
   \lcm(13, 7) = (13 \cdot 7):\gcd(13, 7)
               = (13 \cdot 7):1
               = \overset{10+3}{13} \cdot 7
               = 10 \cdot 7+3 \cdot 7
               = 70+21
               = \boxed{91}
\end{breakalign}

\begin{align*}
&\begin{breakaligned}
   \lcm(13, 7) = (13 \cdot 7):\gcd(13, 7)
               = (13 \cdot 7):1
               = \overset{10+3}{13} \cdot 7
               = 10 \cdot 7+3 \cdot 7
               = 70+21
               = \boxed{91}
\end{breakaligned}
&&
\begin{breakaligned}
   \lcm(13, 7) = (13 \cdot 7):\gcd(13, 7)
               = (13 \cdot 7):1
               = \overset{10+3}{13} \cdot 7
               = 10 \cdot 7+3 \cdot 7
               = 70+21
               = \boxed{91}
\end{breakaligned}
\end{align*}

\end{document}

有三种环境:

  1. breakalign会得到一个数字;
  2. breakalign*将不被编号;
  3. breakaligned可以用作附属环境并接受通常的[t]或诸如垂直对齐[b]的选项。aligned

在此处输入图片描述

与旧版本(但不是太旧)的 TeX Live 版本兼容的版本。

\documentclass{article}
\usepackage{mathtools}
\usepackage{xparse}
\usepackage{environ}

\DeclareMathOperator{\lcm}{lcm}

\ExplSyntaxOn

\NewEnviron{breakalign}
 {
  \begin{equation}
  \soundwave_breakalign:Vnn \BODY { split } { }
  \end{equation}
 }

\NewEnviron{breakalign*}
 {
  \begin{equation*}
  \soundwave_breakalign:Vnn \BODY { split } { }
  \end{equation*}
 }

\NewEnviron{breakaligned}[1][c]
 {
  \soundwave_breakalign:Vnn \BODY { aligned } { #1 }
 }


\tl_new:N \l__soundwave_breakalign_left_tl
\seq_new:N \l__soundwave_breakalign_body_seq

\cs_new_protected:Nn \soundwave_breakalign:nnn
 {
  \seq_set_split:Nnn \l__soundwave_breakalign_body_seq { = } { #1 }
  \seq_pop_left:NN \l__soundwave_breakalign_body_seq \l__soundwave_breakalign_left_tl
  \tl_if_empty:nTF { #3 } { \begin{#2} } { \begin{#2}[#3] }
  \l__soundwave_breakalign_left_tl &=
  \seq_use:Nn \l__soundwave_breakalign_body_seq { \\ &= }
  \end{#2}
 }
\cs_generate_variant:Nn \soundwave_breakalign:nnn { V }

\ExplSyntaxOff

\begin{document}

\begin{breakalign*}
   \lcm(13, 7) = (13 \cdot 7):\gcd(13, 7)
               = (13 \cdot 7):1
               = \overset{10+3}{13} \cdot 7
               = 10 \cdot 7+3 \cdot 7
               = 70+21
               = \boxed{91}
\end{breakalign*}

\begin{breakalign}
   \lcm(13, 7) = (13 \cdot 7):\gcd(13, 7)
               = (13 \cdot 7):1
               = \overset{10+3}{13} \cdot 7
               = 10 \cdot 7+3 \cdot 7
               = 70+21
               = \boxed{91}
\end{breakalign}

\begin{align*}
&\begin{breakaligned}
   \lcm(13, 7) = (13 \cdot 7):\gcd(13, 7)
               = (13 \cdot 7):1
               = \overset{10+3}{13} \cdot 7
               = 10 \cdot 7+3 \cdot 7
               = 70+21
               = \boxed{91}
\end{breakaligned}
&&
\begin{breakaligned}
   \lcm(13, 7) = (13 \cdot 7):\gcd(13, 7)
               = (13 \cdot 7):1
               = \overset{10+3}{13} \cdot 7
               = 10 \cdot 7+3 \cdot 7
               = 70+21
               = \boxed{91}
\end{breakaligned}
\end{align*}

\end{document}

答案3

另一个(稍微脆弱的)选择是使用布雷克,这是自动换行的实验包。

默认情况下,breqn还会包含换行符:,因此我们需要使用来隐藏它们\hiderel{:}。以下是完整代码:

\documentclass{article}
\usepackage{amsmath,breqn}
\DeclareMathOperator{\lcm}{lcm}

\begin{document}
\begin{dmath*}
   \lcm(13, 7) = (13 \cdot 7) \hiderel{:}\gcd(13, 7) 
               = (13 \cdot 7)\hiderel{:}1 
               = \overset{10+3}{13} \cdot 7 
               = 10 \cdot 7+3 \cdot 7 
               = 70+21 
               = \boxed{91}
\end{dmath*}
\end{document}

在此处输入图片描述

相关内容