1

Consider the following two matrices:

enter image description here

The matrix on the left is rendered with the code

\left[
  \begin{array}{rrrr}
    1  & -32  & 0  & 15 \\
    16 & -138 & -3 & 5  \\
    4  & 14   & 11 & 19
  \end{array}
\right]

The one on the left is given by:

\begin{tikzpicture}
  \matrix[
  , matrix of math nodes
  , left delimiter = {[}
  , right delimiter = {]}
  ] (m)
  {
    1  & -32  & 0  & 15 \\
    16 & -138 & -3 & 5  \\
    4  & 14   & 11 & 19 \\
  };
\end{tikzpicture}

I want the one on the right to look like the one on the left. A few obvious differences are:

  1. The columns of the matrix on the left are right-aligned.
  2. The whitespace is distributed differently in the two matrices.

What can I do to make the matrix on the right look more like the matrix on the left?

1 Answers1

3

You can use eqparbox to make the nodes equally wide, and align the contents right, say. The rest can be done by changing some keys. If you have not already done so, have a look at the nicematrix package which offers a number of nice and well documented options.

\documentclass[11pt]{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\usepackage{eqparbox}
\newbox\eqnodebox
\tikzset{r/.style={execute at begin
    node={\setbox\eqnodebox=\hbox\bgroup$},
    execute at end node={$\egroup\eqmakebox[#1-\tikzmatrixname-\the\pgfmatrixcurrentcolumn][r]{\copy\eqnodebox}}},
    r/.default=R}
\begin{document}
\begin{tikzpicture}
  \matrix[matrix of nodes,cells={nodes={r,inner sep=2pt}},
  inner xsep=0pt,inner ysep=1pt,%<- controls the distance and height of the delimiters
  column sep=1.5pt,
  left delimiter = {[},right delimiter = {]}] (m)
  {
    1  & -32  & 0  & 15 \\
    16 & -138 & -3 & 5  \\
    4  & 14   & 11 & 19 \\
  };
\end{tikzpicture}
\end{document}

enter image description here