# Draw a red box around certain parts of multiple lines

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. • Welcome to TeX.SX! Could you please make your example compilable? – TeXnician Nov 24 '17 at 6:51

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} A solution with hf-tikz:

\documentclass{article}
\usepackage{amsmath}

\usepackage[customcolors]{hf-tikz}
\tikzset{offset def/.style={
above left offset={-0.2,0.5},
below right offset={0.35,-0.3},
},
color def/.style={
offset def,
set fill color=white,
set border color=red,
},
}

\begin{document}
\begin{align*}
(IV) - (V) & \Rightarrow 4x_1 = 8 & \tikzmarkin[color def]{z}\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\tikzmarkend{z}
\end{align*}

\end{document}


The result: Check the package documentation for more options, including one to prevent the rounded corners.

Using tikz :

\documentclass[french]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{babel}

\usepackage{amsmath}

\usepackage{tikz}
\usetikzlibrary{fit}

\newcommand{\tikzmark}{%
\tikz[remember picture,baseline=(#1.base)]
\node[text=black,anchor=center,inner sep=1pt] (#1) {#2};}

\begin{document}
\begin{align*}
(IV) - (V) & \Rightarrow 4x_1 = 8 & \Rightarrow\tikzmark{a}{} 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 = \tikzmark{b}{} 3
\end{align*}

\tikz[overlay,remember picture]{
\node[rectangle, draw=red,thick, minimum size=2cm ] at (barycentric cs:a=1,b=1) {};
}

\end{document} 