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.

I'd like to know if somebody know a way to define a path in TikZ and reuse the path's definition several times. For example, consider the following:

code example with duplicate paths

If I wanted to change the path (0,2) -- (0,2) -- (5,3) -- (5,2) I'd have to make two changes to the code. How can I avoid this?

share|improve this question
10  
Why not \def it? \def\mypath{ (0,0) -- (5,5)} and then using it \tikz \draw {\mypath};. Not so elegant though. –  percusse Jan 19 '12 at 17:30
    
@percusse Interesting how obvious the solution sometimes is. I'd never have thought TikZ would be able to parse the definition and expand it. The solution is good enough in the sense that you can keep defs local to a tikzpicture. Interestingly, and this is one of the reasons why I never thought of using a \def, TikZ's name path only seems to work for intersections but not for drawing.... –  Marc van Dongen Jan 19 '12 at 17:49
1  
I've written a library that does exactly this. I've limited internet access right now, though so can't paste much here. If you search this site for spath then you might get some idea of the sort of thing I do with my code. If it looks interesting, tell me and I'll try to see if it could do what you want. –  Andrew Stacey Jan 19 '12 at 23:06
2  
For the interested people this is the question that @AndrewStacey mentions. –  percusse Jan 20 '12 at 1:28
1  
Similar question. –  qubyte Jan 20 '12 at 2:17
show 2 more comments

1 Answer

up vote 4 down vote accepted

In a limited way, a postaction or a preaction may be viewed as repeated work on a path. For example, in the following code

\begin{tikzpicture}

\fill[postaction={draw=red,->},blue] (0,3) -- (5,3) -- (5,2) -- (0,2) -- (0,3);

\end{tikzpicture}

the path is only entered once, but two actions are performed on it, the fill and afterwards the draw.

Again, this is limited in what you can do, but it saves on reentering a path.

share|improve this answer
    
@Fr\'ed\'eric Thanks. I have to study this in more detail. (I've never used postactions.) –  Marc van Dongen Jan 19 '12 at 20:44
add comment

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.