# Typesetting explanations of algebra steps

I'm making a WordPress site explaining high school math, typesetting the math with the QuickLatex plugin. I mention that in case there are any compatibility issues, but so far it seems to work like any LaTeX program I've used.

I want to typeset explanations of the steps in algebra. For instance, sometimes an algebra book will explain to subtract 3x from each side of an equation via a diagram like this:

 3x + 8 - 2y = 6x
-3x           -3x
8 - 2y = 3x


I am trying to use aligned mode, but it doesn't work all that well.

My first try uses one ampersand:

\begin{aligned}
3x + 8 - 2y &=  6x \\
-3x          &  -3x \\
8 - 2y &= 3x
\end{aligned}


It looks like this:

As you can see, the -3x on the left side isn't lined up. So I decided to try another ampersand, here between the + and the 8:

\begin{aligned}
3x + & 8 -2y  &=  6x \\
-3x   &        &  -3x \\
& 8 -2y  &=  3x
\end{aligned}


That looks like this:

Better, but the 3x and the + and the 8 are jammed together.

I tried a number of other things, adding more ampersands, and the whole thing got more and more complicated, and more and more messed up. So I thought about using phantom symbols: typing more symbols than needed on each line, in a way so that the lines are aligned, and then hiding the symbols that shouldn't show.

\begin{aligned}
3x          +8-2y   &          =  6x \\
-3x \phantom{+8-2y}  & \phantom{=} -3x \\
8-2y   &          =  3x
\end{aligned}


This seems better. Here it is:

But it's still a struggle: the results are often hard to predict. If there is a better way to go about this, let me know.

Also I may add some kind of annotation to the lines of equations, including the lines that don't have an equals sign, so it may be handy to use some method that allows for that nicely.

I also want to ask about adding annotation. I tried the answer mentioned below with the array package, and used one column of the array to enter annotation, like this:

\begin{system}
3x & + & 2x & = & 8 & \text{(original equation)} \\
&   & 5x & = & 8 & \text{(after combining the 2x and 3x)}
\end{system}


But the problem is that there is not sufficient padding between the annotation text and the right-most term in some of the equations.

• I think when one wants very specific typesetting or alignment, then the conveniences that the packages give in the usual case don't apply and the value is really minimal, so one may be better off not using them and just specifying everything manually. (How to do that in this case I don't know, though.) – ShreevatsaR May 4 '17 at 6:25

Remember that aligned makes pairs of right-left aligned columns.

This is a different possibility with array:

\documentclass{article}
\usepackage{amsmath,array}

\newenvironment{system}
{\renewcommand{\arraystretch}{1.5}% like aligned does
\begin{array}{
@{}r@{}
*{\value{MaxMatrixCols}}{
>{{}}c<{{}}@{}
>{\mathopen{}}r@{}
}
}%
}{\end{array}}

\begin{document}

$\begin{system} 3x & + & 8 & - & 2y & = & 6x \\ -3x & & & & & & -3x \\ & & 8 & - & 2y & = & 3x \end{system}$

\end{document}


With autoaligne, with some pulls to coerce it into good behavior:

\documentclass{article}
\usepackage{autoaligne}

\begin{document}

\catcode =9 \definirseparateurs{\\}{=||V}{+||-} \begingroup\lccode~=V \lowercase{\endgroup\let~}\relax \mathcodeV="8000 \autoaligne{ 3x + 8 - 2y = 6x\\% \-3x V \-3x\\% + 8 - 2y = 3x }

\end{document}


• Would you happen to know how to make your 'system' solution work with MathJax (which I am now using instead of WordPress plugins)? – composerMike May 9 '17 at 21:49
• @composerMike No: MathJax has very limited capabilities, compared to TeX. – egreg May 9 '17 at 22:00
• Thanks for the reply. Maybe I don't understand; seems like MathJax has almost all of TeX and most of LaTeX, see here docs.mathjax.org/en/latest/tex.html#tex-and-latex-extensions --- I think it doesn't support defining commands however, maybe you are saying your technique relies on that? – composerMike May 10 '17 at 1:32
• @composerMike MathJax uses a very different model from TeX. I'd rather say the former has shares some features with the latter, but it's not a typesetter. – egreg May 10 '17 at 5:58

I think the solution with \phantom has the best chance. The only thing is that you need to specify that the + in +8 inside the \phantom has something in front of it. That will influence the spacing. The same goes for the = sign. And in my opinion the -3x on the right side should not have it since the - is just a negative of 3x.

\begin{aligned}
3x          +8-2y   &          =  6x \\
-3x \phantom{{}+8-2y}  & \phantom{{}={}} {-3x} \\
8-2y   &          =  3x
\end{aligned}


If you also want to align the numbers to the right add \phantom{-} in front,

\begin{aligned}
3x             +8-2y   & =  \phantom{-}6x \\
-3x \phantom{{}+8-2y}  & \phantom{{}={}} {-3x} \\
8-2y                   & = \phantom{-}3x
\end{aligned}


You could try the packages systeme or autoaligne. But be aware that they sometimes enforce a specific layout (e.g. in the example look at the constant "8" which moved to the end in the first equation).

\documentclass{article}
\usepackage{systeme, autoaligne}

\sysequivsign{~}{\phantom{}}

\begin{document}

\sysdelim..
\systeme{
3x  + 8 - 2y   =  6x,
\-3x             ~ \-3x,
+ 8 - 2y   = 3x
}

\bigskip

\autoaligne{
3x  + 8 - 2y   =  6x\\
\-3x             = \-3x\\
+ 8 - 2y   = 3x
}

\end{document}


The combination of tabbed and regular stacks allows for easy annotation.

\documentclass{article}
\usepackage{tabstackengine}
\TABstackMath
\TABbinary
\begin{document}
\tabbedLongunderstack[r]{
3x& + 8& - 2y =& 6x \\
{-3x}&    &       & {-3x} \\
&   8& - 2y =& 3x
}
\Longunderstack[l]{
original equation\\
after combining the $2x$ and $3x$
}
\end{document}


A slightly different (possibly preferable) look can be achieved by shifting the alignment point on the equals sign:

\documentclass{article}
\usepackage{tabstackengine}
\TABstackMath
\TABbinary
\begin{document}
\tabbedLongunderstack[r]{
3x& + 8& - 2y &= 6x \\
{-3x}&    &      &  {-3x} \\
&   8& - 2y &= 3x
}
\Longunderstack[l]{
original equation\\
after combining the $2x$ and $3x$
}
\end{document}


I would add only, in closing, that while the particular example given by the OP was facilitated as shown above, the most general approach, that could handle arbitrary terms and multipliers, would involve more alignment points (on both sides of +, -, and =), e.g.:

\tabbedLongunderstack[r]{
3x &+& 8 &-& 2y &=& 6x \\
{-3x} & &   & &    & & {-3x} \\
& & 8 &-& 2y &=& 3x
}


In this case, the second style of presentation can be achieved with judicious use of \mathllap (mathtools package) on the {-3x} term(s).