问题:
在我的图中,我只想在节点 v9 处绘制一个自循环。我该怎么做?
梅威瑟:
\documentclass[10pt]{article}
\usepackage{pgf,tikz,pgfplots}
\pgfplotsset{compat=1.15}
\usepackage{mathrsfs}
\usetikzlibrary{arrows}
\pagestyle{empty}
\usetikzlibrary{decorations.markings}
\begin{document}
\tikzset{->-/.style n args={2}{decoration={
markings,
mark=at position #1 with {\arrow[line width=2pt]{#2}}},postaction={decorate}}}
\begin{tikzpicture}
\draw[line width=0.5pt,->-={0.5}{stealth}] (0.0,3.0) -- (3.0,3.0);
\draw[line width=0.5pt,->-={0.5}{stealth}] (3.0,3.0) -- (3.0,0.0);
\draw[line width=0.5pt,->-={0.5}{stealth}] (3.0,0.0) -- (0.0,0.0);
\draw[line width=0.5pt,->-={0.5}{stealth}] (3.0,3.0) -- (0.0,0.0);
\draw[line width=0.5pt,->-={0.5}{stealth}] (0.0,0.0) -- (-1.50,1.50);
\draw[line width=0.5pt,->-={0.5}{stealth}] (-1.50,1.50) -- (0.0,3.0);
\draw[line width=0.5pt,->-={0.6}{stealth}] (3.0,0.0) -- (6.0,0.0);
\draw[line width=0.5pt,->-={0.5}{stealth}] (6.0,0.0) -- (6.0,3.0);
\draw[line width=0.5pt,->-={0.6}{stealth}] (5.0,2.0) -- (6.0,3.0);
\draw[line width=1pt,->-={0.5}{stealth}] (7.0,2.0) -- (7.0,2.0);
\draw [fill=black] (0,3) circle (1.5pt);
\draw[color=black,left] (0.25,3.43) node {$v_1$};
\draw [fill=black] (0,0) circle (1.5pt);
\draw[color=black,below] (0.0,-0.10) node {$v_3$};
\draw [fill=black] (3,0) circle (1.5pt);
\draw[color=black,below] (3.0,-0.2) node {$v_5$};
\draw [fill=black] (3,3) circle (1.5pt);
\draw[color=black] (3.0,3.43) node {$v_2$};
\draw [fill=black] (-1.52,1.5) circle (1.5pt);
\draw[color=black,left] (-1.7,1.5) node {$v_4$};
\draw [fill=black] (6,0) circle (1.5pt);
\draw[color=black,left] (6.36,-0.4) node {$v_6$};
\draw [fill=black] (6,3) circle (1.5pt);
\draw[color=black,left] (6.36,3.43) node {$v_7$};
\draw [fill=black] (5,2) circle (1.5pt);
\draw[color=black,left] (4.9,2.0) node {$v_8$};
\draw [fill=black] (7,2) circle (1.5pt);
\draw[color=black,right] (7.16,2.0) node {$v_9$};
\end{tikzpicture}
\end{document}
答案1
代码的最后两行替换为
\draw[fill] (7,2) circle (1.5pt) node[right] {$v_9$};
\draw[line width=0.5pt,->-={0.6}{stealth}]
(7,2) to [out=45, in=-45, distance=17mm] (7,2);
你将获得:
你的代码可以大大缩短。当我有空的时候我会告诉你。
附录: 缩短 MWE 的建议:
\documentclass[10pt]{article}
\usepackage{tikz}
\usepackage{mathrsfs}
\pagestyle{empty}
\usetikzlibrary{arrows.meta,
decorations.markings,
positioning}
\begin{document}
\begin{tikzpicture}[
node distance = 12mm and 12mm,
decoration = {markings, mark=at position 0.55 with {%
\arrow{Stealth[length=3mm]}},
},
dot/.style = {circle, fill, minimum size=5pt,
inner sep=0pt, outer sep=0pt,
node contents={}},
every edge/.style = {draw, semithick,postaction={decorate}},
]
\node (v4) [dot,label= left:$v_4$];
\node (v1) [dot,label=above:$v_1$, above right=of v4];
\node (v2) [dot,label=above:$v_2$, right=24mm of v1];
\node (v3) [dot,label=below:$v_3$, below right=of v4];
\node (v5) [dot,label=below:$v_5$, right=24mm of v3];
\node (v8) [dot,label= left:$v_8$, below right=of v2];
\node (v7) [dot,label=above:$v_7$, above right=of v8];
\node (v6) [dot,label=below:$v_6$, below right=of v8];
\node (v9) [dot,label= left:$v_9$, below right=of v7];
%
\path (v4) edge (v1)
(v1) edge (v2)
(v2) edge (v3)
(v3) edge (v4)
%
(v2) edge (v5)
(v5) edge (v3)
%
(v5) edge (v6)
(v6) edge (v7)
%
(v8) edge (v7)
% loop, distance: distance to loop peak
(v9) edge [out=50, in=-50, distance=24mm] (v9);
\end{tikzpicture}
\end{document}
答案2
如果你希望匹配其他边缘的风格,那么使用to
带角度和其他调整的路径可能是可行的方法。最简单的版本是
\draw[line width=0.5pt,->-={0.5}{stealth}] (7.0,2.0) to [out=45,in=135,min distance=3cm,looseness=8] (7.0,2.0);
或者你可以向路径添加额外的点来指定其形状
\draw[line width=0.5pt,->-={0.5}{stealth}] (7.0,2.0) to [out=-45,in=0]
(7.2,0.0) -- (6.8,0.0) to[out=180,in=-135] (7.0,2.0);
\documentclass[10pt]{article}
\usepackage{pgf,tikz,pgfplots}
\pgfplotsset{compat=1.15}
\usepackage{mathrsfs}
\usetikzlibrary{arrows}
\pagestyle{empty}
\usetikzlibrary{decorations.markings}
\begin{document}
\tikzset{->-/.style n args={2}{decoration={
markings,
mark=at position #1 with {\arrow[line
width=2pt]{#2}}},postaction={decorate}}}
\begin{tikzpicture}
\draw[line width=0.5pt,->-={0.5}{stealth}] (0.0,3.0) -- (3.0,3.0);
\draw[line width=0.5pt,->-={0.5}{stealth}] (3.0,3.0) -- (3.0,0.0);
\draw[line width=0.5pt,->-={0.5}{stealth}] (3.0,0.0) -- (0.0,0.0);
\draw[line width=0.5pt,->-={0.5}{stealth}] (3.0,3.0) -- (0.0,0.0);
\draw[line width=0.5pt,->-={0.5}{stealth}] (0.0,0.0) -- (-1.50,1.50);
\draw[line width=0.5pt,->-={0.5}{stealth}] (-1.50,1.50) -- (0.0,3.0);
\draw[line width=0.5pt,->-={0.6}{stealth}] (3.0,0.0) -- (6.0,0.0);
\draw[line width=0.5pt,->-={0.5}{stealth}] (6.0,0.0) -- (6.0,3.0);
\draw[line width=0.5pt,->-={0.6}{stealth}] (5.0,2.0) -- (6.0,3.0);
\draw[line width=1pt,->-={0.5}{stealth}] (7.0,2.0) -- (7.0,2.0);
\draw [fill=black] (0,3) circle (1.5pt);
\draw[color=black,left] (0.25,3.43) node {$v_1$};
\draw [fill=black] (0,0) circle (1.5pt);
\draw[color=black,below] (0.0,-0.10) node {$v_3$};
\draw [fill=black] (3,0) circle (1.5pt);
\draw[color=black,below] (3.0,-0.2) node {$v_5$};
\draw [fill=black] (3,3) circle (1.5pt);
\draw[color=black] (3.0,3.43) node {$v_2$};
\draw [fill=black] (-1.52,1.5) circle (1.5pt);
\draw[color=black,left] (-1.7,1.5) node {$v_4$};
\draw [fill=black] (6,0) circle (1.5pt);
\draw[color=black,left] (6.36,-0.4) node {$v_6$};
\draw [fill=black] (6,3) circle (1.5pt);
\draw[color=black,left] (6.36,3.43) node {$v_7$};
\draw [fill=black] (5,2) circle (1.5pt);
\draw[color=black,left] (4.9,2.0) node {$v_8$};
\draw [fill=black] (7,2) circle (1.5pt);
\draw[color=black,right] (7.16,2.0) node {$v_9$} ;
\draw[line width=0.5pt,->-={0.5}{stealth}] (7.0,2.0) to [out=45,in=135,min distance=3cm,looseness=8] (7.0,2.0);
\draw[line width=0.5pt,->-={0.5}{stealth}] (7.0,2.0) to [out=-45,in=0]
(7.2,0.0) -- (6.8,0.0) to[out=180,in=-135] (7.0,2.0);
\end{tikzpicture}
\end{document}