# Multiple split equations

I find myself again and again using a combination of the gather and aligned environments. So it would be nice to not have to write so much. In particular, it is often only clear after writing the formula, whether it needs splitting. So the new environment should act like gather, only that when lines are separated by a special symbol/macro (\n for example) they should be put together into an aligned-environment. I tried the following

\documentclass{article}
\usepackage[fleqn]{amsmath}

\newenvironment{multeq}{%
\let\n{\\}
\let\\{\end{aligned}\\\begin{aligned}}
\begin{aligned}
}{%
\end{aligned}
\let\\{\n}
}

\begin{document}

\begin{gather}
\begin{aligned}
S(A) &= (I_{n-k, k}AI_{n-k,k})_{i,j} = (I_{n-k,k})_{i,i}A_{i,j}(I_{n-k,k})_{j,j}\\
&= \begin{cases}A_{i,j}&\begin{aligned}&(i\leq n-k\wedge j\leq n-k)\\\vee&(i>n-k\wedge j>n-k)\end{aligned}\\-A_{i,j}&\text{otherwise}\end{cases}
\end{aligned}\\
\Rightarrow H = S(U(n-k)\times U(k))
\end{gather}

\begin{multeq}
S(A) &= (I_{n-k, k}AI_{n-k,k})_{i,j} = (I_{n-k,k})_{i,i}A_{i,j}(I_{n-k,k})_{j,j}\n
&= \begin{cases}A_{i,j}&\begin{aligned}&(i\leq n-k\wedge j\leq n-k)\n\vee&(i>n-k\wedge j>n-k)\end{aligned}\n-A_{i,j}&\text{otherwise}\end{cases}\\
\Rightarrow H = S(U(n-k)\times U(k))
\end{multeq}

\end{document}


where the output should be 2×

but it gives the error:

! LaTeX Error: There's no line here to end.
l.24    \begin{multeq}


How does this error come about and what is a good way to create such an environment?

So using David’s corrections, and adding gather:

\newenvironment{multeq}{\begin{gather}%
\def\n{\\}%
\def\\{\end{aligned}\\\begin{aligned}}%
\begin{aligned}%
}{%
\end{aligned}%
\end{gather}}


I got

! Paragraph ended before \gather was complete.


So I tried using \Collect@Body from environ:

\def\savebody#1{\def\BODY{#1}}
\usepackage{environ}
\makeatletter

\newenvironment{multeq}{
\def\n{\\}%
\def\\{\end{aligned}\\\begin{aligned}}%
\Collect@Body\savebody
}{\begin{gather}%
\begin{aligned}%
\BODY%
\end{aligned}%
\end{gather}}

\makeatother


But now the redefinition of \\ is ignored. What else could I do?

• \let\n{\\} defines \n to be { like \bgroup. You mean \def there. Commented Jan 26, 2013 at 11:04
• @DavidCarlisle: Is \let\n\\ ok? (and why isn't this markedup as code?)
– bodo
Commented Jan 26, 2013 at 11:05
• that defines \n to have the current meaning of \\  (the markup around backticks with backslashes and spaces in comments is a bit odd, I had to edit my previous comment a couple of times to get it to display I know what you mean though:-) Commented Jan 26, 2013 at 11:12
• so \let\n\ is a legal assignment but would define  \n  to the wrong thing. Commented Jan 26, 2013 at 11:13
• @DavidCarlisle (Not related to the problem) I thought I was the only one having trouble with escaping backslashes and backticks in comment markdowns. Here is an so meta post for backslashes: Backslash escaped with backticks rendered incorrectly in comments as a well related post for backticks :-) Commented Jan 27, 2013 at 4:33

You need \def not \let \nd you need to get into math mode:

\documentclass{article}
\usepackage[fleqn]{amsmath}

\newenvironment{multeq}{% \def\n{\\}% \def\\{\end{aligned}\\\begin{aligned}}% \begin{aligned}% }{% \end{aligned}%}

\begin{document}

\begin{gather}
\begin{aligned}
S(A) &= (I_{n-k, k}AI_{n-k,k})_{i,j} = (I_{n-k,k})_{i,i}A_{i,j}(I_{n-k,k})_{j,j}\\
&= \begin{cases}A_{i,j}&\begin{aligned}&(i\leq n-k\wedge j\leq n-k)\\\vee&(i>n-k\wedge j>n-k)\end{aligned}\\-A_{i,j}&\text{otherwise}\end{cases}
\end{aligned}\\
\Rightarrow H = S(U(n-k)\times U(k))
\end{gather}

\begin{multeq}
S(A) &= (I_{n-k, k}AI_{n-k,k})_{i,j} = (I_{n-k,k})_{i,i}A_{i,j}(I_{n-k,k})_{j,j}\n
&= \begin{cases}A_{i,j}&\begin{aligned}&(i\leq n-k\wedge j\leq n-k)\n\vee&(i>n-k\wedge j>n-k)\end{aligned}\n-A_{i,j}&\text{otherwise}\end{cases}\\
\Rightarrow H = S(U(n-k)\times U(k))
\end{multeq}

\end{document}


You may need some other changes, depending quite wh\t input markup you want, the above produces

• Actually I wanted the new environment to be a short cut for the construction with gather. So to enter math mode I'd rather use gather. (I indeed forgot to put that into my code). Is that possible?
– bodo
Commented Jan 26, 2013 at 11:12
• When I try it with gather, I get ! Paragraph ended before \gather was complete.
– bodo
Commented Jan 26, 2013 at 11:15
• yes just get rid of the  I added and replac should work by gather Commented Jan 26, 2013 at 11:15
• Maybe I need to use \Collect@Body from environ?
– bodo
Commented Jan 26, 2013 at 11:33

After looking at some macro expansions, I found the actual problem. amsmath defines \let\\\math@cr when it expands gather. So whatever I do to \\ beforehand, will not matter. Thus I changed \math@cr et voilá, it works. At least if I collect the body with NewEnviron.

\documentclass{article}
\usepackage[fleqn]{amsmath}
\usepackage{environ}

\makeatletter
\NewEnviron{multeq}{%
\begingroup
\let\n\math@cr
\begin{gather}
\def\math@cr{\end{aligned}\n\begin{aligned}}
\begin{aligned}
\BODY
\end{aligned}
\end{gather}
\endgroup
}
\makeatother


is the needed definition. It also works with align instead of gather or split instead of aligned. And for a simpler usage example:

\begin{document}
\begin{multeq}
0 + 0 = 0\n
0 * 0 = 0\\
1 + 1 = 2\n
1 * 1 = 1
\end{multeq}
\end{document}


will be printed as