5

This is kind of a follow up question of Bug in TikZ? Line width on layers.


When using a {pgfonlayer} two problem occure: a) inside of the background scope settings from e.g. every picture are missing. b) for drawing commands after closing {pgfonlayer} the settings that where active before {pgfonlayer} are lost. While a) seems to be “by design”, I’m not sure about b).

The code below produces this image: result 1) is made with on baclground layer and works as documented.
2) is wrong
3) is the fixed version of 2) 4) and 5) are wrong
6) is again the fixed version.

As you can see it requires two additional {scope}s to get the desired result. While Christian suggested only one as solution in the above mentioned question. This doesn’t seem to work (anymore?).

So basically the question is which of these might be a bug and is worth reporting?

Code

\documentclass{article}

\usepackage{tikz}

\usetikzlibrary{%
   backgrounds,%
}

\tikzset{
   medium/.style={line width=2.5pt},
   thin/.style={line width=0.5pt},
   every picture/.append style={medium},
   every scope/.style={medium},
   every on background layer/.style={every picture},
}

\pgfdeclarelayer{background}
\pgfdeclarelayer{foreground}
\pgfsetlayers{background,main,foreground}

\begin{document}
1 \begin{tikzpicture}
   \draw (0,0) -- (1,1);
   \begin{scope}[on background layer]
      \draw [red] (0,1) -- (1,0);
   \end{scope}
   \draw (0,0) -- (1,0.5);
\end{tikzpicture}
\quad
2 \begin{tikzpicture}
   \draw (0,0) -- (1,1);
   \begin{pgfonlayer}{background}
      \draw [red] (0,1) -- (1,0);
   \end{pgfonlayer}
   \draw (0,0) -- (1,0.5);
\end{tikzpicture}
\quad
3 \begin{tikzpicture}
   \draw (0,0) -- (1,1);
   \begin{scope}
      \begin{pgfonlayer}{background}
         \begin{scope}
            \draw [red] (0,1) -- (1,0);
         \end{scope}
      \end{pgfonlayer}
   \end{scope}
   \draw (0,0) -- (1,0.5);
\end{tikzpicture}
\quad
4 \begin{tikzpicture}
   \draw (0,0) -- (1,1);
   \begin{pgfonlayer}{foreground}
      \draw [red] (0,1) -- (1,0);
   \end{pgfonlayer}
   \draw (0,0) -- (1,0.5);
\end{tikzpicture}
\quad
5 \begin{tikzpicture}
   \draw (0,0) -- (1,1);
   \begin{pgfonlayer}{foreground}
      \begin{scope}
         \draw [red] (0,1) -- (1,0);
      \end{scope}
   \end{pgfonlayer}
   \draw (0,0) -- (1,0.5);
\end{tikzpicture}
\quad
6 \begin{tikzpicture}
   \draw (0,0) -- (1,1);
   \begin{scope}
      \begin{pgfonlayer}{foreground}
         \begin{scope}
            \draw [red] (0,1) -- (1,0);
         \end{scope}
      \end{pgfonlayer}
   \end{scope}
   \draw (0,0) -- (1,0.5);
\end{tikzpicture}
\end{document}
3

Based on the on background layer of tikzlibrarybackgrounds.code.tex, you could build your own style that does not reset the \tikz@options, such that after the scope you'll be fine. This still requires you to smuggle medium into the scope with every scope/.style={medium}. But you could drop every on background layer/.style={every picture},.

\documentclass{article}

\usepackage{tikz}

\usetikzlibrary{%
   backgrounds,%
}
\makeatletter
\tikzset{
on layer/.style={
    execute at begin scope={%
      \pgfonlayer{#1}%
      \tikz@options
      },
    execute at end scope={\endpgfonlayer}
  }
}%
\makeatother

\tikzset{
   medium/.style={line width=2.5pt},
   thin/.style={line width=0.5pt},
   every picture/.append style={medium},
   every scope/.append style={medium},
%    every on background layer/.style={every picture},
}

\pgfdeclarelayer{background}
\pgfdeclarelayer{foreground}
\pgfsetlayers{background,main,foreground}

\begin{document}
1 \begin{tikzpicture}
   \draw (0,0) -- (1,1);
   \begin{scope}[on background layer]
      \draw [red] (0,1) -- (1,0);
   \end{scope}
   \draw (0,0) -- (1,0.5);
\end{tikzpicture}
\quad
2 \begin{tikzpicture}
   \draw (0,0) -- (1,1);
   \begin{scope}[on layer=background]
      \draw [red] (0,1) -- (1,0);
   \end{scope}
   \draw (0,0) -- (1,0.5);
\end{tikzpicture}
\end{document}

enter image description here

  • Thanks for your solution. Do you think I should report the behaviour as a bug? – Tobi Jan 19 at 12:08
  • 1
    @Tobi I don't know. I guess, if at all I would call it a feature request. (I personally would be very interested in something that would allow one to carry the \tikz@options somewhere and to switch them off locally, which would mitigate the problems associated with nesting TikZ pictures.) – user121799 Jan 19 at 15:08
  • 1
    @marmot In 3.1.3 I introduced \pgfutil@pushmacro and \pgfutil@popmacro which allows you to carry definitions over an \egroup. – Henri Menke May 17 at 0:14
  • @HenriMenke Good to know, thanks! I wonder if you want to add an answer to this question. I am also wondering how this differs from \pgfmathsmuggle that has been added in 3.1.1. Could you perhaps add this explanation, too? – user121799 May 17 at 0:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.