8

Why doesn't the pos = x option work when the node is used in the plot command?

\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}
  \draw plot[domain = 0:2, samples = 100] ({\x}, {(\x)^2}) node[pos = 1.1] 
  {\(y = x^2\)};
\end{tikzpicture}
\end{document}

In this picture, f_{quad} is my node that should be at pos = 1.1.

enter image description here

Additionally, it doesn't matter what I make pos equal to. It is always at the origin.

7

My feeling is that the plot operation is special and the way in which TikZ process it differs with respect to how canonical paths are processed. Quoting the manual:

The plot path operation can be used to append a line or curve to the path that goes through a large number of coordinates.

This suggests that the whole plot is appended to the path and thus that could be a possible reason why nodes can be appended only at the beginning or at the end of a plot path.

However, it is always possible to exploit the decorations.markings library as a workaround.

An example:

\documentclass[tikz,png,border=10pt]{standalone}
\usetikzlibrary{decorations.markings}

\tikzset{insert node/.style args={#1 at #2}{
    postaction=decorate,
    decoration={
      markings,
      mark= at position #2
        with
        {
         #1
        }
    }
  }
}
\begin{document}
\begin{tikzpicture}
  \draw[insert node={\node[red,left]{\(y = x^2\)};} at 0.65,
  insert node={\node[blue,draw,right]{\(y = x^2\)};} at 0.45,
  insert node={\node[green!80!black,above]{\(y = x^2\)};} at 1,
  ] plot[domain = 0:2, samples = 100] ({\x}, {(\x)^2});
\end{tikzpicture}
\end{document}

The result:

enter image description here

  • Thanks for your solution. At the present, I will use it, but I plan on waiting a few days to see if anyone has a fix. If not, I will surely accept. – dustin Aug 16 '13 at 19:13
  • @dustin: you're welcome. :) – Claudio Fiandrino Aug 16 '13 at 19:22
6

Regardless of whether you use the pos key, a node will not be placed along a subpath unless an internal "timer" command as been specified, which determines how a node will be positioned along the last subpath (e.g., a line or a curve). Path construction commands specify the appropriate timer and also set other parameters which the timer requires.

In the case of plots, a timer is not specified and it is difficult to see how they could be given that most plots are essentially made up of lots of very very short lineto subpaths. Even if the parameters were set the last subpath would be a very very short straight line (depending on the number of samples) meaning any pos value would place the node at (or very near) the end of the plot.

Furthermore, if you set the pos key when a timer has not been specified (which is the case at the end of a plot) then the node is not moved to any position (even if you use at) and is dumped at the origin (I'm not saying this is desirable, this is just what happens currently). If you remove the pos key the node is placed centered (depending on the anchor) on the last point of the plot.

As has been pointed out, the markings decoration can be used, however, as the manual states, as decorations use TeX for maths it isn't guaranteed to be very accurate with paths made up of lots of very short subpaths.

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.