# Draw a box around matrix elements

I studied the examples in Highlight elements in the matrix and developed my example:

The code is the following:

 \documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}

\begin{tikzpicture}
\matrix (m)[matrix of math nodes,nodes in empty cells, ]
{
0    & -26  & 111 & 5  & -1     & 2    \\
2666 & 67   & 55  & 77 & 6      & -1   \\
-1   & 3    & 3   & 3  & 3      & 8    \\
1    & 2    & 5   & 77 & 7      & 1    \\
1    & 33   & 44  & 66 & 998888 & -266 \\
-2   & 1    & 7   & -1 & 2      & 80   \\
} ;
\draw (m-1-1.north west) -- (m-2-1.south west) -- (m-2-2.south east) -- (m-1-2.north east) -- cycle;

\end{tikzpicture}

\end{document}


What I do not like is that the box is skew.

Another feature that I do not like is that the column widths are not equal - the width of the column containing 998888 is larger.

Is it possible to control these aspects?

I would like to stick to tikz because it has an attractive feature - being able to draw a dotted line across multiple columns/rows (not addressed in the above example).

The rectangle of the example can be easily drawn by specifying opposite corners:

\draw (m-2-1.south west) rectangle (m-1-2.north east);


The general case is covered by Ignasi's answer.

The nodes can be set with a minimum width of the largest entry:

minimum width=width("998888")


Full example:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}
\matrix (m)[
matrix of math nodes,
nodes in empty cells,
minimum width=width("998888"),
] {
0    & -26  & 111 & 5  & -1     & 2    \\
2666 & 67   & 55  & 77 & 6      & -1   \\
-1   & 3    & 3   & 3  & 3      & 8    \\
1    & 2    & 5   & 77 & 7      & 1    \\
1    & 33   & 44  & 66 & 998888 & -266 \\
-2   & 1    & 7   & -1 & 2      & 80   \\
} ;

\draw (m-2-1.south west) rectangle (m-1-2.north east);

\end{tikzpicture}
\end{document}


It's important to understand that a TikZ-matrix only aligns inner nodes but it doesn't change nodes' size. It's esay to understand if you draw all nodes:

As you can see every node has its own size, being them into a matrix only distribute them in rows and columns. So the command

\draw (m-1-1.north west) -- (m-2-1.south west) -- (m-2-2.south east)
-- (m-1-2.north east) -- cycle;


will produce a skewed rectangle.

If you don't want to modify your code the easiest solution could be:

\draw (m-1-1.north-|m-1-2.west) rectangle (m-2-1.east|-m-2-2.south);


You can also force all nodes to have same width with an option like text width={width(998888)} which can be accompained by align=right and all nodes will be verticaly aligned to the right in every column.

nodes={text width={width(998888)}, align=right}


With this option you could use draw (m-1-1.north west) rectangle (m-2-2.south east) to define the desired region.

The coplete code is:

 \documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}

\begin{tikzpicture}
\matrix (m)[matrix of math nodes, nodes in empty cells, nodes={text width={width(998888)}, align=right} ]
{
0    & -26  & 111 & 5  & -1     & 2    \\
2666 & 67   & 55  & 77 & 6      & -1   \\
-1   & 3    & 3   & 3  & 3      & 8    \\
1    & 2    & 5   & 77 & 7      & 1    \\
1    & 33   & 44  & 66 & 998888 & -266 \\
-2   & 1    & 7   & -1 & 2      & 80   \\
} ;
\draw (m-1-1.north-|m-2-1.west) rectangle (m-2-2.east|-m-2-2.south);

\end{tikzpicture}

\end{document}


And the result:

It's rather easy with pstricks: use an ordinary bmatrix enclosed in a postscript environment, put two empty nodes A and B at the relevant places, and join them with an \ncbox command with the convenient size parameters. A second solution (more of an automatic solution) consists in drawing a \psframe which joins two diagonally opposed nodes:

\documentclass[12pt, svgnames]{article}
\usepackage{mathtools, nccmath}
\usepackage{pst-node}
\usepackage{auto-pst-pdf}

\begin{document}
\begin{equation*}
\begin{postscript}
\begin{bmatrix}
0 & -26\pnode{C} & 111 & 5 & -1 & 2 \\
\:\pnode[-0.05,0]{A} 2666 & 67\pnode[0.25, 0]{B} & 55 & 77 & 6 & -1 \\
-1 & 3 & 3 & 3 & 3 & 8 \\
1 & 2 & 5 & 77 & 7 & 1 \\
1 & 33 & 44 & 66 & \medmath{998888} & -266 \\
-2 & 1 & 7 & -1 & 2 & 80
\end{bmatrix}
\ncbox[boxheight=0.85, boxdepth=0.1, linecolor=IndianRed]{A}{B}
\end{postscript}
\end{equation*}
\smallskip

\begin{equation*}
\psset{linearc =0.2,linejoin=1}
\begin{postscript}
\begin{bmatrix}
0 &-26\: \pnode[0,0.35]{C} & 111 & 5 & -1 & 2 \\
\,\pnode[0,-0.1]{A}\:2666 & 67 & 55 & 77 & 6 & -1 \\
-1 & 3 & 3 & 3 & 3 & 8 \\
1 & 2 & 5 & 77 & 7 & 1 \\
1 & 33 & 44 & 66 & \medmath{998888} & -266 \\
-2 & 1 & 7 & -1 & 2 & 80
\end{bmatrix}
\psframe[linecolor=IndianRed](A)(C)
\end{postscript}
\end{equation*}

\end{document}


• Is there a solution that provides an automatic height of the box? Doing that manually seems to me a little bit archaic with Latex. – pzorba75 Oct 7 '16 at 2:19
• @pzorba75: Yes, more or less. As you can see, I used only two nodes (that's what \ncbox is done for), whereas we have to enclose 4 values in a frame. We also can use two nodes, in positions (2,1) and (1,2). What can't be fully automated is the vertical offset of these nodes. Please see my updated answer. – Bernard Oct 7 '16 at 8:37