![Tikz dibuja en la pila de capas de arriba a abajo (orden inverso)](https://rvso.com/image/420763/Tikz%20dibuja%20en%20la%20pila%20de%20capas%20de%20arriba%20a%20abajo%20(orden%20inverso).png)
En Tikz quisiera tener un comando
\StartDrawOnBottomOfLayerStack
para dibujar todos los elementos siguientes enparte inferior de la capa inferiorde la imagen. Eso significa que los siguientes nodos y caminos aparecen detrás de todo lo que aparece en la imagen. Necesito un comando para volver al comportamiento estándar
\StartDrawOnTopOfLayerStack
Por el momento tengo que definir tantas capas como nodos de fondo haya para dibujar. Me pregunto si se puede realizar más fácilmente.
\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}
Respuesta1
Breve introducción, de abajo hacia arriba:
- Similar al entorno
pgfonlayer
,pgfonlayerreversed
se define un nuevo entorno. Su contenido se compondrá en la capa especificada pero en orden inverso, es decir, se compondrá el contenido más reciente.abajocontenidos acumulados. - De manera similar a la opción
on background layer
en la biblioteca , se definebackgrounds
una nueva opción que hace uso del nuevo entorno (en la capa ).on background layer reversed
pgfonlayerreversed
background
- Finalmente,
\StartDrawOnBottomOfLayerStack
y\EndDrawOnBottomOfLayerStack
constituyen unscope
entorno especial, en el que cada uso de\node
equivale a\scoped[on lowest layer] \node
.
Plena aplicación:
% 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}