# Simplifying macro for the align* environment for two column equations

The align* environment provides for a two column layout of equations, similar to the eqnarray* environment.

\begin{align*}
[left side of the first column]     &= [first line of the first column]
&  [left side of the second column]    &= [first line of the second column]  \\
&= [second line of the first column] & &= [second line of the second column] \\
&= [third line of the first column]  & &= [third line of the second column]  \\
% .... and so on
\end{align*}


Not only is this usage cumbersome. It is also confusing due to the fact that you need to write both the line of the first column and the line of the second column consequtively in one tex line.

So I'm trying to write a macro that simplifies this by first reading in the lines seperately per column. And then substituting the values in the cumbersome scheme given above.

Sketch of my idea of the macro:

help1{...[reading in the lines of the first column]...}
help2{...[reading in the lines of the second column]...}
macro{...[substituting the values from the helpmacro's help1 and help2]...}


I ran into 2 problems:

• the number of lines per column is variable. How do you read in a variable number of strings? (In my case the right sides of an equation);
• the strings need to be systematically stored in some kind of registers. I suppose these need to be allocated since the number of lines per column is determined at runtime.

Since I'm a beginner in TeX programming I would like to ask some advise. For instance is my method a practical one? And what kind of solutions are there for the two problems listed above?

Suggestions are welcome.

Minimum working example:

\documentclass[10pt]{article}

\usepackage{amsmath}

\newcommand{\ov}{\overline}

\begin{document}

\begin{align*}
\frac{ |\phi^+(x) - \phi^+(y)| }{ |x-y| }  &= \frac{ |(ax+b) - (ay+b)| }{       |x-y| }
&  \frac{ |\phi^-(x) - \phi^-(y)| }{ |x-y| }  &= \frac{ |(a\ov x+b) - (a\ov      y+b)| }{ |x-y| } \\
&= \frac{ |a(x-y)| }{ |x-y| }
& &= \frac{ |(a(x-y)| }{ |x-y| }   \\
&= |a| \frac{ |x-y| }{ |x-y| }   \\
&= |a|
\end{align*}

\end{document}


This compiles into this pdf:

-
Welcome to TeX.SE! Please provide an entire MWE (minimum working example, starting with \documentclass and ending with \end{document}, rather than some artificial code snippets. –  Mico Mar 10 '14 at 16:16
A tip: You can use backticks  to mark your inline code as I did in my edit. –  Adam Liter Mar 10 '14 at 16:33
By the way, that worked example is hardly minimal! Using simpler formulas would have made the source simpler and clearer, concentrating on the actual problem (the high-level formatting of the equations) without it getting lost in the forest. –  David Richerby Mar 10 '14 at 22:47

## 2 Answers

Stacks are an alternative. However, there is one drawback: if the equation heights of the left side and the right side are unequal, then the vertical placement of the rows on the left and right side won't be matched.

In the example you show, it should work just fine. Also, if you opt for stacks rather than TABstacks, you can use \usepackage[usestackEOL]{stackengine} rather than \usepackage{tabstackengine}.

EDIT to clarify some questions by the OP. The stackengine will, by default, stack text, not math. To set the mode so that it stacks math, without having to enclose everything in \$ delimiters, one merely invokes \stackMath. Note that stacked math will, however be in \textstyle, unless \displaystyle (or \dfrac) is invoked. To go back to stacking text, one invokes \stackText.

The macro \mathrel{} is a LaTeX feature that identifies its argument (in this case, the stack) as a math relation. In so doing, it will create the "proper" amount of space about it (without it, the = sign would be crammed up against the material to the left of the equal sign).

There are two key points to \Shortunderstack:

1) a "short" stack is defined by the package to specify a fixed gap distance between stacked rows, as opposed to a "long" stack that provides a fixed distance between the baselines of adjacent rows (see stackengine package docs for good description of this). If your stack could be of variable height (generally true of math expressions), one must either use a Short stack to avoid row overlap, or else one can use a Long stack if the interrow baseline skip is set large enough for the tallest element. In this package, the short stack gap (default 3pt) is reset with \setstackgap{S}{length} and the long stack gap (default \baselineskip) is reset with \setstackgap{L}{length}.

2) An "understack" is one in which the top element is placed on the baseline, and the subsequent rows build DOWN from the baseline. This is in contrast to a "stack" in which the last row sits on the baseline, and the preceding elements are built UP from it.

\documentclass[10pt]{article}

\usepackage{amsmath}
\usepackage{tabstackengine}
\stackMath
\newcommand{\ov}{\overline}

\begin{document}
ALIGN*
\begin{align*}
\frac{ |\phi^+(x) - \phi^+(y)| }{ |x-y| }  &= \frac{ |(ax+b) - (ay+b)| }{       |x-y| }
&  \frac{ |\phi^-(x) - \phi^-(y)| }{ |x-y| }  &= \frac{ |(a\ov x+b) - (a\ov      y+b)| }{ |x-y| } \\
&= \frac{ |a(x-y)| }{ |x-y| }
& &= \frac{ |(a(x-y)| }{ |x-y| }   \\
&= |a| \frac{ |x-y| }{ |x-y| }   \\
&= |a|
\end{align*}

STACK:
$\frac{ |\phi^+(x) - \phi^+(y)| }{ |x-y| } \mathrel{\Shortunderstack[l]{ = \dfrac{ |(ax+b) - (ay+b)| }{ |x-y| }\\ = \dfrac{ |a(x-y)| }{ |x-y| }\\ = |a| \dfrac{ |x-y| }{ |x-y| } \\ = |a| }} \quad \frac{ |\phi^-(x) - \phi^-(y)| }{ |x-y| } \mathrel{\Shortunderstack[l]{ = \dfrac{ |(a\ov x+b) - (a\ov y+b)| }{ |x-y| }\\ = \dfrac{ |(a(x-y)| }{ |x-y| } }}$
\end{document}


\documentclass{article}
\usepackage{amsmath}
\usepackage{tabstackengine}
\stackMath
\begin{document}
Using align*
\begin{align*}
[lft side of the first col]     &= [first line of the first col]
&  [lft side of the second col]    &= [first line of the second col]  \\
&= [second line of the first col] & &= [second line of the second col] \\
&= [third line of the first col]  & &= [third line of the second col]  \\
% .... and so on
\end{align*}

Using a 2 stacks
$[lft side of the first col]\mathrel{\Shortunderstack[l]{% = [first line of the first col] \\ = [second line of the first col] \\ = [third line of the first col]% }} \quad [lft side of the second col]\mathrel{\Shortunderstack[l]{% = [first line of the second col]\\ = [second line of the second col]\\ = [third line of the second col]% }}$

Using TABstack
\alignShortstack{ [lft side of the first col] =& [first line of the first col] \\ =& [second line of the first col] \\ =& [third line of the first col] } \quad \alignShortstack{ [lft side of the second col] =& [first line of the second col] \\ =& [second line of the second col] \\ =& [third line of the second col] }
\end{document}


-
Could you explain what \stackMath, \mathrel and \Shortunderstack[1] do? And why do you need to use \dfrac instead of \frac? –  Mussé Redi Mar 10 '14 at 16:51
@MusséRedi see revised description. –  Steven B. Segletes Mar 10 '14 at 17:05
What do you mean by variable height and baseline? An what is the difference between a long and a short stack again? –  Mussé Redi Mar 10 '14 at 17:22
@MusséRedi All of your definitional questions are answered on pages 1-2 of stackengine.pdf, found at ctan.org/tex-archive/macros/latex/contrib/stackengine –  Steven B. Segletes Mar 10 '14 at 17:37
@MusséRedi When I speak of variable height, I refer to stacks in which each of the rows have a different "natural" height. The points of note, as pertains to your question are: 1) by breaking the line into two unique stacks, the left stack doesn't know what the right stack is doing, and vice versa (unlike align, which will make sure the 2nd row on the left hand equation is properly matched to the 2nd row on the right-hand equation); and 2) if row 3 of the left stack has different natural height than row 4 of left stack, you need to tell the stack how to deal with it (short vs. long). –  Steven B. Segletes Mar 10 '14 at 17:44

You can use aligned:

\documentclass[10pt]{article}
\usepackage{geometry}% larger text width
\usepackage{amsmath}

\begin{document}
\begin{flalign*}
\frac{|\phi^+(x) - \phi^+(y)|}{|x-y|}
\!\begin{aligned}[t]
&= \frac{|(ax+b) - (ay+b)|}{|x-y|} \\
&= \frac{|a(x-y)|}{|x-y|} \\
&= |a| \frac{|x-y|}{|x-y|} \\
&= |a|
\end{aligned}
&&
\frac{|\phi^-(x) - \phi^-(y)|}{|x-y| }
\!\begin{aligned}[t]
&= \frac{|(a\bar{x}+b) - (a\bar{y}+b)|}{|x-y|} \\
&= \frac{|a(x-y)|}{|x-y|}
\end{aligned}
\end{flalign*}
\end{document}


Keeping the pieces aligned requires using phantoms, if they haven't the same height.

For historical reasons, aligned (like gathered) adds a thin space that we remove with \!.

-
What is the difference between the flalign*- and aligned environments? And can't you use \quad instead of && ? What does the [t] option stand for? –  Mussé Redi Mar 10 '14 at 19:09
With flalign you use the whole width and && is necessary to go to the next column group. The [t] option is for setting the aligned` level with the first part of the equation. –  egreg Mar 10 '14 at 19:11