3

I have some nodes (some of which are calculated using the intersections library) and want to draw a smooth curve through some of those nodes, labeling the curve between each node pair.

The \draw plot[smooth] coordinates {...} command draws a nice smooth curve. How can I label the segments of that curve?

The only workaround I could come up with is drawing the straight lines in white, so that only the labels are visible, but I'm sure there is a better solution, since that way label positioning relative to the smooth curve varies greatly (e.g. label 3 is far from the curve whereas label 4 is very close).

\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}

My attempt

2 Answers 2

7

You can use the show path construction decoration.

\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}

enter image description here

1
5

If you aren't too wedded to using plot[smooth] then you can use the hobby TikZ library to join the coordinates by smooth curves. This creates Bézier curves between each pair of coordinates and it is possible to put nodes on these paths in the usual way.

\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}

Labelled smooth curve

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.