我想在 TikZ 中创建一个对称矩阵,但不想显示上对角线元素。
\documentclass[tikz]{standalone}
\usetikzlibrary{matrix, positioning, calc}
\usetikzlibrary{shapes.geometric, fit}
\begin{document}
\begin{tikzpicture}[baseline=(U.center)]
\matrix [matrix of math nodes, left delimiter=[ ,right delimiter={]}, nodes={outer sep=1pt}] (U) {
0 & \textcolor{white}{9} & \textcolor{white}{3} & \textcolor{white}{6} & \textcolor{white}{11} \\[0.1cm]
9 & 0 & \textcolor{white}{11} & \textcolor{white}{11} & \textcolor{white}{11} \\[0.1cm]
3 & 7 & 0 & \textcolor{white}{11} & \textcolor{white}{11} \\[0.1cm]
6 & 5 & 9 & 0 & \textcolor{white}{11} \\[0.1cm]
11 & 10 & 2 & 8 & 0 \\[0.1cm]
};
\foreach \x in {1,...,5}{
\node[black, left = 3 mm of U-\x-1] (U-\x-6) {$\x$};
};
\foreach \y in {1,...,5}{
\node[black, above= 1 mm of U-1-\y] (U-6-\y) {$\y$};
};
\draw[red] ([shift={(4pt, 2pt)}]U-1-1.north west) -- ([shift={(5pt,2pt)}]U-5-5.south east);
\draw[red] ([shift={(-5pt, -2pt)}]U-1-1.north west) -- ([shift={(-4pt,-2pt)}]U-5-5.south east);
\draw[red] ([shift={(5pt, 2pt)}]U-1-1.north west) -- ([shift={(-5pt,-2pt)}]U-1-1.north west);
\draw[red] ([shift={(5pt, 2pt)}]U-5-5.south east) -- ([shift={(-5pt,-2pt)}]U-5-5.south east);
\node [draw, circle, red] at (U-5-3) {};
\node[black, left = 6 mm of U] {$\mathbf{D}=\left\{ d_{ij}\right\}=$};
\end{tikzpicture}
\end{document}
我想知道如何在不使用元素的白色文本颜色的情况下获取空白的上对角线元素。谢谢
答案1
您可以在方程中使用 TikZ,由于对角线已满,因此上对角线条目内不需要任何节点。分隔符通常太宽,因此向内稍微移动看起来会更好(意见!)
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
\[
\mathbf{D}=\{ d_{ij} \}=
\begin{tikzpicture}[baseline=(U.center),
every left delimiter/.style={xshift=1mm},
every right delimiter/.style={xshift=-1mm}]
\matrix [matrix of math nodes,
inner sep=0pt,
left delimiter={[},
right delimiter={]},
nodes={inner sep=0,minimum size=6mm,anchor=center}
] (U) {
0& & & & \\
9& 0& & & \\
3& 7& 0& & \\
6& 5& 9& 0& \\
11&10& 2& 8& 0\\
};
\foreach \x in {1,...,5}{\\
\node[left] at (U-\x-\x.west -| U.west) {$\x$};
\node[above] at (U-\x-\x.north|- U.north) {$\x$};
}
\draw[red] (U-1-1.north) -- (U-1-1.west) -- (U-5-5.south) -- (U-5-5.east) -- cycle;
\node [draw, circle, red] at (U-5-3) {};
\end{tikzpicture}
\]
\end{document}