我有这个代码:
\documentclass[tikz, border=3mm]{standalone}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}
\matrix[matrix of nodes,
inner sep=0pt,
anchor=south west,
row sep=-\pgflinewidth,
column sep=-\pgflinewidth,
nodes={draw=black!30,
anchor=center,
align=center,
minimum size=1cm,
outer sep=0pt,
}
]{
-t & 1 & 1 & 1 & 0 \\
0 & 1-t & 0 & 0 & 0 \\
0 & 1 & 1-t & 0 & 0 \\
0 & -1 & 0 & 1-t & 1 \\
0 & 0 & -1 & -1 & -t-1 \\
};
\end{tikzpicture}
\end{document}
有一种方法可以:
答案1
对于数学节点,使用matrix of math nodes
而不是matrix of nodes
,但看起来您需要删除align=center
它才能工作。
如果您为矩阵命名,例如m
下面的代码,您可以使用 访问各个节点m-<rownum>-<colnum>
,因此您可以使用它来绘制线条并添加标签。几个循环可以让您自动完成此操作。
\documentclass[tikz, border=3mm]{standalone}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}
\matrix (m) [matrix of math nodes,
inner sep=0pt,
anchor=south west,
row sep=-\pgflinewidth,
column sep=-\pgflinewidth,
nodes={draw=black!30,
anchor=center,
minimum size=1.1cm,
outer sep=0pt,
}
]{
-t & 1 & 1 & 1 & 0 \\
0 & 1-t & 0 & 0 & 0 \\
0 & 1 & 1-t & 0 & 0 \\
0 & -1 & 0 & 1-t & 1 \\
0 & 0 & -1 & -1 & -t-1 \\
};
% for a single row/column
%\node [draw,circle,minimum size=9mm] (a) at (m-1-1.center) {};
%\draw [thick] ([xshift=-1mm]m-1-5.east) -- (a) -- ([yshift=1mm]m-5-1.south);
% or use a loop to do all three at once
\foreach \i in {1,...,3}
{
\node [draw,circle,minimum size=9mm] (a) at (m-\i-\i.center) {};
\draw [thick] ([xshift=-1mm]m-\i-5.east) -- (a) -- ([yshift=1mm]m-5-\i.south);
}
\foreach \i in {1,...,5}
{
\node [above] at (m-1-\i.north) {\i};
\node [left] at (m-\i-1.west) {\i};
}
\end{tikzpicture}
\end{document}