我想在每个单元格的角落放置一个简单的印刷体字母(并将每个数字留在单元格的中心)以形成迷宫。
\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}