How to draw arrows between circled coefficients of polynomials using tikz

After reading Gonzalo's answer to Arrow between parts of equation in LaTeX, I thought I would be able to modify it so that I could have the corresponding coefficients circled and an arrow going from one circle to the next. For example, I would like the coefficient of x^2 on the left, a, circled and the coefficient of x^2 on the right, 5 circled, then a curved arrow drawn between the two circles. How might I achieve this? Also, how would I add some text along the path of the arrow? I reproduce his code here.

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

\newcommand{\tikzmark}{\tikz[overlay,remember picture] \node (#1) {};}

\begin{document}

\begin{equation}
a\tikzmark{a}x^2 + bx + c = 5\tikzmark{b}x^2 + bx + c.
\begin{tikzpicture}[overlay,remember picture,out=315,in=225,distance=0.4cm]
\draw[->,red,shorten >=3pt,shorten <=3pt] (a.center) to (b.center);
\end{tikzpicture}
\end{equation}

\end{document}


Edit Sorry, I misread the question and thought that you wanted to circle the exponents. Modifying my first solution you can circle the coefficients with:

\documentclass{article}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{decorations.text}

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

\begin{document}

\begin{equation}
\tikzmark{a}{a}x^2 + bx + c = \tikzmark{b}{5}x^{2} + bx + c.
\begin{tikzpicture}[overlay,remember picture,
decoration={text along path,text color=red, text align=center,
raise=2pt, text={|\scriptsize|match coefficients}}]
\draw[->,red] (a) to [out=35,in=145](b);
\draw[decorate] (a) to [out=35,in=145](b);
\end{tikzpicture}
\end{equation}

\end{document}


The use of baseline is presumably necessary because the circles contribute to the baseline, but this is a bit of a fudge.

The tikz library decorations.txt is needed to get the text to follow the curve. This gives: My original solution circled the exponents: The circles are more cramped than I would like. This used a small variation on the above:

\newcommand{\tikzmark}{%
\tikz[overlay,remember picture] \node[circle,red,draw,text=black,
inner sep=0pt] (#1) {\scriptsize$#2$};}

\begin{equation}
ax^{\tikzmark{a}{2}} + bx + c = 5x^{\tikzmark{b}{2}} + bx + c.
\begin{tikzpicture}[overlay,remember picture,
decoration={text along path,text color=red, text align=center,
raise=2pt, text={|\scriptsize|match exponents}}]
\draw[->,red] (a) to [out=25,in=155](b);
\draw[decorate] (a) to [out=25,in=155](b);
\end{tikzpicture}
\end{equation}


Notice that I had to change the angles that the red line leaves and enters the circles in the two examples because if I had used the angles 25 and 155 for the coefficients then the red line goes through the exponents.

You have to remove the option overlay from \tikzmark definition first. And second, alter the definition to take the same argument as label and content. Also, if you want circles to have same radius, fix some value for minimum width key. With these it becomes,

\newcommand{\mathtikzmark}{\tikz[baseline={(#1.base)},remember picture] \node[draw,circle,inner sep=0.5pt,minimum width=3ex] (#1) {$#1$};}


I have named it \mathtikzmark since the contents will be in math mode (note $#1$ inside the content).

Full code:

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

\newcommand{\mathtikzmark}{\tikz[baseline={(#1.base)},remember picture] \node[draw,circle,inner sep=0.5pt,minimum width=3ex] (#1) {$#1$};}

\begin{document}

\begin{equation}
\mathtikzmark{a} x^2 + bx + c = \mathtikzmark{99} x^2 + bx + c.
\begin{tikzpicture}[overlay,remember picture,distance=0.4cm]
\draw[->,red,in=225,out=315] (a) to (99);
\end{tikzpicture}
\end{equation}

\end{document} If you want correct math spacing and don't mind if the circles overlap the adjacent parameter, here is one way:

\documentclass{article}
\usepackage{amsmath}
\usepackage{tikz}
\newcommand{\mathtikzmark}{\tikz[remember picture] \node[inner sep=0pt,outer sep=0pt] (#1) {$#1$};}

\begin{document}

\begin{equation}
\mathtikzmark{a}x^2 + bx + c = \mathtikzmark{99}x^2 + bx + c.
\begin{tikzpicture}[overlay,remember picture]
\node[draw,circle,minimum width=1.8ex]  (n1) at (a.center) {};
\node[draw,circle,minimum width=3ex]  (n2) at (99.center) {};
\draw[->,red,in=225,out=315] (n1) to (n2);
\end{tikzpicture}
\end{equation}

\end{document} Adjust the minimum width for nodes n1 and n1 as you like.