如何讓 tikz 矩陣看起來更像陣列?

如何讓 tikz 矩陣看起來更像陣列?

考慮以下兩個矩陣:

在此輸入影像描述

左邊的矩陣是用程式碼渲染的

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

左邊的數值由下式給出:

\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}

我希望右邊的那個看起來像左邊的那個。一些明顯的差異是:

  1. 左側矩陣的列右對齊。
  2. 兩個矩陣中的空白分佈不同。

我該怎麼做才能讓右邊的矩陣看起來更像左邊的矩陣?

答案1

你可以用於eqparbox使節點等寬,然後將內容右對齊。其餘的可以透過更改一些鍵來完成。如果您還沒有這樣做,請查看該nicematrix軟體包,它提供了許多不錯且有據可查的選項。

\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}

在此輸入影像描述

相關內容