在 {pgfonlayer} 之后恢复 TikZ 设置

在 {pgfonlayer} 之后恢复 TikZ 设置

这是一个后续问题TikZ 中存在错误?图层上的线宽


使用时{pgfonlayer}会出现两个问题:a)背景范围设置内部every picture缺失。b)绘图命令关闭{pgfonlayer}处于活动状态的设置 {pgfonlayer}丢失了。虽然 a) 似乎是“设计使然”,但我不确定 b)。

下面的代码生成此图像: 结果 1) 是按照文档制作的on baclground layer并且按文档说明工作。2
) 是错误的
3) 是 2) 的修复版本;4) 和 5) 是错误的;
6) 又是修复版本。

如您所见,需要两个额外的{scope}s 才能获得所需的结果。虽然克里斯蒂安建议上述问题只有一个解决方案。这个似乎不再起作用了?

所以基本上的问题是其中哪一个可能是错误并且值得报告?

代码

\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}

答案1

基于on background layertikzlibrarybackgrounds.code.tex您可以构建自己的样式,不重置\tikz@options,这样在范围之后您就没问题了。这仍然需要您使用 偷偷medium带入范围every scope/.style={medium}。但您可以删除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}

在此处输入图片描述

相关内容