# How to highlight / uncover / connect by paths step by step various parts of math formulas blended with text?

I wrote the following code, that works, and approximately do the job. But it suffers from many flaws, I evaluate it as ugly, and would like to receive some advices on how to do same things the right way !

\documentclass{beamer}
\usepackage{etex}
\usepackage[utf8]{luainputenc}
\usepackage[T1]{fontenc}
\usepackage[french]{babel}
\usepackage{amsmath, amssymb, ifthen, calc}
\hypersetup{pdfencoding=utf8}
\usepackage{pgfplots, tikz, xargs}
\usetikzlibrary{overlay-beamer-styles}
\usetikzlibrary{matrix, fit, backgrounds, patterns, decorations.pathreplacing}

\begin{document}

\tikzstyle{every picture}+=[remember picture]

\begin{frame}[fragile]
\textbf{\Large Exemple 1 (simple)}

\tikzset{%
mes styles/.style={},
mes styles 1/.style={fill=white},
mes styles 2/.style={fill=green!20}}

\only<2-3>{\tikzset{mes styles/.style={mes styles 2}}}

\begin{tikzpicture}
\node[anchor=west] (R) at (0,0) {Résoudre} ;
\matrix[matrix of math nodes, nodes={minimum width=width("$-1$"), minimum height=1.5em}, align=left, column sep=-\pgflinewidth, row sep=-\pgflinewidth] at (5.5, -.5ex) (S1) {
|[mes styles]| $2$ & $x$ & $+$ &   & $y$ & $-$ & $5$ & $=$ & $0$ & $L_1$\\
|[mes styles]| \alt<3>{\color{red}{$\!\!-1\!\!$}}{$\!-\!$} & $x$ & $+$ & $2$ & $y$ & $-$ & $8$ & $=$ & $0$ & $L_2$\\ % How to avoid moves (question related to node size)
} ;
\draw[decorate, decoration={brace, mirror, amplitude=5pt}] (S1-1-1.north west) -- (S1-2-1.south west) ;
\fill<4>[red, opacity=.2] (S1-2-1.south west) rectangle (S1-2-9.north east) ;

