6

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

enter image description here

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).

Viesturs
  • 7,895

4 Answers4

5

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:

enter image description here

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:

enter image description here

Ignasi
  • 136,588
5

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}

Result

Heiko Oberdiek
  • 271,626
3

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} 

enter image description here

Bernard
  • 271,350
  • 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 07 '16 at 02: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 07 '16 at 08:37
2

With {bNiceMatrix} of nicematrix, which, as the Tikz library matrix does, create a PGF/Tikz node under each of the matrix (if you need them...).

\documentclass{article}
\usepackage{nicematrix}

\begin{document}

$\begin{bNiceMatrix}[r,left-margin=0.6em] \Block[draw]{2-3}{} 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 \ \end{bNiceMatrix}$

\end{document}

You need several compilations (because nicematrix uses PGF/Tikz nodes under the hood).

Output of the above code

F. Pantigny
  • 40,250