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}

MWE

답변1

아래에서 위로 짧은 소개:

  1. Environment 와 유사하게 pgfonlayer새로운 환경이 pgfonlayerreversed정의됩니다. 그 내용은 지정된 레이어에 조판되지만 역순으로 조판됩니다. 즉, 최신 내용이 조판됩니다.아래에축적된 콘텐츠.
  2. on background layer라이브러리의 옵션과 유사하게 새 환경 (레이어에서 ) 을 사용하는 backgrounds새 옵션이 정의됩니다 .on background layer reversedpgfonlayerreversedbackground
  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}

관련 정보