# Define node at relative coordinates of \draw plot

I am not able to fix that simple plot. I think I missed something, but I can't point out what ! Could you tell me why (a) is not at midway of the red line ?

\documentclass{article}

\usepackage{tikz}

\begin{document}

\begin{tikzpicture}[domain=0:4]
\draw[very thin,color=gray] (-0.1,-1.1) grid (3.9,3.9);
\draw[->] (-0.2,0) -- (4.2,0) node[right] {$x$};
\draw[->] (0,-1.2) -- (0,4.2) node[above] {$f(x)$};
\draw[color=red] plot (\x,\x) node [midway,inner sep=0] (a) {} node[right] {$f(x) =x$};
\draw[->] (4,1) node[right] {this is not required (a)} -- (a) ;
\node[circle,fill,label={above left:this should be (a)}] at (2,2) {};

\end{tikzpicture}

\end{document}


Is this a duplicate of tikz-parabola-node-position ?

-
It appears to be at the correct spot: (2,2) is midway for f(x)=x with the domain 0:4? Am I missing something? –  Peter Grill Feb 9 '12 at 15:23
@PeterGrill the large circle is not (a) that is to indicate where it should be. (a) is in fact at the origin. It looks like the plot option doesn't allow this kind of positioning. –  Roelof Spijker Feb 9 '12 at 15:26

Unfortunately, the midway (or pos=0.5) syntax doesn't work for TikZ plots (it would work if you used pgfplots to draw your plots).

However, you can use the decorations.markings library to define a coordinate along the path. Here's a style mark position=<pos>(<coordinate name>) that places a coordinate with a specified name at a specified distance along the path. You can call this multiple times if you need more than one position on a path.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{decorations.markings}
\begin{document}

\tikzset{
mark position/.style args={#1(#2)}{
postaction={
decorate,
decoration={
markings,
mark=at position #1 with \coordinate (#2);
}
}
}
}

\begin{tikzpicture}[domain=0:4]
\draw[very thin,color=gray] (-0.1,-1.1) grid (3.9,3.9);
\draw[->] (-0.2,0) -- (4.2,0) node[right] {$x$};
\draw[->] (0,-1.2) -- (0,4.2) node[above] {$f(x)$};
\draw[color=red, mark position=0.5(a)] plot (\x,\x) node[right] {$f(x) =x$};
\node at (a) [left] {this is (a)};

\end{tikzpicture}

\end{document}

-
Indeed, here is another question with a similar take on it. Probably @cjorssen has something to say about the latest status. –  percusse Feb 9 '12 at 15:43
That's very nice. I was looking at decorations to solve this, but couldn't find a straightforward way. This looks very nice though! –  Roelof Spijker Feb 9 '12 at 15:44
Thanks for the tip, but my goal was to defined a node I could use after... I think I'll use pgfplots ! @percusse : indeed, cjorssen solve this using the CVS version of tokz... it's coming ! –  Sigmun Feb 9 '12 at 15:44
@Sigmun if you use a \node instead of a \coordinate you can also name your node with an additional argument. See my previous comment for a similar problem regarding arcs. –  percusse Feb 9 '12 at 15:47
@Jake : This solves the simplified problem I give, but of course, my equation is not (\x,\x) but (\x,{0.12/0.2*(0.2969*sqrt(\x )-0.126*\x -0.3516*\x^2 +0.2843*\x^3 -0.1015*\x^4 )}) and I get ! Dimension too large. at compilation. Have you got a solution ? I see the post which describe the same error. Any idea to solves that ? –  Sigmun Feb 11 '12 at 9:52