4

Given the following matrix, how can I change the parentheses to enclose only the numbers of the matrix, like the ones that I scribbled in the picture below?

enter image description here

\documentclass[border=5pt]{standalone}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}

\usepackage{tikz}
\usetikzlibrary{matrix}

\begin{document}

\begin{tikzpicture}

\matrix(m) [matrix of math nodes, 
         left delimiter=(,right delimiter={)}, nodes={minimum size=1cm, outer sep=0pt, text height=1.5ex, text depth=.25ex}]
{
\phantom{} & I & II & III & IV \\
A & 15 & 3 & 30 & 20 \\
B & 20 & 2 & 20 & 30\\
C & 40 & 20 & 10 & 40 \\
D & 5 & 30 & 40 & 40 \\
};

\end{tikzpicture}
\end{document}
4

The left delimeter and right delimeter option can be given to a any node that has the standard anchors. Therefore you can enclose any tikz node with delimiters.

For example, you can define a node that fit the submatrix that you want with the fit library:

\documentclass[border=5pt]{standalone}
\usepackage[utf8]{inputenc}
\usepackage{mathtools}

\usepackage{tikz}
\usetikzlibrary{matrix}
\usetikzlibrary{fit}

\begin{document}
\begin{tikzpicture}[
    add paren/.style={
      left delimiter={(},
      right delimiter={)}, 
    }
  ]

  \matrix(m) [matrix of math nodes, 
    add paren,
    nodes={minimum size=1cm, outer sep=0pt, text height=1.5ex, text depth=.25ex}
  ]
  {
      & I  & II & III & IV \\
    A & 15 & 3  & 30  & 20 \\
    B & 20 & 2  & 20  & 30 \\
    C & 40 & 20 & 10  & 40 \\
    D & 5  & 30 & 40  & 40 \\
  };

  \node[fit=(m-3-3) (m-5-5), add paren] (submatrix) {};

\end{tikzpicture}
\end{document}

The result is not entirely satisfactory:

enter image description here

You can fix it with a negative inner sep in sumbmatrix node:

\node[fit=(m-3-3) (m-5-5), add paren, inner xsep=-1ex] (submatrix) {};

enter image description here

or by using different options in the matrix definition:

\documentclass[border=5pt]{standalone}
\usepackage[utf8]{inputenc}
\usepackage{mathtools}

\usepackage{tikz}
\usetikzlibrary{matrix}
\usetikzlibrary{fit}

\begin{document}
\begin{tikzpicture}[
    add paren/.style={
      left delimiter={(},
      right delimiter={)}, 
    }
  ]

  \matrix(m) [matrix of math nodes, 
    add paren,
    column sep={1cm,between origins},
    row sep={1cm,between origins},
  ]
  {
      & I  & II & III & IV \\
    A & 15 & 3  & 30  & 20 \\
    B & 20 & 2  & 20  & 30 \\
    C & 40 & 20 & 10  & 40 \\
    D & 5  & 30 & 40  & 40 \\
  };

  \node[fit=(m-3-3) (m-5-5), add paren, inner sep=0pt] (submatrix) {};

\end{tikzpicture}
\end{document}

enter image description here

1
  • Thanks a lot! It's a great solution and easy to implement. – John W. de Burgh May 9 at 19:04
6

Since you already used TikZ to implement your matrix, here's a solution using nicematrix package (which loads and uses TikZ):

matrix inside matrix

\documentclass[border=3.14mm]{standalone}
\usepackage[utf8]{inputenc}
\usepackage{amsmath,nicematrix}

\usepackage{tikz}
\usetikzlibrary{matrix}

\begin{document}

$\begin{pNiceMatrix}[columns-width = 20pt,margin=5pt,cell-space-limits = 10pt]
    \phantom{} & I & II & III & IV \\
    A & 15 & 3 & 30 & 20 \\
    B & 20 & 2 & 20 & 30\\
    C & 40 & 20 & 10 & 40 \\
    D & 5 & 30 & 40 & 40
    \CodeAfter\SubMatrix({2-2}{5-5})
\end{pNiceMatrix}$

\end{document}

Note that you may have to compile at least twice to see the final result (one for defining nodes, and another one to draw accordingly).

3
  • Great for general use of Latex, however in my case I wanted tikz to be able to draw some figures. – John W. de Burgh May 9 at 19:07
  • No problem, the other solution is of great use and with some nice exlplanations. But note that nicematrix is TikZ based and so allows you to draw anything, since all figures are nested into nodes named (1-1) to (n-n). – SebGlav May 9 at 19:33
  • Thanks for the clarification! – John W. de Burgh May 10 at 8:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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