如何在表中包含这样的路径?
这是@Schrodinger's cat 提出的代码,问题是当我将“jeu”改为“game”时结果并不好:
\documentclass[border=3mm,tikz]{standalone}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}
\matrix[matrix of nodes,nodes in empty cells,
nodes={minimum size=2.5em,draw,outer sep=0pt,anchor=center},
row sep=-\pgflinewidth,column sep=-\pgflinewidth,
column 1/.style={nodes={draw=none},align=right,text width={width("game")}},
row 1/.style={nodes={draw=none},align=center},
] (mat) {
& 0 & 15 & 30 & 40 & game \\
0 & & & & & |[fill=purple!30]| \\
15 & & & & & |[fill=purple!30]| \\
30 & & & & & |[fill=purple!30]| \\
40 & & & &|[fill=green!70!black]| & \\
game & |[fill=orange]| &|[fill=orange]| & |[fill=orange]| & & \\
};
\draw[red,very thick] plot[mark=*] coordinates {
(mat-2-2) (mat-2-3) (mat-3-3) (mat-3-4) (mat-4-4) (mat-4-5) (mat-4-6)};
\end{tikzpicture}
\end{document}
答案1
欢迎光临!钾Z矩阵非常简单。
\documentclass[border=3mm,tikz]{standalone}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}
\matrix[matrix of nodes,nodes in empty cells,
nodes={minimum size=2.5em,draw,outer sep=0pt,anchor=center},
row sep=-\pgflinewidth,column sep=-\pgflinewidth,
column 1/.style={nodes={draw=none},align=right,text width={width("jeu")}},
row 1/.style={nodes={draw=none},align=center},
] (mat) {
& 0 & 15 & 30 & 40 & jeu \\
0 & & & & & |[fill=purple!30]| \\
15 & & & & & |[fill=purple!30]| \\
30 & & & & & |[fill=purple!30]| \\
40 & & & &|[fill=green!70!black]| & \\
jeu & |[fill=orange]| &|[fill=orange]| & |[fill=orange]| & & \\
};
\draw[red,very thick] plot[mark=*] coordinates {
(mat-2-2) (mat-2-3) (mat-3-3) (mat-3-4) (mat-4-4) (mat-4-5) (mat-4-6)};
\end{tikzpicture}
\end{document}
附录:不需要自己设置第一列文字宽度的版本,需要编译两次。
\documentclass[border=3mm,tikz]{standalone}
\usetikzlibrary{matrix}
\newbox\matrixcellbox
\tikzset{column align/.style 2 args={column #1/.append style={nodes={execute at begin
node={\setbox\matrixcellbox=\hbox\bgroup},
execute at end
node={\egroup\eqmakebox[\tikzmatrixname\the\pgfmatrixcurrentcolumn][#2]{\copy\matrixcellbox}}}}}}
\usepackage{eqparbox}
\begin{document}
\begin{tikzpicture}
\matrix[matrix of nodes,nodes in empty cells,
nodes={minimum size=3.25em,draw,outer sep=0pt,anchor=center},
row sep=-\pgflinewidth,column sep=-\pgflinewidth,
column 1/.style={nodes={draw=none}},column align={1}{r},
row 1/.style={nodes={draw=none},align=center},
] (mat) {
& 0 & 15 & 30 & 40 & game \\
0 & & & & & |[fill=purple!30]| \\
15 & & & & & |[fill=purple!30]| \\
30 & & & & & |[fill=purple!30]| \\
40 & & & &|[fill=green!70!black]| & \\
game & |[fill=orange]| &|[fill=orange]| & |[fill=orange]| & & \\
};
\draw[red,very thick] plot[mark=*] coordinates {
(mat-2-2) (mat-2-3) (mat-3-3) (mat-3-4) (mat-4-4) (mat-4-5) (mat-4-6)};
\end{tikzpicture}
\end{document}
如果需要,可以进一步玩这个游戏以完全自动调整单元格大小。除了这样做,这里有一个版本,widest=game
例如,您可以使用 指定宽度条目。
\documentclass[border=3mm,tikz]{standalone}
\usetikzlibrary{matrix}
\tikzset{widest/.style={nodes={minimum size={width("#1")+4*2pt+4*\pgflinewidth}},
column 1/.append style={nodes={text width={width("#1")}}}}}
\begin{document}
\begin{tikzpicture}
\matrix[matrix of nodes,nodes in empty cells,
nodes={draw,outer sep=0pt,anchor=center},widest=game,
row sep=-\pgflinewidth,column sep=-\pgflinewidth,
column 1/.append style={nodes={draw=none},align=right},
row 1/.append style={nodes={draw=none},align=center},
] (mat) {
& 0 & 15 & 30 & 40 & game \\
0 & & & & & |[fill=purple!30]| \\
15 & & & & & |[fill=purple!30]| \\
30 & & & & & |[fill=purple!30]| \\
40 & & & &|[fill=green!70!black]| & \\
game & |[fill=orange]| &|[fill=orange]| & |[fill=orange]| & & \\
};
\draw[red,very thick] plot[mark=*] coordinates {
(mat-2-2) (mat-2-3) (mat-3-3) (mat-3-4) (mat-4-4) (mat-4-5) (mat-4-6)};
\end{tikzpicture}
\end{document}
答案2
这是另一种方法!你看到了简单的 TikZ 代码吗?
\documentclass[tikz,border=5mm]{standalone}
\begin{document}
\begin{tikzpicture}
\begin{scope}[shift={(-.5,.5)}]
\fill[purple!30] (4,0) rectangle +(1,-3);
\fill[cyan] (3,-3) rectangle +(1,-1);
\fill[orange] (0,-4) rectangle +(3,-1);
\draw (0,0) grid (5,-5);
\end{scope}
\draw[red,mark=*,line width=1.2pt] plot coordinates
{(0,0) (1,0) (1,-1) (2,-1) (2,-2) (3,-2) (4,-2)};
\foreach \i/\j in {0/0,1/15,2/30,3/40,4/jeu}
\path (\i,1) node{\j} (-1,-\i) node{\j};
\end{tikzpicture}
\end{document}