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!
Variables, or more correctly styles or keys, in pgf
is a very powerful feature introduced to aid the creation of the drawing framework. And here I say pgf
as that is in fact the
underlying mechanism which TikZ
uses so much. Notice that \tikzset
is equivalent to \pgfkeys{/tikz/.cd,#1}
.
If you want a quick answer, goto Scopes of keys/styles.
In my answer many will see things also mentioned in the manual. This is intentionally.
I will not go over the tree structure, neither the very powerful features introduced by the handlers, nor will I go over .code as here it is
.styles that govern the user interface. I will not distinguish between .styles and .code as it is not aiding the basic explanation of the key structure.
Styles: shortcuts for code execution
This is the most often used feature when a picture/path is created. Every time a path is added thick
, color=red
or line width=2mm
.
A .style is in effect a shortcut or an abbreviation for single/multiple .code keys. This means that .style really doesn't do anything, it is
the reference herein that does the job. Notice that a .style could equally refer to any other .style.
Styles are therefore often used when you need to shorten, or do multiple things to a path.
An example:
\tikzset{%
my box color red/.style={%
draw,color=red!50!white,fill=black!20!red!50!white
}
}
Now look at the following two paths. They are in fact completely identical.
\path[my box color red] (0,0) -- (1,0);
\path[draw,color=red!50!white,fill=black!20!red!50!white] (0,.1) -- (1,.1);
Notice that the fact that my box color red
refers to other styles, i.e. draw
, color=red!50!white
and fill=black!20!red!50!white
.
We then later realised that my box color <color>
has to be used several places with several different colors.
One solution could be to generate .styles for each color that will be used, however, that seams redundant.
Every style is made so that it will take one optional argument.
This means that instead we can define the .style as:
\tikzset{%
my box color/.style={%
draw,color=#1!50!white,fill=black!20!#1!50!white
}
}
Now look at the following two and two paths. They are in fact completely identical, two and two respectively.
\path[my box color=red] (0,0) -- (1,0);
\path[draw,color=red!50!white,fill=black!20!red!50!white] (0,.1) -- (1,.1);
\path[my box color=blue] (0,0) -- (1,0);
\path[draw,color=blue!50!white,fill=black!20!blue!50!white] (0,.1) -- (1,.1);
We created my box color
because it was way easier to typeset this and because I have to use it many times.
Styles and their defaults
The above style my box color
will not compile if used as:
\path[my box color] (0,0) -- (1,0);
and the reason is that the style color
has a mandatory argument. This you will learn by mistake, I did. Therefore the above code will never compile.
Often, however, you want default behaviours if no argument is specified.
Take for instance this style which adds an arrow to a path. It takes one argument which is the fractional position of the arrow on the path.
\tikzset{%
->-/.style={postaction={decorate},decoration={%
markings,mark=at position #1 with {\arrow{stealth}}%
}%
}%
}
This seams obvious that it should always place the arrow at the 0.5
position on the path. But it doesn't unless you specify ->-=0.5
everytime you need
to use it.
This is accompanied by the use of the .default specification on the style.
So:
\tikzset{%
->-/.default=.5%
}
ensures that if you do not specify the fractional position on the path it will take it as if you specified ->-=0.5
.
\begin{tikzpicture}
% Will have arrow at 0.5
\draw[->-] (0,0) -- (1,0);
% Will have arrow at 0.3
\draw[->-=0.3] (0,.1) -- (1,.1);
\end{tikzpicture}
Lifetime of used styles
Sometimes you will be in the situation of defining several styles with the same reference to another style.
Example:
\tikzset{%
my box/.style={thin,color=red},
my thick box/.style={thick,color=blue}
}
\begin{tikzpicture}
% line 1
\draw[my box] (0,0) -- (1,0);
% line 2
\draw[my thick box] (0,.1) -- (1,.1);
% line 3
\draw[my thick box,my box] (0,.2) -- (1,.2);
% line 4
\draw[my box,my thick box] (0,.3) -- (1,.3);
% line 5
\draw[my box,my thick box,thin] (0,.3) -- (1,.3);
\end{tikzpicture}
In the above picture you see 5 lines. The first two are pretty obvious to have their respective colors of red and blue. But the last two?
This can be answered by knowing that the keys are read and executed consecutively. The lines will thus have the following properties:
- Red and thin
- Blue and thick
- Red and thin
- Blue and thick
- Blue and thin
Thus you do not need to worry about the keys overlapping, just know that it is the last seen key which takes precedence on the others.
Scopes of keys/styles
The scope, as your question basically refers to, is very important in TikZ
.
You should expect that keys/styles have the same scope as normal TeX
groups, i.e. they live until the end of the current group.
Globally
So if you in the preamble, or elsewhere not in a nested group, set:
\tikzset{%
line width=2mm
}
this will be a globally set variable. It will take effect on all drawings and shapes where the length in line width
is used.
Picture Globally
This refers to styles which only lives inside in a current picture.
\begin{tikzpicture}[line width=10mm] % Picture Globally
% will have width of 10mm
\draw (0,0) -- (1,0);
\end{tikzpicture}
Picture locally
A declaration of a style that will only take on the current path.
\begin{tikzpicture}
% will have width of 7mm
\draw[line width=7mm] (0,0) -- (1,0);
\end{tikzpicture}
Collected
An example of how they are defined at different locations in the document:
\tikzset{%
line width=2mm
}
\begin{tikzpicture}[line width=10mm] % overwrites the above
% will have width of 10mm
\draw (0,0) -- (1,0);
% will have width of 3mm, will override the above
\draw[line width=3mm] (0,0) -- (1,0);
\end{tikzpicture}
\begin{tikzpicture}
% will have width of 2mm, due to the globally defined style.
\draw (0,0) -- (1,0);
% will have width of 7mm, overrides the global set style.
\draw[line width=7mm] (0,0) -- (1,0);
\end{tikzpicture}
Best Answer
\coordinate
is only a shorthand for\path coordinate
. You may thus place coordinates along an arbitrary path which supports relative coordinates without a hassle.