我需要画一些类似的东西(请原谅,它非常粗糙),以表示我的算法中的依赖关系。基本上,我想说圆 (l,r) 有来自其他 3 个圆 (l,r-1)、(l-1,r-1) 和 (l-1,r) 的传入箭头。我只能使用 Tikz 想出一个网格。如何绘制如下图所示的依赖箭头和节点?
\documentclass[24point]{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\draw[step=0.5cm,color=gray] (-1,-1) grid (1,1);
\foreach \x/\y/\m in {+0.75/+0.75/,-0.75/-0.75/} % etc
\node at (\x,\y) {\m};
\end{tikzpicture}%
\end{document}
答案1
我会像这样画你的图表
\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}[>=stealth]
\foreach \i in {0,1,2,3.5,4.5,5.5,7,8} {
\draw[gray,very thin] (0,\i) -- (8.5,\i);
\draw[gray,very thin] (\i,0) -- (\i,8.5);
}
\draw[<->] (8.5,0) node[below] {$n$} -- (0,0) node[below left] {$(1,1)$} -- (0,8.5) node[left] {$n$};
\draw[->] (2,-.5) -- (3,-.5) node[above,near start] {$r$};
\foreach \i in {2.75,6.25} {
\foreach \j in {.5,1.5,4,5,7.5} {
\node at (\i,\j) {$\cdots$};
\node at (\j,\i) {$\vdots$};
}
\foreach \j in {2.75,6.25}
\node at (\i,\j) {\reflectbox{$\ddots$}};
}
\begin{scope}[every node/.style={
circle,draw,
minimum size=.8cm,
align=center,
font=\footnotesize,
inner sep=0pt
}]
\node at (4,4) (11) {$\scriptstyle l-1$\\[-1ex]$\scriptstyle r-1$};
\node at (5,4) (21) {$\scriptstyle l-1$\\[-1ex]$\scriptstyle r$};
\node at (4,5) (12) {$\scriptstyle l$\\[-1ex]$\scriptstyle r-1$};
\node at (5,5) (22) {$\scriptstyle l$\\[-1ex]$\scriptstyle r$};
\end{scope}
\draw[thick,red,->] (11) -- (22);
\draw[thick,red,->] (12) to[bend left=45] (22);
\draw[thick,red,->] (21) to[bend right=45] (22);
\end{tikzpicture}
\end{document}
答案2
在这里使用循环可能有点过头了,但无论如何,这是对你的代码的修改。单个draw
命令即可完成此操作 ;)
\documentclass[tikz,border=7pt]{standalone}
\begin{document}
\tikz
\draw[nodes={inner sep=1pt},color=gray]
(0,0) grid[shift={(-.5,-.5)}] (4,4)
foreach[count=\i] \x/\y/\m in {2/3/a,2/2/b,3/3/c,}{(\x,\y) node(n\i) {\m}}
(n1) edge[red,-latex] (n3)
(n2) edge[red,-latex] (n3)
;
\end{document}