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?

  • 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 '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.... –  Jan 19 '12 at 17:49
  • @MarcvanDongen: The motivation here is that control sequences acts as short-hand for their <replacement text>: \def\<cmd>{<replacement text>}. – Werner Jan 19 '12 at 19:55
  • @Werner Sure, but doesn't it depend on the TikZ parser? For example, after the definitions \def\A{(0,0) \B {hello}}\def\B{node} you cannot write \tikz\draw\A\B;. –  Jan 19 '12 at 20:42
  • 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 01:28
  • 1

1 Answers1

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.

Frédéric
  • 11,087
  • 3
  • 37
  • 43
  • @Fr'ed'eric Thanks. I have to study this in more detail. (I've never used postactions.) –  Jan 19 '12 at 20:44