\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
这种实现避免了繁琐的替换:
- 环境主体在 处分裂
=
; - 第一个项目被分开并传递给
split
然后是&=
; - 剩余物品将被送达,并以 分隔
\\ &=
。
\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}
有三种环境:
breakalign
会得到一个数字;breakalign*
将不被编号;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}