TikZ-PGF – Using to Path with Relative Coordinates

tikz-pgf

The following gives an error with PGF 2.00:

\begin{tikzpicture}
  \draw (0,0) to +(1,1);
\end{tikzpicture}

I could swear that this worked once with a different version, but I don't know which. The problem seems to be that the target coordinate of a "to" path must begin with a parenthesis. But drawing "to" paths to a relative coordinate is a really useful thing to be able to do! Is there any way around this? The only one I've thought of so far is to use the "calc" package instead:

\begin{tikzpicture}
  \draw (0,0) to ($(0,0)+(1,1)$);
\end{tikzpicture}

but this quickly gets cumbersome, especially with a path of several segments using ++ relative coordinates in the middle.

Edit: Yes, of course in this simple example I could replace to with --, but the point is that I want to use other to paths, specifically curve to.

Edit: Here's a more complicated thing I'd like to be able to do:

\begin{tikzpicture}
  \filldraw[fill=gray] (0,0) to[out=20,in=180] ++(2,1)
    to[out=0,in=160] ++(2,-1) -- cycle;
\end{tikzpicture}

Best Answer

Update (2011-05-15): I've just tested this with PGF2.10 and it appears that relative coordinates are now supported (again). So if you are able to upgrade to 2.10, the following hacks are unnecessary.


That is annoying!

If you're just drawing straight lines then you can replace the to by -- and it will work. However, that won't fix more general to paths. If you have a lot of to paths that are essentially the same, you can specify your own variant which takes relative coordinates. For example, to get the example that you gave to work (I've added in nodes to show that they work as expected), you could do:

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\draw (0,0) to node {x} (1,1);
\begin{scope}[to path={-- +(\tikztotarget) \tikztonodes}]
\draw (0,1) to node {y} (1,1);
\end{scope}
\end{tikzpicture}
\end{document}

To define this globally, you can use the \tikzstyle command:

\documentclass{article}
\usepackage{tikz}
\tikzstyle{rel line to}= [to path={-- +(\tikztotarget) \tikztonodes}]
\begin{document}
\begin{tikzpicture}
\draw (0,0) to node {x} (1,1);
\draw (0,1) to[rel line to] node {y} (1,1);
\end{tikzpicture}
\end{document}

To see how to modify the other types of to path, you need to look in the file tikzlibrarytopath.code.tex (using zsh: less $(kpsewhich tikzlibrarytopath.code.tex)). Some of the definitions look easy to modify (simply add a + in the right place), but some might be a little more complicated as the target coordinate might be used in more than one place. If there's a specific type that you want to get working and you can't figure it out just ask!