# boxing a matrix column with tikzpicture, how to ensure sides don't bend around contents?

When using tikzpicture to draw a rectangle around subsets of matrix elements, how can I be sure of getting a rectangle, rather than a shape that has wonky sides because it takes account of the varying widths of the elements ?

For example:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix,positioning}
\begin{document}
\begin{tikzpicture}
\matrix [matrix of math nodes] (m)
{1 & 1 & 1 \\
1 & 1 & -2 \\
1 & 1 & 1 \\};
\draw[rounded corners,ultra thick][color=blue]
(m-2-2.north west) -- (m-1-2.north west)
-- (m-1-3.north east) -- (m-2-3.south east) -- (m-2-2.south west) -- (m-2-2.north west);
\end{tikzpicture}
\end{document}


...gives a rectangle with a wonky right hand edge. This can be fixed by adding a white minus sign before the middle 1 in the top row (\color{white}-\color{black}1), but this unattractive kludge messes up the position of the 1!

Thanks for any help with this.

# UPDATE

Many thanks to Heiko for the answer which solved the given example. But there remains a problem when a mid-row has long contents, e.g.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix,positioning,fit}
\begin{document}
\begin{tikzpicture}
\matrix [matrix of math nodes] (m)
{1 & 1 & 1 \\
1 & 1.618034 & -2 \\
1 & 1 & 1 \\};
\node[draw=blue,inner sep=0pt,ultra thick,rounded corners,
fit=(m-1-2.north west) (m-1-3.north east)
(m-3-2.south west) (m-3-3.south east)
]{};
\end{tikzpicture}
\end{document}

-
@Jake - Many thanks! That fixes it. (I've also learnt a lesson about making examples here too simple; the matrix I've been having problems with is 9x9. But all problems solved now, thanks to Heiko and yourself!) –  Harry Macpherson Sep 11 '12 at 12:48

The library fit helps:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix,positioning,fit}
\begin{document}
\begin{tikzpicture}
\matrix [matrix of math nodes] (m)
{1 & 1 & 1 \\
1 & 1 & -2 \\
1 & 1 & 1 \\};
\node[draw=blue,inner sep=0pt,ultra thick,rounded corners,
fit=(m-1-2.north west) (m-1-3.north east)
(m-2-2.south west) (m-2-3.south east)
]{};
\end{tikzpicture}
\end{document}


## Update

• As jake noted, the specification north west and similar are not needed, because all node anchors are used.

• If the node area is larger, then use all nodes or all outer nodes. Then it does not matter, what is inside the nodes.

Example file for the update:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix,positioning,fit}
\begin{document}
\begin{tikzpicture}
\matrix [matrix of math nodes] (m)
{1 & 1 & 1 \\
1 & 1.618034 & -2 \\
1 & 1 & 1 \\};
\node[draw=blue,inner sep=0pt,ultra thick,rounded corners,
fit=(m-1-2) (m-1-3)
(m-2-2) (m-2-3)
(m-3-2) (m-3-3)
]{};
\end{tikzpicture}
\end{document}


Further reading: 16.3 Fitting Nodes to a Set of Coordinates of the TikZ and PGF manual.

-
Many thanks. That does the job perfectly! –  Harry Macpherson Sep 11 '12 at 11:28
oops! My example was too simple! There's still a problem when a middle row contains long contents, as described in updated question above. –  Harry Macpherson Sep 11 '12 at 12:38
@jake Thanks. The updated anwer also addresses the middle row problem. –  Heiko Oberdiek Sep 11 '12 at 13:13