4

I have trouble defining a node on a path. If the path is described using points and the in/outgoing angle, the node is always at the beginning. If the path is described with control points, it works flawlessly.

In the code below you see that one path correctly has a node about midway, whereas for the second path the node will be placed at the start of the path.

\documentclass[border=5pt]{standalone}

\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{math,calc}

\usepackage{amsmath}

\begin{document}
\begin{tikzpicture}
    \draw (0,0) .. controls (1,2) and (4,3) .. (5,5)
    node [pos=0.5] (mid1) {};
    \draw[fill] (mid1) circle (1pt);

    \draw (0,0) to[out=90, in=-180]  (5,5)
    node [pos=0.5] (mid2) {};
    \draw[fill] (mid2) circle (1pt);
\end{tikzpicture}
\end{document}

I have a path constructed as in the second example, how can a place a node at a specific relative position (2/3 in, say)?

I am also interested why tikz treads the two paths so differently and why "node [pos= ]" works in one of the cases, but not the other.

6

I will try to explain a little bit because the OP is asking about reasons.

Keep in mind that there are two methods to place a node on a line or curve: explicit placement and implicit placement.

  • Explicit: putting the node after ending point of the line/curve, e.g. (a) .. controls (c) and (d) .. (b) node[pos=...] {...}.
  • Implicit: putting the node before ending point of the line/curve. e.g. (a) -- node[pos=...] {...} (b).

The difference between them is that implicit placements inherit outer options (for example, options of tikzpicture or scope environment) when no inner options are given. But if inner options are given, it will be the final ones accepted by TikZ, no matter which type of placement you are using. See an example below:

\begin{tikzpicture}[pos=0.8]
\draw (1,0) -- (4,0) node {A}
      (1,.5) -- (4,.5) node[midway] {A'};
\draw (1,1) -- node {B} (4,1)
      (1,1.5) -- node[midway] {B'} (4,1.5);
\end{tikzpicture}

You will get something like below: enter image description here


Ok, let' back to the original OP question.

One question is about the reason. I've explained some above. Actually, I don't really know the exact reason behind; but it's always safer to implicitly place nodes when no outer options need to be inherited. (UPDATE: the to operation only allows implicit placement)

So please use:

\draw (0,0) to[out=90, in=-180] node [pos=0.5] (mid2) {} (5,5);

Another question is how to put a node using a given absolute position with respect to the line. If you mean "2/3 inch" along the curve, I'd say I have no idea how to achieve that.

  • The reason is that the to path construction do not accept the explicit form (p.157 of v3.0.1a says \path ... to[<options>] <nodes> <coordinate or cycle> ...;) – Kpym Apr 28 '18 at 6:22
  • @Kpym Thank you. When I read this line in the manual I even didn't realize that it implies this. I guess you mean "implicit" and I've corrected my answer. – wklchris Apr 28 '18 at 8:13
4

Actually, it works :)

enter image description here

A small change in the order of things made it not work for you.

Where you have:

\draw (0,0) to[out=90, in=-180] (5,5) node [pos=0.5] (mid2) {}      ;

you should change to:

\draw (0,0) to[out=90, in=-180]       node [pos=0.5] (mid2) {} (5,5);

Notice that the node [] () {} is before the final coordinate.

\documentclass[border=5pt]{standalone}

\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{math,calc}

\usepackage{amsmath}

\begin{document}
\begin{tikzpicture}
    \draw (0,0) .. controls (1,2) and (4,3) .. (5,5)
    node [pos=0.5] (mid1) {};
    \draw[fill] (mid1) circle (1pt);

    \draw (0,0) to [out=90, in=-180] node [pos=0.5] (mid2) {} (5,5);
    \draw[fill] (mid2) circle (1pt);
\end{tikzpicture}
\end{document}
  • Thank you for the quick answer. I accepted the other answer since it goes a bit more into details about the why part of the question with a reference to the manual. – mna Apr 28 '18 at 17:24
  • @mna No worries :) His answer is really much better than mine. I too didn't know the why of this, I only knew how to fix it :/ – Phelype Oleinik Apr 28 '18 at 17:41

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.