# Applying a postaction to every path in TikZ

Sometime, for example in a solution to a recent question, one wants to apply a postaction (like a decoration) to a group of paths. A naive solution is to use every path/.style={}. Unfortunately this also this then applies the postaction to every path drawn in the postaction, leading to an infinite regress.

Is there any way to avoid this?

A somewhat stupid minimal example of the problem:

\begin{tikzpicture}[every path/.style={postaction={draw}}]
\draw (0,0)--(1,0);
\end{tikzpicture}


with error message

! TeX capacity exceeded, sorry [grouping levels=255].
\toks@ \expandafter {#1#2}\xdef #1{...
l.12     \draw (0,0)--(1,0);

-
Let's see if a bounty can get this question answered. –  Seamus Nov 12 '10 at 13:29
@seamus: much to me surprise it did. :) –  Caramdir Nov 14 '10 at 16:44
(I only did it to get the badge for posting a bounty on someone else's question, but I actually feel like I've helped make the site better, a little bit...) –  Seamus Nov 16 '10 at 11:31
@Seamus: fair enough, it did work. :) –  Ulrich Schwarz Nov 16 '10 at 18:45

Please check if the following solves your problem, in particular with regard to scoping, this seems far too easy. nomorepostactions is intended to locally empty out the postactions, so they don't get applied after that.

It did fix your toy problem.

\makeatletter
\tikzset{nomorepostaction/.code=\let\tikz@postactions\pgfutil@empty}
\makeatother
[...]
\begin{tikzpicture}[every path/.style={postaction={nomorepostaction,draw}}]
\draw (0,0)--(1,0);
\end{tikzpicture}

-
works fine, indeed: tex.stackexchange.com/questions/3161/… –  Grigory M Nov 14 '10 at 10:13
Thanks a lot. I didn’t know about .code. That might be useful in other situations too. –  Caramdir Nov 14 '10 at 16:54
Fantastic! I also didn't know about the .code and have just looked it up in the manual (p383 in v2.00). I can think of lots of things to do with this! (Having small kids, I also like the example for the .store in handler). –  Loop Space Nov 14 '10 at 18:01
@Andrew With small children it may be better to evaluate what happened first, which is why .estore in may be the preferred option. –  Marc van Dongen Jan 14 '12 at 22:40

Maybe this answer on "How to draw an arrow in the middle of the line?" might help you. This way you do not run into a recursion.

-
The point was that in my answer to the question you reference, one needs to add [postaction={decorate}] to every path by hand which in larger examples adds a lot of copy&pasted code. And copy&pasting code is usually bad design. –  Caramdir Oct 10 '10 at 16:03

Usually one can override properties, but is there a way to remove 'postaction' completely? I mean, there should be a way to write something like [every path/.style={postaction={nopostaction}},nopostaction/.style={draw, <some way to override postaction option>}], probably.
Interesting approach. I don’t know what to write in place of <some way to override postaction option> though. Setting a postaction inside a postaction seems to get ignored. –  Caramdir Nov 13 '10 at 17:15
In the original example even replacing postaction={draw} by postaction={} leads to an infinite recursion. –  Caramdir Nov 13 '10 at 17:20