Custom align environment which auto breaks line before = and aligns at =
The customalign
is defined using newenviron
in order to tap into the \BODY
of the environment. Every =
is replaced by \\ &=
via \xpatchcmd*
. A second \xpatchcmd
then restores the first global replacement back to only &=
:
\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}
This implementation avoids cumbersome replacements:
- the body of the environment is split at
=
; - the first item is set apart and passed to
split
followed by&=
; - the remaining item are delivered, separated by
\\ &=
.
\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}
There are three environments:
breakalign
will get a number;breakalign*
will be unnumbered;breakaligned
can be used as a subsidiary environment and accepts the usual[t]
or[b]
options likealigned
for vertical alignment.
A version compatible with older (but not too old) versions of 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}