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.

In the reply to this question on shadows, it was suggested to use the backgrounds library so that the shadow of one object doesn't overlap another object drawn previously.

If I understand the documentation correctly, I have to pass on background layer as an option to the scope, not to the path. On the other hand, a preaction constructs a scope internally (according to the docs).

Is it possible to change the options for the scope that is constructed internally by preaction? How? If not, an enhancement to TikZ would solve the "overlapping shadows" problem in an elegant way.

share|improve this question
    
It's possible that the methods in the question tex.stackexchange.com/q/20425/86 might help. –  Andrew Stacey Mar 6 '12 at 10:46
    
That's impressive, thank you! –  krlmlr Mar 6 '12 at 10:59
    
I should make clear that I don't know if that will work - I haven't tested it in this case. But it might, and if not it might give you (or someone) a start to finding something that does work. –  Andrew Stacey Mar 6 '12 at 11:02
    
Are you willing to take the challenge and answer the associated bounty question? :-) –  krlmlr Mar 6 '12 at 12:31
add comment

1 Answer

up vote 5 down vote accepted

Much to my surprise, the solution from the question "Z-level" in TikZ seems to work with preactions and postactions. Here's a fairly simple example.

\documentclass{article}
%\url{http://tex.stackexchange.com/q/46957/86}

\usepackage{tikz}
\pgfdeclarelayer{back}
\pgfsetlayers{back,main}

\makeatletter
\pgfkeys{%
  /tikz/on layer/.code={
    \pgfonlayer{#1}\begingroup
    \aftergroup\endpgfonlayer
    \aftergroup\endgroup
  },
  /tikz/node on layer/.code={
    \pgfonlayer{#1}\begingroup
    \expandafter\def\expandafter\tikz@node@finish\expandafter{\expandafter\endgroup\expandafter\endpgfonlayer\tikz@node@finish}%
  },
}

\begin{document}
\begin{tikzpicture}
\draw[line width=1cm,red] (2,1) -- (2,-1);
\draw[ultra thick,white,preaction={on layer=back,line width=1cm,blue,draw}] (0,0) -- (4,0);
\draw[line width=1cm,red] (2,-2) -- (2,-4);
\draw[ultra thick,white,postaction={on layer=back,line width=1cm,blue,draw}] (0,-3) -- (4,-3);
\begin{scope}[xshift=5cm]
\draw[line width=1cm,red] (2,1) -- (2,-1);
\draw[ultra thick,white,preaction={line width=1cm,blue,draw}] (0,0) -- (4,0);
\draw[line width=1cm,red] (2,-2) -- (2,-4);
\draw[ultra thick,white,postaction={line width=1cm,blue,draw}] (0,-3) -- (4,-3);
\end{scope}
\end{tikzpicture}
\end{document}

Result:

TikZ pre- and post-actions with z-level

The left-hand pictures have the layer set for the blue line, the right do not. The upper have the blue line drawn as a preaction, the lower as a postaction. In each, the vertical red line is drawn before the horizontal lines. So for the upper two, the order of specifying is: red, blue, white. For the lower two, the order is: red, white, blue (coincidence, I assure you). With the "on layer" set, the order of rendering is: blue, red, white. This shows that the blue line (the action line) is sent to the back by the on layer=back key.

share|improve this answer
    
Amazing! Apart from the missing \makeatother, could you please also provide an example for node on layer? –  krlmlr Mar 6 '12 at 19:14
    
Yes, I spotted that before looking at your other question but not in time for this one! I'll add the example for node on layer to the original question as it fits better there. –  Andrew Stacey Mar 6 '12 at 19:30
    
Aagh. node on layer is no longer working for me. A workaround is \path[on layer=back] node .... –  Andrew Stacey Mar 6 '12 at 20:17
    
Perhaps some PGF internals have changed? More than half a year has passed since you answered the other question... Is \node[node on layer=back] still the correct syntax? –  krlmlr Mar 6 '12 at 20:31
    
@user946850 See original question: I've fixed the node on layer code. –  Andrew Stacey Mar 7 '12 at 19:25
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.