Context: This question is a follow-up of Best practice for creating TikZ pictures with nested elements. Symbol 1 provided an answer to this question, where \scopenode is defined.
\scopenode are scopes turned into nodes, i.e. one can: name the scope by name=foo; position the scope by at=(somewhere); and tune the position by anchor=something. They are basically awesome, since they can be nested.
Then, in How to make the use of tikzexternalize and saveboxes compatible?, cfr provided an answer improving these \scopenodes by enabling the display of both \scopenode's background and the content of the \scopenode. (Scopenode background would indeed be drawn above the content otherwise.)
Problem: I tried to include \scopenode in a TikZ matrix. However, I have some issues:
with Symbol 1's solution,
\scopenodes are well positioned, but their content do not appear because it's hidden behind the fill color.

with cfr's solution, content is shown (and well positioned), but
\scopenodes get messed up.

Question: How to make \scopenode compatible with TikZ matrix?
MWEs
(The example creates a matrix with one row and two columns. In both cells (A1 and B1), a scopenope is filled-drawn. A (red-orange) is south anchored, and B (yellow-green) is north anchored. In each scopenode, a path is drawn from (0,0) to (1,1)).
_______ | A | | |---|---| <-- baseline |___|_B_| With Symbol 1's solution:
\documentclass{article} \usepackage{tikz} \usetikzlibrary{matrix} \usetikzlibrary{backgrounds} % \usetikzlibrary{external} % \tikzexternalize % \tikzset{external/prefix=build/} \makeatletter \newbox\tikz@sand@box \newcount\tikz@scope@depth \tikz@scope@depth111\relax \def\scopenode[#1]#2{% \begin{pgfinterruptboundingbox}% \advance\tikz@scope@depth111\relax% % process the user option \begin{scope}[name=tempscopenodename,at={(0,0)},anchor=center,#1]% % try to extract positioning information: name, at, anchor \global\let\tikz@fig@name\tikz@fig@name% \global\let\tikz@node@at\tikz@node@at% \global\let\tikz@anchor\tikz@anchor% \end{scope}% \let\tikz@scopenode@name\tikz@fig@name% \let\tikz@scopenode@at\tikz@node@at% \let\tikz@scopenode@anchor\tikz@anchor% % try to typeset this scope % we only need bounding box information % the box itself will be discard \setbox\tikz@sand@box=\hbox{% \begin{scope}[local bounding box=tikz@sand@box\the\tikz@scope@depth,#1]% #2% \end{scope}% }% % goodbye. haha \setbox\tikz@sand@box=\hbox{}% % now typeset again \begin{scope}[local bounding box=\tikz@scopenode@name]% % use the bounding box information to reposition the scope \pgftransformshift{\pgfpointanchor{tikz@sand@box\the\tikz@scope@depth}{\tikz@scopenode@anchor}% \pgf@x-\pgf@x\pgf@y-\pgf@y}% \pgftransformshift{\tikz@scopenode@at}% \begin{scope}[#1]% #2 \end{scope}% \end{scope}% \pgfkeys{/pgf/freeze local bounding box=\tikz@scopenode@name}% \global\let\tikz@scopenode@name@smuggle\tikz@scopenode@name% \end{pgfinterruptboundingbox}% % make up the bounding box \path(\tikz@scopenode@[email protected] west)(\tikz@scopenode@[email protected] east);% % draw something, not necessary \draw[#1](\tikz@scopenode@[email protected] west)rectangle(\tikz@scopenode@[email protected] east);% } \makeatother \begin{document} \begin{tikzpicture}[ remember picture, inner sep=0pt, outer sep=0pt, ] \draw [help lines](-2,-2) grid (2,2); \matrix[ column sep=2em, row sep = 1em, nodes in empty cells, anchor=center, nodes={anchor=center}, ] { \scopenode[draw = red, fill = orange, anchor=south] { \draw [blue] (0,0) -- (1,1); }; & \scopenode[draw = yellow, fill = green, anchor=north] { \draw [black] (0,1) -- (1,0); }; \\ }; \end{tikzpicture} \end{document} With cfr's solution:
\documentclass{article} \usepackage{tikz} \usetikzlibrary{matrix} \usetikzlibrary{backgrounds} % \usetikzlibrary{external} % \tikzexternalize % \tikzset{external/prefix=build/} \makeatletter \pgfdeclarelayer{scopenode} \pgfsetlayers{background,scopenode,main} \tikzset{% % adapted from tex/generic/pgf/frontendlayer/tikz/libraries/tikzlibrarybackgrounds.code.tex on scopenode layer/.style={% execute at begin scope={% \pgfonlayer{scopenode}% \let\tikz@options=\pgfutil@empty% \tikzset{every on scopenode layer/.try,#1}% \tikz@options% }, execute at end scope={\endpgfonlayer} }, } % ateb Symbol 1: tex.stackexchange.com/a/… \newbox\tikz@sand@box \newcount\tikz@scope@depth \tikz@scope@depth111\relax \def\scopenode[#1]#2{% name=<enw>, at=<man>, anchor=<angor> \begin{pgfinterruptboundingbox}% \advance\tikz@scope@depth111\relax% % process the user option \begin{scope}[name=tempscopenodename,at={(0,0)},anchor=center,#1]% % try to extract positioning information: name, at, anchor \global\let\tikz@fig@name\tikz@fig@name% \global\let\tikz@node@at\tikz@node@at% \global\let\tikz@anchor\tikz@anchor% \end{scope}% \let\tikz@scopenode@name\tikz@fig@name% \let\tikz@scopenode@at\tikz@node@at% \let\tikz@scopenode@anchor\tikz@anchor% % try to typeset this scope % we only need bounding box information % the box itself will be discard \setbox\tikz@sand@box=\hbox{% \begin{scope}[local bounding box=tikz@sand@box\the\tikz@scope@depth,#1]% #2% \end{scope}% }% % goodbye. haha \setbox\tikz@sand@box=\hbox{}% % now typeset again \begin{scope}[local bounding box=\tikz@scopenode@name]% % use the bounding box information to reposition the scope \pgftransformshift{\pgfpointanchor{tikz@sand@box\the\tikz@scope@depth}{\tikz@scopenode@anchor}% \pgf@x-\pgf@x\pgf@y-\pgf@y}% \pgftransformshift{\tikz@scopenode@at}% \begin{scope}[#1]% #2 \end{scope}% \end{scope}% \pgfkeys{/pgf/freeze local bounding box=\tikz@scopenode@name}% \global\let\tikz@scopenode@name@smuggle\tikz@scopenode@name% \end{pgfinterruptboundingbox}% % make up the bounding box \path(\tikz@scopenode@[email protected] west)(\tikz@scopenode@[email protected] east);% % draw something, not necessary \begin{scope}[on scopenode layer]% \draw[#1](\tikz@scopenode@[email protected] west)rectangle(\tikz@scopenode@[email protected] east);% \end{scope}% } \makeatother \begin{document} \begin{tikzpicture}[ remember picture, inner sep=0pt, outer sep=0pt, ] \draw [help lines](-2,-2) grid (2,2); \matrix[ column sep=2em, row sep = 1em, nodes in empty cells, anchor=center, nodes={anchor=center}, ] { \scopenode[draw = red, fill = orange, anchor=south] { \draw [blue] (0,0) -- (1,1); }; & \scopenode[draw = yellow, fill = green, anchor=north] { \draw [black] (0,1) -- (1,0); }; \\ }; \end{tikzpicture} \end{document} 