我正在努力寻找改变我试图构建的 tikzpicture/matrix 中特定节点之间的间距的选项。MWE(在底部)生成以下内容。这很接近 - 但我想做的是减少节点 3 和 4 之间的间距(其中节点 4 为“空”,用 \cdots 填充),然后是 4 和 5 之间的间距(例如,大约是现在的一半)。我已经看过 tikzpositioning,但如果这是正确的方向,还没有弄清楚如何得到我想要的东西。提前感谢你的建议。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{decorations,shapes,arrows,matrix,positioning}
\usepackage{graphicx}
\usepackage{float}
\begin{document}
\begin{figure}[h]
\centering
\rule[-1.85cm]{0pt}{3.5cm}
\begin{tikzpicture}[>=stealth,->,shorten >=1.5pt,transform canvas={scale=1.05},line width=0.75pt]
\matrix (a) [matrix of nodes,row sep=0.5em, column sep=4em, every node/.style={circle,draw,font=\small,minimum width=1cm},
1.node/.style={font=\bf\small\sffamily,fill=blue!20},
2.node/.style={font=\bf\small\sffamily,fill=green!10},
3.node/.style={font=\bf\small\sffamily,fill=green!20},
m.node/.style={font=\bf\small\sffamily,fill=green!30},
empty.node/.style={draw=none}]
{|[1.node]|1 & |[2.node]|2 & |[3.node]|3 & |[empty.node]|$\cdots$ & |[m.node]|m \\};
% left to right, right to left
\path[>=stealth,font=\small, transform canvas={shift={(-0.4pt,-2.25pt)}}] (a-1-1) edge (a-1-2);
\path[>=stealth,font=\small, transform canvas={shift={(-0.4pt,-2.25pt)}}] (a-1-2) edge (a-1-3);
\path[>=stealth,font=\small, transform canvas={shift={(-0.4pt,-2.25pt)}}] (a-1-3) edge (a-1-4);
\path[>=stealth,font=\small, transform canvas={shift={(-0.4pt,-2.25pt)}}] (a-1-4) edge (a-1-5);
\path[>=stealth,font=\small, transform canvas={shift={(0.4pt, 2.25pt)}}] (a-1-5) edge (a-1-4);
\path[>=stealth,font=\small, transform canvas={shift={(0.4pt, 2.25pt)}}] (a-1-4) edge (a-1-3);
\path[>=stealth,font=\small, transform canvas={shift={(0.4pt, 2.25pt)}}] (a-1-3) edge (a-1-2);
\path[>=stealth,font=\small, transform canvas={shift={(0.4pt, 2.25pt)}}] (a-1-2) edge (a-1-1);
% returning arcs
\path[>=stealth,->,font=\small] (a-1-2) edge [bend right=50] (a-1-1) ;
\path[>=stealth,->,font=\small] (a-1-3) edge [bend right=60] (a-1-1) ;
% self-loops
\path[>=stealth,->,font=\small] (a-1-1) edge [loop, below, out=240, in=305, looseness=10, distance=0.9cm] (a-1-1) ;
% smaller nested-loop
\path[>=stealth,->,font=\small] (a-1-1) edge [dotted, loop, below, out=250, in=290, looseness=10, distance=0.45cm] (a-1-1) ;
\path[>=stealth,->,font=\small] (a-1-2) edge [loop, below, out=240, in=300, looseness=10, distance=0.9cm] (a-1-2) ;
\path[>=stealth,->,font=\small] (a-1-3) edge [loop, below, out=240, in=300, looseness=10, distance=0.9cm] (a-1-3) ;
\path[>=stealth,->,font=\small] (a-1-5) edge [loop, below, out=240, in=300, looseness=10, distance=0.9cm] (a-1-5) ;
\end{tikzpicture}
\end{figure}
\end{document}
答案1
\documentclass{article}
\usepackage{tikz-cd}
\begin{document}
\begin{tikzcd}[column sep=1.5cm, arrows={-{Stealth[length=3pt]}, shorten >=1pt}, cells={nodes={draw, circle, minimum size=8mm, fill=green!10}}]
|[fill=blue!20]|1\arrow[r, yshift=-2pt]\arrow[loop below, out=240, in=300, looseness=4]\arrow[dotted, loop below, out=250, in=290, looseness=3.5] &
2\arrow[l, yshift=2pt]\arrow[r, yshift=-2pt]\arrow[l, bend right=45]\arrow[loop below, out=240, in=300, looseness=4] &
3\arrow[l, yshift=2pt]\arrow[r, yshift=-2pt]\arrow[ll, bend right=60]\arrow[loop below, out=240, in=300, looseness=4] &[-5mm]
|[draw=none, fill=none]|\cdots\arrow[l, yshift=2pt]\arrow[r, yshift=-2pt] &[-5mm]
m\arrow[l, yshift=2pt]\arrow[loop below, out=240, in=300, looseness=4]
\end{tikzcd}
\end{document}
答案2
同样使用tikz-cd
包但使用'tikzset˙来定义循环的一些列样式:
\documentclass{article}
\usepackage{tikz-cd}
\tikzset{Loop/.style = {loop below, out=240, in=300, looseness=5},
execute at end node=\vphantom{4},
bend angle=45,
column 3/.style = {column sep=7mm},
column 4/.style = {column sep=7mm}
}
\begin{document}
\begin{tikzcd}[column sep=1.5cm,
arrows={-Stealth, shorten >=1pt},
cells={nodes={draw, circle, minimum size=8mm, inner sep=0pt, fill=green!10}},
]
|[fill=blue!30]| 1 \rar[shift left]
\ar[Loop] \ar[loop below, densely dotted, out=250, in=290, looseness=5]
& 2 \lar[shift left]\rar[shift left]\lar[bend right]
\ar[Loop]
& 3 \lar[shift left]\rar[shift left]\lar[bend right]
\ar[Loop]
& |[draw=none,fill=none]|\cdots \lar[shift left]\rar[shift left]
& m \lar[shift left]
\ar[Loop]
\end{tikzcd}
\end{document}