7

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 http://csweb.ucc.ie/~dongen/TeX-SX/11--12/path.png

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?

7
  • 11
    Why not \def it? \def\mypath{ (0,0) -- (5,5)} and then using it \tikz \draw {\mypath};. Not so elegant though.
    – percusse
    Jan 19, 2012 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....
    – user10274
    Jan 19, 2012 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. Jan 19, 2012 at 23:06
  • 2
    For the interested people this is the question that @AndrewStacey mentions.
    – percusse
    Jan 20, 2012 at 1:28
  • 1
    Similar question.
    – qubyte
    Jan 20, 2012 at 2:17

1 Answer 1

5

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.

1
  • @Fr\'ed\'eric Thanks. I have to study this in more detail. (I've never used postactions.)
    – user10274
    Jan 19, 2012 at 20:44

You must log in to answer this question.