\uncover<4-6>{
\node[anchor=west] at (0, -2) {d'où} ;
\matrix[matrix of math nodes, nodes={minimum size=width("$-2$"), minimum height=1.5em}, ampersand replacement=\&, row 3/.style={visible on=<5->},
below=of S1.south east, anchor=north east, align=left, yshift=1cm] (S2) {
$\phantom{-}2$ \& $x$ \& $+$ \&   \& $y$ \& $-$ \& $5$ \& $=$ \& $0$ \& $L_1$\\ % Why matrix of math nodes are not effective ? I need to add $...$
\alt<4>{\color{red}{$-2$}}{$-2$} \& $x$ \& $+$ \& $4$ \& $y$ \& $-$ \& $16$ \& $=$ \& $0$ \& $\color{red}2\color{black}L_2$\\
$\hphantom{-}0$ \& $x$ \& $+$ \& $5$ \& $y$ \& $-$ \& $21$ \& $=$ \& $0$ \& \rlap{$L_1+2L_2$} \\
} ;
\draw[decorate, decoration={brace, mirror, amplitude=5pt}] (S2-1-1.north west) -- (S2-2-1.south west) ;
\draw<5-9> (S2-3-1.north west) -- (S2-3-9.north east) ;
\uncover<5>{
\fill[green, opacity=.2]   (S2-1-1.north west) rectangle (S2-3-2.south east) ; % Why different nodes heights (ok for the lower part of y, though) ?
\fill[cyan, opacity=.2] (S2-1-3.north west) rectangle (S2-3-5.south east) ;    % I tried to set the height without success
\fill[blue, opacity=.2]  (S2-1-6.north west) rectangle (S2-3-7.south east) ;   % Why the colored blocks are overlapping by default ?
\fill[orange, opacity=.2] (S2-1-9.north west) rectangle (S2-3-9.south east) ;  %
}
}
\fill<4>[red, opacity=.2] (S2-2-1.south west) rectangle (S2-2-9.north east) ;
\draw<4>[line width=4pt, red, -stealth] (S1-2-9.east) to [controls=+(-10:1) and +(0:1)] node[right] {$\pmb{\times 2}$} (S2-2-9.east) ; % How to use a transparency group to connect the two rectangular zone with a nice arrow ? I mean, how to blend it with various \uncover not happening all at the same moment ?

\uncover<7-10>{
\matrix[matrix of math nodes, nodes={minimum size=width("$-2$"), minimum height=1.5em}, ampersand replacement=\&,
below=of S1.south east, anchor=north east, align=left, xshift=5pt, yshift=1.3cm] (S3) {
\hphantom{-0} \& \hphantom{x} \& \hphantom{+} \& $5$ \& $y$ \& $-$ \& $21$ \& $=$ \& $0$ \& \rlap{$L_1+2L_2$}\hphantom{$L_1$} \\
} ;
} ;

\uncover<7-11>{
\matrix[matrix of math nodes, nodes={minimum size=width("$-2$"), minimum height=1.5em}, ampersand replacement=\&,
below=of S3.south east, anchor=north east, align=left, xshift=5pt, yshift=1.5cm] (S4) {
|[visible on=<11>]| $x$ \& |[visible on=<11>]| $=$ \& |[visible on=<11>]| $\dfrac25$ \& \hphantom{5} \& |[visible on=<10>]| et \& \hphantom{-} \& $y$ \& $=$ \& $\dfrac{21}5$ \& \hphantom{$L_1$} \\
} ;
} ;

\uncover<10>{
\node[anchor=west] at (0,-2.8) (S5) {
$2x+\dfrac{21}5-5=0$ d'où $2x+\dfrac{21}5-\dfrac{25}5=0$ d'où $2x=\dfrac45$ d'où $x=\dfrac25$.
} ;
} ;

\uncover<12>{
\node[] at (5.5,-2) {% How to position this text properly and relatively to other elements ?
Ce système a pour solution $x=\dfrac25$ et $y=\dfrac{21}5$.
} ;
} ;
\end{tikzpicture}

\pause

\begin{enumerate}
\small
\item On remarque qu'éliminer $x$ se fera simplement en multipliant $L_2$ par \tikzmarknode[minimum height=1em, inner sep=1pt, fill=green!20, fill on=<2-3>]{R3}{$2$}
et en additionnant $L_1$ et $L_2$ : \pause
le coefficient de $x$ dans $L_2$, qui vaut initialement \tikzmarknode[minimum height=1em, inner sep=1pt, fill=green!20, fill on=<3>, text=red, text on=<3>]{R4}{$-1$}, \pause
passera alors à \tikzmarknode[minimum height=1em, inner sep=1pt, fill=red!20, fill on=<4>, text=red, text on=<4>]{R5}{$-2$}, \pause
ce qui par sommation avec $L_1$ permettra d'éliminer l'inconnue $x$.\pause
\item On obtient alors une équation à une inconnue facile à résoudre : $5y-21=0$ \pause d'où $5y=21$ d'où $y=\dfrac{21}5$.\pause
\item On remplace alors $y$ par sa valeur dans $L_1$ ou $L_2$, au choix. \pause Comme $y$ figure seul dans $L_1$, sans coefficient, on choisit $L_1$.

\end{enumerate}
\end{frame}

\end{document}


It renders to :

Here are the problems I encounter :

• I cannot fix the node size, thus there are some inappropriate "moves" (e.g. curly brace) and some colored rectangles with different sizes (because of the lower part of the y, for example).
• Matrix of math nodes does not work inside a tikzpicture environment ?
• Why my colored rectangles overlaps a little bit ?
• How to use a transparency group to connect two nodes with a nice arrow (how to blend it with various \uncover not happening all at the same moment) ?
• How to correctly align the multiple matrices and elements inside matrices ?
• How to reduce the spaces between scalar and unknown quantities ?

To answer the questions I could understand:

• Use alt=<3>{text=red}{} to make the text red on the 3rd slide.
• math in matrix of math nodes gets "killed" by align=left.
• The overlaps come from the outer seps of the nodes.
• Again, use e.g. alt=<5>{opacity=0.2}{opacity=0} instead of all these \uncover and so on commands.
• If you mean that the filled boxes were uneven: this can be cured by using the boundaries of the matrix rather than the nodes, e.g.

\fill[green] (S2-1-1.west|-S2.north) rectangle (S2-3-2.east|-S2.south) ;

The last item I do not understand. Here is the result:

\documentclass{beamer}
\usepackage{etex}
\usepackage[utf8]{luainputenc}
\usepackage[T1]{fontenc}
\usepackage[french]{babel}
\usepackage{amsmath, amssymb, ifthen, calc}
\hypersetup{pdfencoding=utf8}
\usepackage{pgfplots, tikz, xargs}
\usetikzlibrary{overlay-beamer-styles}
\usetikzlibrary{matrix, fit, backgrounds, patterns, decorations.pathreplacing}

\begin{document}

\tikzset{every picture/.append style={remember picture}}

\begin{frame}[fragile]
\textbf{\Large Exemple 1 (simple)}

\tikzset{%
mes styles/.style={},
mes styles 1/.style={fill=white},
mes styles 2/.style={fill=green!20}}

\only<2-3>{\tikzset{mes styles/.style={mes styles 2}}}

\begin{tikzpicture}
\node[anchor=west] (R) at (0,0) {Résoudre} ;
\matrix[matrix of nodes, cells={nodes={minimum width=width("$-1$"), minimum height=1.5em}},
align=left, %<-undoes math in matrix of math nodes
column sep=0pt,
row sep=-\pgflinewidth] at (5.5, -.5ex) (S1) {
|[mes styles]| $2$ & $x$ & $+$ &   & $y$ & $-$ & $5$ & $=$ & $0$ & $L_1$\\
|[mes styles,alt=<3>{text=red}{}]| $\!-\!$ & $x$ & $+$ & $2$ & $y$ & $-$ & $8$ & $=$ & $0$ & $L_2$\\ % How to avoid moves (question related to node size)
} ;
\draw[decorate, decoration={brace, mirror, amplitude=5pt}] (S1-1-1.north west) -- (S1-2-1.south west) ;
\fill[red,alt=<4>{opacity=.2}{opacity=0}] (S1-2-1.south west) rectangle (S1-2-9.north east) ;

\begin{scope}[visible on=<4-6>]
\node[anchor=west] at (0, -2) {d'où} ;
\matrix[matrix of nodes,cells={nodes={minimum size=width("$-2$"), minimum height=1.5em,outer sep=0pt}}, %<-outer sep fixes overlap
ampersand replacement=\&, row 3/.style={visible on=<5->},
below=of S1.south east,xshift={width("$2L_2$")-width("$L_2$")}, anchor=north east, align=left, yshift=1cm,
column sep=0pt] (S2) {
$\phantom{-}2$ \& $x$ \& $+$ \&   \& $y$ \& $-$ \& $5$ \& $=$ \& $0$ \& $L_1$\\ % Why matrix of math nodes are not effective ? I need to add $...$
\alt<4>{\color{red}{$-2$}}{$-2$} \& $x$ \& $+$ \& $4$ \& $y$ \& $-$ \& $16$ \& $=$ \& $0$ \& $\color{red}2\color{black}L_2$\\
$\hphantom{-}0$ \& $x$ \& $+$ \& $5$ \& $y$ \& $-$ \& $21$ \& $=$ \& $0$ \& \rlap{$L_1+2L_2$} \\
} ;
\draw[decorate, decoration={brace, mirror, amplitude=5pt}] (S2-1-1.north west) -- (S2-2-1.south west) ;
\draw<5-9> (S2-3-1.north west) -- (S2-3-9.north east) ;
\end{scope}
\begin{scope}[alt=<5>{opacity=0.2}{opacity=0}]
\fill[green]   (S2-1-1.west|-S2.north) rectangle (S2-3-2.east|-S2.south) ; % Why different nodes heights (ok for the lower part of y, though) ?
\fill[cyan] (S2-1-3.west|-S2.north) rectangle (S2-3-5.east|-S2.south) ;    % I tried to set the height without success
\fill[blue]  (S2-1-6.west|-S2.north) rectangle (S2-3-7.east|-S2.south) ;   % Why the colored blocks are overlapping by default ?
\fill[orange] (S2-1-9.west|-S2.north) rectangle (S2-3-9.east|-S2.south) ;  %
\end{scope}
\fill<4>[red, opacity=.2] (S2-2-1.south west) rectangle (S2-2-9.north east) ;
\draw<4>[line width=4pt, red, -stealth] (S1-2-9.east) to [controls=+(-10:1) and +(0:1)] node[right] {$\pmb{\times 2}$} (S2-2-9.east) ; % How to use a transparency group to connect the two rectangular zone with a nice arrow ? I mean, how to blend it with various \uncover not happening all at the same moment ?

\matrix[visible on=<7-10>,matrix of math nodes, nodes={minimum size=width("$-2$"), minimum height=1.5em}, ampersand replacement=\&,
below=of S1.south east, anchor=north east, align=left, xshift=5pt, yshift=1.3cm] (S3) {
\hphantom{-0} \& \hphantom{x} \& \hphantom{+} \& $5$ \& $y$ \& $-$ \& $21$ \& $=$ \& $0$ \& \rlap{$L_1+2L_2$}\hphantom{$L_1$} \\
} ;

\matrix[matrix of math nodes, nodes={minimum size=width("$-2$"), minimum height=1.5em}, ampersand replacement=\&,
below=of S3.south east, anchor=north east, align=left,
xshift=8pt, yshift=1.5cm,
visible on=<7-11>] (S4) {
|[visible on=<11>]| $x$ \& |[visible on=<11>]| $=$ \& |[visible on=<11>]| $\dfrac25$ \& \hphantom{5} \& |[visible on=<10>]| et \& \hphantom{-} \& $y$ \& $=$ \& $\dfrac{21}{5}$ \& \hphantom{$L_1$} \\
} ;

\node[anchor=west,visible on=<10>] at (0,-2.8) (S5) {
$2x+\dfrac{21}5-5=0$ d'où $2x+\dfrac{21}5-\dfrac{25}5=0$ d'où $2x=\dfrac45$ d'où $x=\dfrac25$.
} ;

\node[visible on=<12>] at (5.5,-2) {% How to position this text properly and relatively to other elements ?
Ce système a pour solution $x=\dfrac25$ et $y=\dfrac{21}5$.
} ;
\end{tikzpicture}

\pause

\begin{enumerate}
\small
\item On remarque qu'éliminer $x$ se fera simplement en multipliant $L_2$ par \tikzmarknode[minimum height=1em, inner sep=1pt, fill=green!20, fill on=<2-3>]{R3}{$2$}
et en additionnant $L_1$ et $L_2$ : \pause
le coefficient de $x$ dans $L_2$, qui vaut initialement \tikzmarknode[minimum height=1em, inner sep=1pt, fill=green!20, fill on=<3>, text=red, text on=<3>]{R4}{$-1$}, \pause
passera alors à \tikzmarknode[minimum height=1em, inner sep=1pt, fill=red!20, fill on=<4>, text=red, text on=<4>]{R5}{$-2$}, \pause
ce qui par sommation avec $L_1$ permettra d'éliminer l'inconnue $x$.\pause
\item On obtient alors une équation à une inconnue facile à résoudre : $5y-21=0$ \pause d'où $5y=21$ d'où $y=\dfrac{21}5$.\pause
\item On remplace alors $y$ par sa valeur dans $L_1$ ou $L_2$, au choix. \pause Comme $y$ figure seul dans $L_1$, sans coefficient, on choisit $L_1$.

\end{enumerate}
\end{frame}
\end{document}


• Sorry, was offline for some days. This looks much better, thanks again. Is there a way to get the two systems be conveniently aligned (on equal signs and zero) ? For the last query, I wanted to have multiplier coefficients closer to unknowns. But still far enough to let them be « hilighted » without flaw, and without being moved from one slide to the other. Sorry, my english is not sophisticated enough to make things more clear... – natsirt Oct 26 '19 at 13:37
• Thank you again. I don't know why your last comment was deleted, but I remeber it. I wanted to apologize for using my native language in examples : I did not thought it could annoy you (but you could have felt free to just got rid of these boring accented characters ;-) !). And I tried to express all my needs in the question, namely « How to correctly align the multiple matrices [and elements inside matrices] ? » and « How to reduce the spaces between scalar and unknown quantities ? ». I was probably not clear enough in the way I wrote my question, and want to apologize for this too. – natsirt Oct 30 '19 at 11:08
• @natsirt No need to apologize! My last comment was a bit rough, and I deleted it. – user194703 Oct 30 '19 at 11:27