答案1
您可以尝试matrix
\documentclass[tikz, border=2mm]{standalone}
\usetikzlibrary{matrix, positioning, arrows.meta}
\begin{document}
\begin{tikzpicture}[line/.style={-Stealth, shorten >=2pt, shorten <=2pt}]
\matrix[matrix of math nodes, nodes={minimum size=8mm, anchor=center}, nodes in empty cells](A){
& A & B & C & D \\
A & & & & \\
B & & & & \\
C & & & & \\
D & & & & \\};
\foreach \i/\j in {2-2/2-4, 2-5/2-4, 3-2/3-3, 3-5/3-3, 4-2/4-3, 4-5/4-3, 5-2/5-5}
\draw[line] (A-\i.center)--(A-\j.center);
\foreach \i/\j in {5-2/2-2, 2-3/4-3, 5-3/4-3, 2-4/3-4, 5-4/3-4, 2-5/4-5, 5-5/4-5}
\draw[line] (A-\i.center)--(A-\j.center);
\draw (A-1-1.north east)--(A-5-1.south east) (A-1-1.south west)--(A-1-5.south east);
\path (A-1-3)--(A-1-4) node[midway, above=3mm]{Colin};
\path (A-3-1)--(A-4-1) node[midway, rotate=90, left=3mm, anchor=south]{Rose};
\end{tikzpicture}
\end{document}
答案2
举个例子,硬编码所有内容可能更容易,但由于每个向上箭头都会遇到相应的向下箭头,每个向左箭头都会遇到向右箭头,因此循环遍历垂直和水平箭头的高度和长度似乎很自然。这样做的主要优点是可以创建用于绘制这些图表的宏。完成此操作后,您就可以生成图表了
使用以下行:
\MovementDiagram{3,1,2,1}{3,1,1,2}
\MovementDiagram{2,1,3,2}{1,2,3,2}
您也可以将名称Colin
和Rose
参数设为\MovementDiagram
宏的参数。另一种可能性(需要多做一点工作)是允许根据垂直和水平列表的长度自动设置网格大小。以下是完整代码:
\documentclass{article}
\usepackage{alphalph}
\usepackage{tikz}
\usetikzlibrary{arrows.meta}
\newcommand\MovementDiagram[2]{%
\begin{tikzpicture}[thick, move/.style={arrows={-Latex[width=4pt,length=8pt]}}]
% loop over the vertical arrows using their heights
\foreach \y [count=\x] in {#1} {
\draw[move] (\x,1.05)--+(0,\y-0.1);
\ifnum \y<3
\draw[move] (\x,3.95)-- +(0,\y-2.9);
\fi
\node at (\x,4.6) {\AlphAlph\x};
}
% now the horizontal arrows using their lengths
\foreach \x [count=\y, evaluate=\y as \Y using {int(5-\y)}] in {#2} {
\draw[move] (1.05,\y)--+(\x-0.1,0);
\ifnum \x<3
\draw[move] (3.95,\y)-- +(\x-2.9,0);
\fi
\node at (0.2,\y) {\AlphAlph\Y};
}
% finally draw the "axes" and Rose and Colin
\draw(0.5,0.8)--+(0,4.2);
\draw(-0.2,4.4)--+(4.5,0);
\node at (-0.6,2.5){Rose};
\node at (2.5,5.2){Colin};
\end{tikzpicture}%
}
\begin{document}
\MovementDiagram{3,1,2,1}{3,1,1,2}
\MovementDiagram{2,1,3,2}{1,2,3,2}
\end{document}