我有以下代码来生成一个有效的矩阵:
\begin{center}
\begin{tikzpicture}[cell/.style={rectangle,draw=black}, nodes in empty cells]
\matrix[
matrix of nodes,
row sep =-\pgflinewidth,
column sep = -\pgflinewidth,
nodes={anchor=center, text height=1.3ex,text depth=0, cell},
column 1/.style = {nodes={minimum width=1cm, fill=mlightgray}},
column 2/.style = {nodes={minimum width=2cm}},
column 3/.style = {nodes={minimum width=2cm}},
column 4/.style = {nodes={minimum width=2cm}},
row 1/.style={nodes={fill=mlightgray}}
]
{ & $3$ & $4$ & $5$ \\
$3$ & & & $100$ \\
$4$ & $-2$ & $-2.6$ & $97.4$ \\
};
\end{tikzpicture}
\end{center}
正如您所注意到的,所有节点的值都被$
(数学模式)包围,是否有人知道如何在矩阵周围放置全局数学模式,以避免$
每个节点都绑定?
答案1
您可以使用matrix of math nodes
而不是matrix of nodes
然后节点内容将$
自动被 包围。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\colorlet{mlightgray}{gray!20}
\begin{document}
\begin{center}
\begin{tikzpicture}[cell/.style={rectangle,draw=black}, nodes in empty cells]
\matrix[
matrix of math nodes,
row sep =-\pgflinewidth,
column sep = -\pgflinewidth,
nodes={anchor=center,minimum width=2cm, text height=1.3ex,text depth=0, cell},
column 1/.style = {nodes={minimum width=1cm, fill=mlightgray}},
%column 2/.style = {nodes={minimum width=2cm}},
% column 3/.style = {nodes={minimum width=2cm}},
% column 4/.style = {nodes={minimum width=2cm}},
row 1/.style={nodes={fill=mlightgray}}
]
{ & 3 & 4 & 5 \\
3 & & & 100 \\
4 & -2 & -2.6 & 97.4 \\
};
\end{tikzpicture}
\end{center}
\end{document}