我有一些节点(其中一些是使用交叉点库计算的),并且想要通过其中一些节点绘制一条平滑的曲线,标记每个节点对之间的曲线。
该\draw plot[smooth] coordinates {...}
命令绘制了一条漂亮的平滑曲线。我该如何标记该曲线的各段?
我能想到的唯一解决方法是用白色画直线,这样就只能看到标签了,但我确信有更好的解决方案,因为这样标签相对于平滑曲线的定位就会有很大差异(例如标签 3 离曲线很远而标签 4 非常近)。
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{calc}
\usetikzlibrary{intersections}
\usetikzlibrary{positioning}
\begin{document}
\tikzstyle{vertex}=[circle,draw=black!30,fill=black!30,inner sep=1pt]
\begin{tikzpicture}[scale=2]
\node[vertex] (a) at (0,0) {};
\node[vertex] (b) at (2,-.3) {};
\node[vertex] (c) at (2.5, 1.3) {};
\node[vertex] (d) at (3.5, .6) {};
\node[vertex] (e) at (3, .2) {};
\draw (a) -- node[above,pos=.2] {1} (b) -- node[left] {2} (c) -- node[below left] {3} (d) -- node[below] {4} (e);
\draw[red] plot[smooth] coordinates {(a) (b) (c) (d) (e)};
\end{tikzpicture}
\end{document}
答案1
您可以使用show path construction
装饰品。
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{calc}
\usetikzlibrary{decorations.pathreplacing}
\newcounter{smoothcounter}
\begin{document}
\begin{tikzpicture}[scale=2,
vertex/.style={circle,draw=black!30,fill=black!30,inner sep=1pt},
label smooth/.style={decorate,decoration={show path construction,
curveto code={
\path (\tikzinputsegmentfirst) .. controls
(\tikzinputsegmentsupporta) and (\tikzinputsegmentsupportb) ..(\tikzinputsegmentlast)
coordinate[pos=0.4] (aux1) coordinate[pos=0.6] (aux2)
(aux1) to[edge label={$\stepcounter{smoothcounter}\arabic{smoothcounter}$}] (aux2);
}}
}]
\node[vertex] (a) at (0,0) {};
\node[vertex] (b) at (2,-.3) {};
\node[vertex] (c) at (2.5, 1.3) {};
\node[vertex] (d) at (3.5, .6) {};
\node[vertex] (e) at (3, .2) {};
\draw (a) -- node[above,pos=.2] {1} (b) -- node[left] {2} (c) -- node[below left] {3} (d) -- node[below] {4} (e);
\setcounter{smoothcounter}{0}
\draw[red,postaction=label smooth] plot[smooth] coordinates {(a) (b) (c) (d) (e)};
\end{tikzpicture}
\end{document}
答案2
如果你不太习惯使用,plot[smooth]
那么你可以使用hobby
TikZ 库通过平滑曲线连接坐标。这会在每对坐标之间创建贝塞尔曲线,并且可以按常规方式将节点放在这些路径上。
\documentclass{article}
%\url{https://tex.stackexchange.com/q/570505/86}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{calc}
\usetikzlibrary{intersections}
\usetikzlibrary{positioning}
\usetikzlibrary{hobby}
\begin{document}
\tikzstyle{vertex}=[circle,draw=black!30,fill=black!30,inner sep=1pt]
\begin{tikzpicture}[scale=2]
\node[vertex] (a) at (0,0) {};
\node[vertex] (b) at (2,-.3) {};
\node[vertex] (c) at (2.5, 1.3) {};
\node[vertex] (d) at (3.5, .6) {};
\node[vertex] (e) at (3, .2) {};
\draw[red,use Hobby shortcut,text=black] (a.center) .. node[auto] {\(1\)} (b.center) .. node[auto] {\(2\)} (c.center) .. node[auto] {\(3\)} (d.center) .. node[auto] {\(4\)} (e.center);
\end{tikzpicture}
\end{document}