How to draw arrows between boxed coefficients in a formula in beamer?

I am trying to draw an arrow to link two terms in a matrix multiplication just like in the picture below. I managed to create the coloured boxes but not the arrow to link both "c" terms. I have seen some solutions but they involve the creation of circled terms as nodes and linking them using as references. The equation is very simple and I provide it with the color boxes creation.


% colour boxes to highlight parts of equations
\newcommand{\highlightred}[1]{%
\colorbox{red!20}{$\displaystyle#1$}}

\newcommand{\highlightgreen}[1]{%
\colorbox{green!20}{$\displaystyle#1$}}

\newcommand{\highlightblue}[1]{%
\colorbox{blue!20}{$\displaystyle#1$}}

\begin{equation*}
(\highlightgreen{r}\times \highlightblue{c})\times(\highlightblue{c}\times \highlightred{p})\Longrightarrow (\highlightgreen{r}\times \highlightred{p})


• Please make your code compilable (if possible), or at least complete it with \documentclass{...}, the required \usepackage's, \begin{document}, and \end{document}. That may seem tedious to you, but think of the extra work it represents for the users willing to give you a hand. Help them help you: remove that one hurdle between you and a solution to your problem. Commented Jan 30 at 21:22

This is more or less one of the suggested use cases of the tikzmark package:

\documentclass{beamer}
\usepackage{tikz}
\usetikzlibrary{tikzmark}

\tikzset{
highlight red/.style={
inner sep=5pt,
minimum size=1.5em,
text depth=0pt,
fill=red!20
},
highlight green/.style={
inner sep=5pt,
minimum size=1.5em,
text depth=0pt,
fill=green!20
},
highlight blue/.style={
inner sep=5pt,
minimum size=1.5em,
text depth=0pt,
fill=blue!20
}
}

\begin{document}
\begin{frame}

\begin{equation*}
(
\tikzmarknode[highlight green]{n1}{r} \times
\tikzmarknode[highlight blue]{n2}{c}
) \times (
\tikzmarknode[highlight blue]{n3}{c} \times
\tikzmarknode[highlight red]{n4}{p}
) \Longrightarrow (
\tikzmarknode[highlight green]{n5}{r} \times
\tikzmarknode[highlight red]{n6}{p}
)
\end{equation*}

\tikz[remember picture, overlay] {
\draw[<->, blue] (n2.north east) to[bend left] (n3.north west);
}

\end{frame}
\end{document}


(Also works outside of beamer, but the title of your question suggests that you use this document class.)

• Thanks for your prompt reply but it doesn't work. The only result I got is just the characters of the syntax formula ([highlightgreen]n1r×[highlightblue]n2c)×([highlightblue]n3c×[... as if something is missing and only writes the code. I tried in a new standalone document to check if there were any conflicts with other packages but got the same result.
– José
Commented Jan 30 at 20:11
• @José Sorry, but I cannot replicate your problem. Did you load tikz? If you had posted a compilable code example in the first place, it would have been easier to come up with a solution that suits your needs. Commented Jan 30 at 20:20
• @José What does your log say? Maybe some packages are missing in the TeX installation on your computer. The above code compiles fine. I double-checked. Commented Jan 30 at 20:36
• Yes, I tried your code in a clean file and I got ./test1.tex:49: Package pgf Error: No shape named n3 is known. which is quite strange as n3 is defined.
– José
Commented Jan 30 at 20:46
• The strange thing is that the code you provided above works on your computer and it doesn't work on mine. I replicated it as it is. Under those circumstances, I do not know what package would be missed.
– José
Commented Jan 30 at 20:54