[Tex/LaTex] How to draw arrows between circled coefficients of polynomials using tikz

arrowstikz-pgftikzmark

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}[1]{\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}

Best Answer

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}[2]{%
    \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:

enter image description here

My original solution circled the exponents:

enter image description here

The circles are more cramped than I would like. This used a small variation on the above:

\newcommand{\tikzmark}[2]{%
    \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.