Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}
share|improve this question

2 Answers 2

up vote 9 down vote accepted

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!

share|improve this answer
    
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. –  Mike Shulman Sep 20 '10 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. –  Loop Space Sep 20 '10 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. –  Mike Shulman Sep 20 '10 at 18:46
    
I think that file should be tikzlibrarytopaths.code.tex. –  Mike Shulman Sep 21 '10 at 17:42

Is this simple enough?:

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

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

share|improve this answer
    
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. –  Mike Shulman Sep 20 '10 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? –  Brent.Longborough Sep 21 '10 at 17:32
    
Okay, I added another example. –  Mike Shulman Sep 21 '10 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. –  Brent.Longborough Sep 25 '10 at 17:12

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.