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

4
  • Does hobby not support giving the nodes names using the usual syntax (name)? I tried, but couldn’t seem to get it to work.
    – Gaussler
    Commented Nov 28, 2022 at 17:09
  • @Gaussler I just tried it and it worked how I expected it to; if you have some code that's not working, either ask a new question here or open an issue on the github page (github.com/loopspace/hobby). Commented Nov 28, 2022 at 21:06
  • I will do that, but what I meant was that I hoped to be be able to use a notation such as node {1} (2,7) (name1) to assign the name (name1) to the coordinate (2,7), just like I can with the usual TikZ syntax. But that didn’t seem possible.
    – Gaussler
    Commented Nov 29, 2022 at 14:59
  • @Gaussler Snippets of code are hard to debug so if you post the full code somewhere (and bring it to my attention) then I'll have a look. Commented Nov 29, 2022 at 21:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .