Bug in TikZ? Line width on layers
This seems to be bug in pgf: path styles like \pgfsetlinewidth
seem to be forgotten when the layers are set. I have no idea why this happens. Here is a minimal example directly in pgf:
\documentclass{article}
\usepackage{pgf}
\pgfdeclarelayer{background}
\pgfsetlayers{background,main}
\begin{document}
\begin{pgfpicture}
\pgfsetlinewidth{5pt}
\pgfpathmoveto{\pgfpointorigin}
\pgfpathlineto{\pgfpoint{5cm}{0cm}}
\pgfusepath{draw}
\begin{pgfonlayer}{background}
\pgfpathmoveto{\pgfpoint{0cm}{1cm}}
\pgfpathlineto{\pgfpoint{5cm}{1cm}}
\pgfusepath{draw}
\end{pgfonlayer}
\end{pgfpicture}
\end{document}
Using TikZ, you can add \makeatletter\tikz@options\makeatother
after \begin{pgfonlayer}{background}
. This should get you the options set in the last scope
(or tikzpicture
), but it won't work correctly if you have nested scopes
(i.e. only the options from the innermost scope are used).
As noted in a comment, I believe that the PGF responsible will see your request as a feature request (not a bug report). As such, it might take longer for results. But perhaps the PGF responsible will see that it is quite unexpected that options set for the complete picture are not part of the layer, he may want to implement a fix for that particular part, I do not know. Many words just to state: I would like a better solution as well. But be prepared to wait (perhaps very long).
In the meantime, it might help to understand how the given layering architecture works in order to avoid such problems.
Here is what I understand of it. It consists of the interaction between
- layers
- option changes (especially styles)
- graphics settings
Usually, you can change options as you want and at any time. If you change styles, the styles will be evaluated on usage (for example every node
will be evaluated during \node
). But options which affect the graphics state (like color
, linewidth
and similar ones) are different: they are applied by scopes (at least I think so) and by \path
s (including \draw
, of course).
Whenever you switch the layer, pgf starts to collect low level drawing instructions into a different box. All low level drawing instructions which have been collected in a different layer will NOT be copied into the new layer.
This is pretty clear. But since "low level drawing instruction" includes things like \pgfsetlinewidth
as well as color assignments, such settings are not copied to the activated layer as well.
After switching to the new layer, PGF will continue its normal processing. In particular, it will continue to evaluate styles, and these styles still have the same value as they had before switching layers.
I suppose this is also not new for you. But it means that you can (and perhaps should) define (custom) styles to share options between layers. Your approach
with the every picture
style actually uses the approach, but it can be applied in general.
For example,
\begin{tikzpicture}
\tikzset{my style/.style={red,line width=10pt}}
\begin{scope}[my style]
...
\end{scope}
\pgfonlayer{...}
\begin{scope}[my style]
...
\end{scope}
\endpgfonlayer}
\end{tikzpicture}
will do the job. It is not as simple as one would like (or expect), I agree. But this is how it has been designed until now; everything else will have to wait (until someone implements an improved version).
In other words: your approach with the every picture
is correct and perhaps the only way to do it right now.
Concerning the "reset": as explained, a layer does not introduce a completely new picture. It inherits all current styles and all current options (except for those graphics state options which have already been written to its layer). Thus, "resetting" anything is not an option.