Tikz 从上到下绘制图层堆栈(反向顺序)

Tikz 从上到下绘制图层堆栈(反向顺序)

在 Tikz 中,我想要一个命令

\StartDrawOnBottomOfLayerStack

绘制以下所有元素底层的底部图片的。这意味着以下节点和路径出现在图片的所有内容后面。我需要一个命令来返回标准行为 \StartDrawOnTopOfLayerStack

目前我必须定义与要绘制的背景节点数量相同的图层。我想知道是否可以更轻松地实现?

\documentclass[tikz]{standalone}
\usepackage{tikz}
\usetikzlibrary{fit}
\begin{document}

\pgfdeclarelayer{background3}
\pgfdeclarelayer{background2}
\pgfdeclarelayer{background1}
\pgfsetlayers{background3,background2,background1,main}

\begin{tikzpicture}

%% block diagram
\node[rectangle,draw,fill=yellow] (A) at (-4,0) {A};
\node[rectangle,draw,fill=yellow] (B) at (-3,0) {B};
\node[rectangle,draw,fill=yellow] (C) at (-2,0) {C};
\node[rectangle,draw,fill=yellow] (D) at (-1,0) {D};

% \StartDrawOnBottomOfLayerStack

%% group 1
\begin{pgfonlayer}{background1}
\node[rectangle,fill=green,fit={(B) (C)}](G1) {}; 
\end{pgfonlayer}

%% group 2
\begin{pgfonlayer}{background2}
\node[fill=blue,fit={(B) (C) (D)(G1)}](G2) {}; 
\end{pgfonlayer}

%% group 3
\begin{pgfonlayer}{background3}
\node[fill=red,fit={(A)(B) (C) (D) (G1) (G2)}](G3) {}; 
\end{pgfonlayer}

\end{tikzpicture}
\end{document}

平均能量损失

答案1

简单介绍一下,从下到上:

  1. 与 environment 类似,定义pgfonlayer新的环境,其内容会排版在指定的图层上,但顺序相反,即排版最新的内容pgfonlayerreversed以下累积的内容。
  2. on background layer与库中的选项类似backgrounds,定义了新选项on background layer reversed,利用了新的环境pgfonlayerreversed(在层上background)。
  3. 最后,\StartDrawOnBottomOfLayerStack\EndDrawOnBottomOfLayerStack构成一个特殊scope环境,其中每次使用\node都等同于\scoped[on lowest layer] \node

全面实施:

% from https://tex.stackexchange.com/q/562577
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{backgrounds, fit}
\usepackage{xpatch}

\makeatletter
% similar to env "pgfonlayer", but the latest contents are typeset on
% lowest bottom (on reversed order)

\let\pgfonlayerreversed\pgfonlayer
\let\endpgfonlayerreversed\endpgfonlayer

\xpatchcmd\pgfonlayerreversed
  {\expandafter\box\csname pgf@layerbox@#1\endcsname\begingroup}
  {\begingroup}
  {}{\fail}

\xpatchcmd\endpgfonlayerreversed
  {\endgroup}
  {\endgroup\expandafter\box\csname pgf@layerbox@\pgfonlayer@name\endcsname}
  {}{\fail}


\tikzset{
  on background layer reversed/.style={%
    execute at begin scope={%
      \pgfonlayerreversed{background}%
      \let\tikz@options=\pgfutil@empty
      \tikzset{every on background layer/.try,#1}%
      \tikz@options
    },
    execute at end scope={\endpgfonlayerreversed}
  }
}


\def\StartDrawOnBottomOfLayerStack{%
  \scope\relax
  % patch \path variants to auto insert "\scoped[on lowest layer]"
  % currently \node, \pic, \coordinate, and \matrix are patched
  \let\tikz@path@overlay\tikz@path@overlay@autoscoped
  \let\tikz@path@overlayed\tikz@path@overlayed@autoscoped
}

\def\EndDrawOnTopOfLayerStack{%
  \endscope
}

\def\tikz@path@overlay@autoscoped#1{%
  \let\tikz@signal@path=\tikz@signal@path% for detection at begin of matrix cell
  \pgfutil@ifnextchar<%
    {\tikz@path@overlayed{#1}}
    {\scoped[on background layer reversed] \path #1}}%
\def\tikz@path@overlayed@autoscoped#1<#2>{%
  \scoped[on background layer reversed] \path<#2> #1}%
\makeatother

\begin{document}

\begin{tikzpicture}
  % text nodes
  \node[rectangle,draw,fill=yellow] (A) at (-4,0) {A};
  \node[rectangle,draw,fill=yellow] (B) at (-3,0) {B};
  \node[rectangle,draw,fill=yellow] (C) at (-2,0) {C};
  \node[rectangle,draw,fill=yellow] (D) at (-1,0) {D};

  % background rectangles
  \StartDrawOnBottomOfLayerStack
    \node[rectangle,fill=green,fit={(B) (C)}](G1) {};
    \node[fill=blue,fit={(B) (C) (D)(G1)}](G2) {};
    \node[fill=red,fit={(A)(B) (C) (D) (G1) (G2)}](G3) {};
  \EndDrawOnTopOfLayerStack
\end{tikzpicture}
\end{document}

相关内容