Commenting an equation with a vertical bar

I want to use a vertical bar to comment on solving a simple equation.

Do you know a way to do that with LaTeX ?

• Welcome to TeX.SE! – Sebastiano Dec 17 '18 at 21:40

3 Answers

Yes, it's possible:

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

\begin{document}

\begin{equation*}
\renewcommand{\arraystretch}{1.2}
\setlength{\arraycolsep}{0pt}
\begin{array}{
>{\displaystyle}r   % right aligned
>{\displaystyle{}}l % left aligned
@{\hspace{4em}}     % spacing
|                   % vertical rule
@{\hspace{2em}}     % spacing
>{\displaystyle}r   % comments
}
2x+2 &= 7x+3 & -2 \\
2x   &= 7x+1 & -7x \\
-5x  &= 1    & \div(-5) \\
x    &= \boxed{-\frac{1}{5}} & \\
\end{array}
\end{equation*}

\end{document}


If you're satisfied by the spacing, you can make this into an environment:

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

\newenvironment{solve}
{%
\renewcommand{\arraystretch}{1.2}%
\setlength{\arraycolsep}{0pt}%
\begin{array}{
>{\displaystyle}r   % right aligned
>{\displaystyle{}}l % left aligned
@{\hspace{4em}}     % spacing
|                   % vertical rule
@{\hspace{2em}}     % spacing
>{\displaystyle}r   % comments
}%
}%
{\end{array}}

\begin{document}

\begin{equation*}
\begin{solve}
2x+2 &= 7x+3 & -2 \\
2x   &= 7x+1 & -7x \\
-5x  &= 1    & \div(-5) \\
x    &= \boxed{-\frac{1}{5}} & \\
\end{solve}
\end{equation*}

\end{document}


It almost seems better to use a different format, which allows you easier control of numbering elements within a sequence if needed.

\documentclass{article}

\usepackage{amsmath}

\newcommand{\eqcomment}[1]{& \triangleright~ & #1}

\begin{document}

\begin{align*}
2x + 2 &= 7x + 3 \eqcomment{-2}       \\
2x &= 7x + 1 \eqcomment{-7x}      \\
-5x &= 1      \eqcomment{\div(-5)} \\
x &= \boxed{-\frac{1}{5}}
\end{align*}

\end{document}


One can play around with the alignment and notation.

Two other possibilities:

\documentclass{article}
\usepackage{showframe}
\renewcommand{\ShowFrameLinethickness}{0.3pt}
\usepackage{eqparbox}
\usepackage{amsmath,array}

\begin{document}

\begin{flalign*}
\qquad & & 2x+2 &= 7x+3 &\smash{\rule[-4.5\baselineskip]{0.5pt}{5.5\baselineskip}} \quad\eqmakebox[B][r]{$-2$} & \\
& & 2x &= 7x+1 &\eqmakebox[B][r]{$-7x$}& \\
& & -5x &= 1 & \eqmakebox[B][r]{$\div(-5)$} & \\
& & x &= \boxed{-\frac{1}{5}} & &
\end{flalign*}

\begin{alignat*}{2}
\qquad2x+2 &= 7x+3 &\hspace{6em}\smash{\rule[-5\baselineskip]{0.5pt}{6\baselineskip}} \qquad-2 & \\
2x &= 7x+1 & -7 x & \\
-5x &= 1 &\div(-5) & \\
x &= \boxed{-\frac{1}{5}} & &
\end{alignat*}

\end{document}