![在每个 TIKZ 矩阵节点的左上角添加附加文本](https://linux22.com/image/438636/%E5%9C%A8%E6%AF%8F%E4%B8%AA%20TIKZ%20%E7%9F%A9%E9%98%B5%E8%8A%82%E7%82%B9%E7%9A%84%E5%B7%A6%E4%B8%8A%E8%A7%92%E6%B7%BB%E5%8A%A0%E9%99%84%E5%8A%A0%E6%96%87%E6%9C%AC.png)
我想在每个单元格的角落放置一个简单的印刷体字母(并将每个数字留在单元格的中心)以形成迷宫。
\documentclass[11pt]{report}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}
\matrix[matrix of math nodes,nodes in empty cells,
nodes={minimum size=3.5em,outer sep=0pt,anchor=center,draw,loosely dashed},
row sep=-\pgflinewidth,column sep=-\pgflinewidth
] (mat) {
-1& -2,1 & -2,3 & -2,2 & -4,7 & -5\ & -5,04 & -1,3\\
-1,4 & -1,7 & -2,6 & -2 & -4,5 & -4,3 & -5,1 & -5,13 \\
-1 & -4 & -3 & -3,2 & -3 & -4,2 & -4,4 & -49 \\
-2 & -1,5 & -1,1 & -3,7 & -4 & -4,1 & -11 & -100\\
};
\draw[thick] (mat-1-1.north west) rectangle (mat-4-8.south east);
\draw[red,thick,dashed] plot coordinates {
(mat-1-1) (mat-2-1) (mat-2-2) (mat-1-2) (mat-1-3) (mat-2-3) (mat-3-3) (mat-3-4)
(mat-4-4) (mat-4-6) (mat-2-6) (mat-2-5) (mat-1-5) (mat-1-7) (mat-2-7) (mat-2-8)
(mat-4-8) };
\end{tikzpicture}
\end{document}
答案1
一种可能性是重叠两个矩阵,将第二个矩阵(带有字母)相对于第一个矩阵移动。
像这样:
\documentclass[11pt]{report}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}
\matrix[matrix of math nodes,nodes in empty cells,
nodes={minimum size=3.5em,outer sep=0pt,anchor=center,draw,loosely dashed},
row sep=-\pgflinewidth,column sep=-\pgflinewidth
] (mat) {
-1& -2,1 & -2,3 & -2,2 & -4,7 & -5\ & -5,04 & -1,3\\
-1,4 & -1,7 & -2,6 & -2 & -4,5 & -4,3 & -5,1 & -5,13 \\
-1 & -4 & -3 & -3,2 & -3 & -4,2 & -4,4 & -49 \\
-2 & -1,5 & -1,1 & -3,7 & -4 & -4,1 & -11 & -100\\
};
\draw[thick] (mat-1-1.north west) rectangle (mat-4-8.south east);
\draw[red,thick,dashed] plot coordinates {
(mat-1-1) (mat-2-1) (mat-2-2) (mat-1-2) (mat-1-3) (mat-2-3) (mat-3-3) (mat-3-4)
(mat-4-4) (mat-4-6) (mat-2-6) (mat-2-5) (mat-1-5) (mat-1-7) (mat-2-7) (mat-2-8)
(mat-4-8) };
% this is for centering:
\useasboundingbox (mat-1-1.north west) rectangle (mat-4-8.south east);
\matrix[matrix of nodes,blue,
nodes={minimum size=3.5em,outer sep=0pt,anchor=center},
] (mat2) at ([shift={(-0.5,0.4)}]mat) {% <-- change the shift if you need to
H & B & C & D & E & F & G & H\\
A & B & C & D & E & F & G & H\\
A & B & C & D & E & F & G & H\\
A & B & C & D & E & F & G & H\\
};
\end{tikzpicture}
\end{document}
答案2
还有[label={...}]
机制。标签的位置相对于中心(线与边缘相交的地方)而不是相对于角,因此有助于固定父节点的纵横比。
\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[label position=139,
every label/.style={green, below right, inner sep=1pt}]
\node[draw, inner sep=1em, label=A] at (0,0) {1,1};
\end{tikzpicture}
\end{document}