# How can I add an underbrace in this TikZ picture matrix of maths nodes?

I have the current tikz picture and would like to add a underbrace between the 2nd and 3rd row at the location unidentified by the 2 arrows pointing down.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{decorations.pathreplacing, matrix, arrows}
\begin{document}
\begin{tikzpicture}[>=angle 90]
\matrix(a)[matrix of math nodes,
column sep=-5pt,
%       row sep=-5pt,
text height=1ex]
{ y = & x^2 & -5x & +6 & \qquad & & & \frac{1}{2}(-5)=\frac{-5}{2} \\
y = & x^2 &  -5x &+\frac{25}{4} & - \frac{25}{4}&+6 & \qquad & & \left(\frac{-5}{2}\right)^2=\frac{25}{4} \\
&  &  & \hspace{1pt} & \\
y=& (x &-&\frac{5}{2}&)^2 -\frac{1}{4} &\\};
\path[->](a-2-9) edge [bend left=20] (a-2-4);
\path[->](a-2-9) edge [bend left=20] (a-2-5);
\path[->](a-1-8) edge [bend left=50] (a-4-4);
\path[->](a-2-2) edge [out=270,in=90] (a-4-3);
\path[->](a-2-3) edge [out=270,in=90] (a-4-3);
\end{tikzpicture}
\end{document}


In the following figure, I want to change the arrows next to the red into an underbrace:

• Where exactly? "between the 2nd and 3rd row at the location unidentified by the 2 arrows pointing down" confuses me so much. How long should this underbrace be? Can you add an image of the desired output? – Gonzalo Medina Aug 26 '15 at 22:46
• In the figure here: i.stack.imgur.com/R0Nuv.png I want to change the arrows next to the red into an underbrace. Hope this better explains what i am trying to say. – Christopher Zerafa Aug 26 '15 at 22:50
• Better, thanks, although I still am not sure. Do my answers below do what you want? – Gonzalo Medina Aug 26 '15 at 23:01

Nothing special here, since you have access to the anchors for the nodes in the matrix, so you simply need

\draw[red,decorate,decoration={brace,mirror,raise=-2pt}]
([xshift=3pt]a-2-2.south west) -- ([xshift=-5pt]a-2-3.south east);


or

\draw[red,decorate,decoration={brace,mirror,raise=-2pt}]
([xshift=3pt]a-2-2.south west) -- ([xshift=-4pt]{a-2-3.south east|-a-2-2.south west});


to guarantee that the brace is completely horizontal.

The complete code (I did some other optional changes):

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{decorations.pathreplacing, matrix, arrows}

\begin{document}

\begin{tikzpicture}[>=angle 90]
\matrix (a) [
matrix of math nodes,
column sep=-5pt,
%row sep=-5pt,
text height=1ex
]
{
y = & x^2 & -5x & +6 & \qquad & & & \frac{1}{2}(-5)=\frac{-5}{2} \\
y = & x^2 &  -5x &+\frac{25}{4} & - \frac{25}{4}&+6 & \qquad
& & \left(\frac{-5}{2}\right)^2=\frac{25}{4} \\
& & & & \\[8pt]
y=& (x &-&\frac{5}{2})^2 & -\frac{1}{4} &\\
};

\path[->]
(a-2-9) edge [bend left=20] (a-2-4)
(a-2-9) edge [bend left=20] (a-2-5)
(a-1-8) edge [bend left=50] (a-4-4)
(a-2-2) edge [out=270,in=90] (a-4-3)
(a-2-3) edge [out=270,in=90] (a-4-3);
\draw[red,decorate,decoration={brace,mirror,raise=-2pt}]
([xshift=3pt]a-2-2.south west) -- ([xshift=-4pt]{a-2-3.south east|-a-2-2.south west});
\end{tikzpicture}

\end{document}


Notice, however, that the spacing in the above solution is wrong and inconsistent (looking closely around the operators you'll see this). I propose you an alternative approach. Use align* (or alignat*) from amsmath to write the equation and then TikZ with the help of the tikzmark library to place some marks later used to draw the brace and the arrows:

\documentclass{article}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{decorations.pathreplacing, matrix, arrows,tikzmark}

\begin{document}

\begin{align*}
y &= x^2 -5x +6 & -5\Bigl(\tikzmark{startc}\frac{1}{2}\Bigr) &= \frac{-5}{2} \\[10pt]
y &= \tikzmark{starta}x^2 - 5x\tikzmark{enda} + \frac{25}{\tikzmark{endbi}4}
- \frac{25}{\tikzmark{endbii}4} +6 & \Bigl(\frac{-5}{2}\Bigr)^2
&= \tikzmark{startb}\frac{25}{4} \\[10pt]
y &= \Bigl(x  - \frac{5}{\tikzmark{endc}2} \Bigr)^2 -\frac{1}{4}
\end{align*}

\begin{tikzpicture}[remember picture,overlay]
\draw[red,decorate,decoration={brace,mirror,raise=2pt}]
(pic cs:starta) -- (pic cs:enda);
\path[->]
(pic cs:startb) edge [bend left=20] ([shift={(5pt,-3pt)}]pic cs:endbi)
(pic cs:startb) edge [bend left=20] ([shift={(5pt,-3pt)}]pic cs:endbii)
([shift={(5pt,-8pt)}]pic cs:startc) edge [bend left=20] ([shift={(5pt,-3pt)}]pic cs:endc);
\end{tikzpicture}

\end{document}


The result: