# What is the most elegant way to get a diagram showing elimination method to solve simultaneous equations?

First of all, I show you my effort as follows. It seems to be too complicated in coding with unsatisfied output.

\documentclass[border=12pt,12pt,varwidth]{standalone}
\usepackage{mathtools}

\begin{document}
\begin{aligned} \! \begin{aligned} 3x +2y &=7 \\ 9x +8y &=22 \end{aligned} \left| \! \begin{aligned} \times 3\\ \times 1 \end{aligned} \right| &\! \begin{aligned} 9x +6y &=21 \\ 9x +8y &=22 \end{aligned}\\ &\! \begin{aligned} -2y &=-1 \\ y &=1/2 \end{aligned} \end{aligned}
\end{document}


What I want to achieve are

• The coding must use as minimal number of keystrokes as possible.
• The output should look as follows,

where all spaces are well balanced, all equal signs are well aligned, the horizontal line exists to show the process of subtraction or addition, the sign of the process can be changed to either plus or minus, and the vertical lines are tall enough just to separate the left and right equations (no excessive vertical line).

How is the most elegant way to get such a diagram?

• Note that I used MS Paint to edit my original output such that it becomes the ideal output I want to achieve. That is why there is no code for the ideal output. – kiss my armpit May 16 '14 at 6:21
• Here's version 2... – Werner May 16 '14 at 6:40

An elementary array implementation:

\documentclass{article}
\usepackage{amsmath}

\begin{document}
$\begin{array}{r@{}l@{\quad}l@{\quad}r@{}l@{}c} 3x + 2y & {}= 7 & \xrightarrow{\times 3} & 9x + 6y & {}= 21 \\[\jot] 9x + 8y & {}= 22 & \xrightarrow{\phantom{\times 3}} & 9x + 8y & {}= 22 & ~\smash{\raisebox{.8\normalbaselineskip}{+}} \\ \cline{4-5} & & & - 2y & {}= -1 \\[\jot] & & & y & {}= 1/2 \\ \end{array}$
\end{document}


One can play with the rule length in or to reduce the overhang on the right. For this, perhaps use \rlap{$-1$} and \rlap{1/2}.

• The best answer but not the perfectest one. :-) – kiss my armpit May 19 '14 at 5:31
• I didn't understand, why the solution with redundant keystrokes (\smash,\raisebox, .8\normalbaselineskip, &=, \xrightarrow, & &, etc. which have to type the user, is signed as best one. What is bad about \gem macro? – wipet May 19 '14 at 18:11
• @wipet: I could write that in a macro also, if you wish. – Werner May 19 '14 at 19:10

From the question doesn't follow that LaTeX is necessary but the coding must use as minimal number of keystrokes as possible''. So I suggest the following solution with minimal external resources and without LaTeX:

\input opmac

