[Tex/LaTex] How to add circles and arrows in linear equation systems

casesequationsmath-mode

How can I write a linear equation system that uses circles and arrows to indicate what should be added on each line.

This is what I have so far:

$S = \begin{cases}
    x_1+2x_2 - 2x_3 = 1 \\
    2x_1 - x_2 + x_3 = 3 \\
    x_1 + 3x_2 + x_3 = 1
\end{cases}$

enter image description here

And it should look like this:

enter image description here

Best Answer

One option using TikZ:

New version:

\documentclass{article}
\usepackage{amsmath}
\usepackage{systeme}
\usepackage{tikz}

\newcounter{tmp}

\newcommand\tikzmark[1]{%
\tikz[remember picture,baseline=-0.65ex]
  \node[inner sep=0,outer sep=0] (#1){};%
}

\newcommand\mess[4][25pt]{%
\stepcounter{tmp}%
\begin{tikzpicture}[remember picture,overlay,>=latex,xshift=#1,cyan]
  \node[circle,draw,cyan,inner sep=2pt] at ([xshift=#1]#2) (a\thetmp) {$#4$};
  \draw[->] (a\thetmp.south) |- (#3);
\end{tikzpicture}%
}

\begin{document}

\[
S = 
\systeme{x_1+2x_2 - 2x_3 = 1 \tikzmark{a},
    2x_1 - x_2 + x_3 = 3 \tikzmark{b},
    x_1 + 3x_2 + x_3 = 1 \tikzmark{c}}
\]

\mess{a}{b}{-2}
\mess[55pt]{a}{c}{-1}

\end{document}

Explanation:

First, you place some marks for the relevant lines using \tikzmark, then you use the \mess command to add the circles with their arrows; the three mandatory arguments for \mess are the string for the initial mark, the string for the final mark, and the text to be used inside the circle. The optional argument gives control over the length of the horizontal separation.

Notice also the use of the systeme package, so that the system of equations gets nicely typeset.

Since the code requires some internal calculation, two or three runs will be needed for the elements to stabilize.

enter image description here

First version

\documentclass{article}
\usepackage{amsmath}
\usepackage{tikz}

\newcommand\mess[2][20pt]{%
\begin{tikzpicture}[overlay,>=latex,yshift=22pt,xshift=#1,cyan]
  \node[circle,draw,cyan,inner sep=2pt] (a) {$#2$};
  \draw[->,shorten >= 3pt] (a.south) |- ([yshift=-9pt,xshift=-#1]a.south);
\end{tikzpicture}%
}

\begin{document}

\[
S = \begin{cases}
    \phantom{0}x_1+2x_2 - 2x_3 = 1 \\
    2x_1 - \phantom{0}x_2 + \phantom{0}x_3 = 3 \mess{-2}\\
    \phantom{0}x_1 + 3x_2 + \phantom{0}x_3 = 1 \mess[40pt]{-1}
\end{cases}
\]

\end{document}

enter image description here

The mandatory argument for \mess will be written in the circular node; the optional argument allows control over the length of the horizontal part of the arrow.