Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}
share|improve this question
    
@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

1 Answer 1

up vote 6 down vote accepted

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}

Result

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}

Result

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

share|improve this answer
    
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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.