\def\gem{%
\def\tabiteml{$\enspace}\def\tabitemr{\enspace$}
\def\0{\phantom0}
\def\grule##1{\multispan3&\multispan2\hrulefill
\lower\tmpdim\rlap{ \ $##1$\phantom+}\cr}
\def\result{&\omit&\omit&}
\table{rl|c|rl}
}
$$\gem{ 3x + 2y = \07 & \times3 & 9x + 6y = 21 \cr 9x + 8y = 22 & \times1 & 9x + 8y = 22 \cr \grule - \result -2y = -1 \cr \result y = 1/2 }$$
\end


Now, you can run the command pdftex document.

May17: I rewrote my solution without using opmac only by basic TeX commands. So the macro code is more mysterious but the user can use exactly the same macro \gem as above. It will work in LaTeX too.

\def\gem{%
\lccode\~=\= \lowercase{\def~}{&{}=}
\catcode\==13
\def\0{\phantom0}
\def\grule##1{\multispan3&\multispan2\hrulefill
\lower\dimen0\rlap{ \ $##1$\phantom+}\cr}
\def\result{&\omit&\omit&}
\def\myhalign##1##2{\vcenter{\halign{##1\cr##2\crcr}}}%
\myhalign{\hfil$##$&$##$\hfil\quad\strut\vrule&\quad\hfil$##$\hfil\quad\vrule
&\quad\hfil$##$&$##$\hfil}
}
$$\gem{ 3x + 2y = \07 & \times3 & 9x + 6y = 21 \cr 9x + 8y = 22 & \times1 & 9x + 8y = 22 \cr \grule - \result -2y = -1 \cr \result y = 1/2 }$$


Note that the \gem macro needs minimal number of keystrokes from all solutions mentioned here and it outputs exactly the result given in the question.

• Where can I find opmac? – morbusg May 16 '14 at 13:27
• @morbusg: In TeXlive or google "olsak opmac" – wipet May 16 '14 at 15:23

Here, I use tabstackengine features to build some useful macros that will allow for a compact syntax you desire. One could, of course, reduce the keystrokes further by \letting things like \alignLongunderstack to shortcuts like \aLus, etc.

Note that, if one of the \times multipliers requires two or more digits, that particular stack will be left-aligned by my aesthetic interpretation, though the alignment can be changed with the optional argument on \Longunderstack in the definition of \multsimeq.

REVISED to handle added case commented on by OP.

\documentclass{article}
\usepackage{tabstackengine,scalerel}
\stackMath
\newcommand\solveop[1]{\smash{\protect\raisebox{-.4\baselineskip-2.5pt}{${}#1{}$}}}
\newcommand\multsimeq[2]{%
\,\,\stretchleftright{\vert}{\,\Longunderstack[l]{\times #1\\ \times #2}\,}{\vert}}
\def\xlu#1{%
\setbox0=\hbox{$#1{}$}\setbox1=\hbox{$\,\,$}%
\stackunder[.4\baselineskip]{#1{}}{\protect\rule{\wd0+\wd1}{.5pt}}%
}
\setstackgap{L}{1.2\baselineskip}\def\stacktype{L}
\setstacktabulargap{0pt}
\begin{document}
\alignLongunderstack{ 3x + 2y =& 7\\ 9x + 8y =& 22 } \multsimeq{3}{1} \alignLongunderstack{ 9x + 6y = & 21\\ \xlu{9x + 8y =}& \xlu{22}\solveop{-}\\ -2y= & -1\\ y = & 1/2 }%
$\TABbinary \tabularLongunderstack{lcr}{ 2x - 5y &=& 2\\ 10x &=& 5 } \multsimeq{5}{1} \tabularLongunderstack{lrcl}{ 10x&\!\!{}-{}25y&=&10\\ \xlu{10x}&\xlu{\,~~~~~~} &\xlu{\,=}& \xlu{~\,5}\solveop{-}\\ &{-}25y&=&5\\ & y&=& -1/5 }$
\end{document}


The use of the math environment $...$ is optional in this MWE, because the solution is composed solely of stacks and stretches. The \stackMath directive will force all stacking commands to process their arguments in math mode. Likewise, scalerel macros process arguments in math by default.

• The shortcoming of this answer is when for example we have 2x - 5y =2 and 10x = 5, the alignment becomes no longer well positioned. – kiss my armpit May 19 '14 at 15:28
• @MoneyOrientedProgrammer Please see revision. – Steven B. Segletes May 19 '14 at 17:03
• It is more complicated than Werner's. – kiss my armpit May 19 '14 at 19:51
• @MoneyOrientedProgrammer Acknowledged. – Steven B. Segletes May 19 '14 at 23:31

This is what I came up with at first:

\documentclass[border=12pt,12pt,varwidth]{standalone}
\usepackage{empheq}
\usepackage{nicefrac}

\begin{document}
\begin{empheq}{align*}
3x +2y =7  \xrightarrow{\times 3} 9x +6y           &=21 \raisebox{-0.3cm}{\makebox[0.2cm]{\hrulefill}}\\
9x +8y =22 \xrightarrow{\phantom{\times 3}} 9x +8y &=22                                               \\[0.5em]
-2y &=-1                                               \\
y &=\nicefrac{1}{2}
\end{empheq}
\end{document}


But then I realised that this isn't really the optimal solution for several reasons:

1. This layout confuses the reader as to what direction represents time - sometimes it's to the right and sometimes it's downwards. It would be better if there were a way to consistently move through iterations of the solution such that it is obvious by following the page how the equations changed before they were solved.

2. This layout limits your ability to annotate each step because at some points more than one formula is on the same line. Obviously this is not a concern with such a simple piece of maths, but if the idea is to scale up to harder things then this could prove unhelpful in the future.

3. This layout is not easy to set well, and looks bad when set less than well.

4. For similar reasons as 2, it is hard to label either of the formulae because there are other elements both in their X and Y axes.

I spent less time on this alternative, but I think it will serve your needs better.

\documentclass[border=12pt,12pt,varwidth]{standalone}
\usepackage{empheq}
\usepackage{nicefrac}

\begin{document}

\begin{empheq}{align}
\nonumber &\text{A}      &\text{B}                                               \\
3x + 2y &= 7 & 9x + 8y &= 22             &\text{The question}                  \\
9x + 6y &= 21& 9x + 8y &= 22             &\text{Formula A $\times$ 3}          \\
6y &= 21& 8y      &= 22             &\text{Cancel 9$x$ from both A and B} \\
&    &       2y&=1               &\text{Formula B $-$ Formula A}       \\
&    &        y&=\nicefrac{1}{2} &\text{Checkmate}
\end{empheq}

\end{document}


It is mainly based on Werner's answer, Mico's answer and Herbert's answer scattered around this site. I display here the final code just for sharing purpose. Therefore, wiki is attached to this answer such that I will not receive any reps.

\documentclass[preview,border=12pt,12pt]{standalone}
\usepackage{mathtools,booktabs,array}

\newcolumntype\specifier{%
r@{}r@{}l
\newcommand\ope[1]{~\smash{\raisebox{.8\normalbaselineskip}{$#1$}}}
$\begin{array}{\specifier} \equ{3x}{+3y}{7} & \act{1} & \equ{3x}{+3y}{7} \\[\jot] \equ{3x}{}{22} & \act{3} & \equ{3x}{}{66} & \ope{-} \\\cmidrule{5-7} && & & \equ{}{{{-3y}}}{59} \\[\jot] && & & \equ{}{y}{-59/3} \end{array}$
`