21

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}

2 Answers 2

9

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!

4
  • That's a good idea. It's suboptimal that the relative coordinate argument doesn't come with a + and so could easily be mistaken for an absolute one, especially if the relative to path is defined globally; I can imagine this causing much confusion in someone else reading my source code (such as me 6 months in the future, cf. Eagleson's law). But this might be the best hack possible. Sep 20, 2010 at 16:11
  • @Mike I completely agree that it's suboptimal (and I'd consider mentioning this as a bug/feature request on the pgf mailing list, though as several of its developers are here they might have noticed anyway). Naming new types of 'to' path at least gives you the chance to make sure that the name is very clearly a relative one. Sep 20, 2010 at 17:57
  • I'm surprised that this isn't more commonly asked. It seems to me like a very natural and useful thing to want to do. Sep 20, 2010 at 18:46
  • I think that file should be tikzlibrarytopaths.code.tex. Sep 21, 2010 at 17:42
2

Is this simple enough?:

\path (0,0) node (y) {} +(-1,-2) edge [-] (y);

(Yes, I do appreciate this means inverting the coordinates, but...)

4
  • Inverting the coordinates and also switching in= and out= and switching -> and <-. Given that, it works for drawing a single edge, but not if, say, this is part of a path with many nodes which I also want to fill at the same time. Sep 20, 2010 at 16:07
  • @Mike Shulman: yes, of course. My answer targetted only your initial problem; perhaps you'd like to revise the question with a more complex example we could have a go at? Sep 21, 2010 at 17:32
  • Okay, I added another example. Sep 21, 2010 at 17:47
  • @Mike Shulman: Oh, I see. I'm afraid I give up on that one -- can't see a simple way to specify relative coordinates with to. Sorry. Sep 25, 2010 at 17:12

You must log in to answer this question.

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