[Tex/LaTex] Draw a red box around certain parts of multiple lines

equationsformattinghighlighting

I would like to highlight the values for variables of a system of linear equations in the way shown in the picture below.

\begin{align*}
(IV) - (V) & \Rightarrow 4x_1 = 8 & \Rightarrow x_1 = 2 
\\ x_1 = \text{2 in (V):} & \Rightarrow 7 * 2 + 5x_2 = 34 & 
\Rightarrow x_2 = 4 \\ x_1 = \text{2 und } x_2 =  \text{4 in (II):} & 
\Rightarrow 3 * 2 + 4 + x_3 = 13 &\Rightarrow x_3 = 3
\end{align*}

This is the code I used, if its relevant.

How it should look like

Best Answer

Here a possible solution with a TikZ matrix of math nodes:

\documentclass{article}
\usepackage{amsmath,amssymb}
\usepackage{tikz}
\usetikzlibrary{matrix}

\begin{document}
\noindent Your example as a benchmark:
\begin{align*}
(IV) - (V) & \Rightarrow 4x_1 = 8 & \Rightarrow x_1 = 2 
\\ x_1 = \text{2 in (V):} & \Rightarrow 7 * 2 + 5x_2 = 34 & 
\Rightarrow x_2 = 4 \\ x_1 = \text{2 und } x_2 =  \text{4 in (II):} & 
\Rightarrow 3 * 2 + 4 + x_3 = 13 &\Rightarrow x_3 = 3
\end{align*}
My solution with a Ti\emph{k}Z matrix:
\begin{center}
\begin{tikzpicture}
    \matrix[matrix of math nodes,
        column 1/.style={nodes={anchor=east}},
        column 2/.style={nodes={anchor=west}},
        row sep=-.4ex
        ] (m) {
        (IV) - (V) &[-4pt] \Rightarrow 4x_1 = 8 &[30pt] \Rightarrow x_1 = 2 
        \\ 
        x_1 = \text{2 in (V):} & \Rightarrow 7 * 2 + 5x_2 = 34 & 
        \Rightarrow x_2 = 4 \\ 
        x_1 = \text{2 und } x_2 =  \text{4 in (II):} & 
        \Rightarrow 3 * 2 + 4 + x_3 = 13 &\Rightarrow x_3 = 3\\
    };
    \draw[red, line width=2pt] ([shift={(-4pt,2pt)}]m-1-3.north west) rectangle ([shift={(4pt,-2pt)}]m-3-3.south east);
\end{tikzpicture}
\end{center}
\end{document}

enter image description here

Related Question