
いくつかのノード(そのうちのいくつかは交差ライブラリを使用して計算されます)があり、それらのノードのいくつかを通る滑らかな曲線を描画し、各ノード ペア間の曲線にラベルを付けたいと考えています。
この\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}