Contexto:Esta pergunta é uma continuação dePrática recomendada para criar imagens TikZ com elementos aninhados.Símbolo 1oferecidouma respostapara esta questão, onde \scopenode
está definido.
\scopenode
são escopos transformados em nós, ou seja, pode-se: nomear o escopo por name=foo
; posicionar o escopo por at=(somewhere)
; e ajuste a posição por anchor=something
. Eles são basicamente incríveis, pois podem ser aninhados.
Então, emComo compatibilizar o uso de tikzexternalize e saveboxes?,cfroferecidouma respostamelhorando estes \scopenode
, permitindo a exibição do \scopenode
plano de fundo e do conteúdo do arquivo \scopenode
. (O fundo do escoponodo seria de fato desenhadoacimao conteúdo de outra forma.)
Problema:Tentei incluir \scopenode
em um TikZ. matrix
No entanto, tenho alguns problemas:
com a solução do Símbolo 1,
\scopenode
os s estão bem posicionados, mas seu conteúdo não aparece porque está oculto atrás da cor de preenchimento.
com a solução do cfr, o conteúdo é mostrado (e bem posicionado), mas
\scopenode
fica confuso.
Pergunta:Como tornar \scopenode
compatível com TikZ matrix
?
MWEs
(O exemplo cria uma matriz comumlinha e duas colunas. Em ambas as células (A1 e B1), um escoponope é preenchido e desenhado. A (vermelho-laranja) está ancorado ao sul e B (amarelo-verde) está ancorado ao norte. Em cada scopenode, um caminho é traçado de (0,0) a (1,1)).
_______
| A | |
|---|---| <-- baseline
|___|_B_|
Com a solução do Símbolo 1:
\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}
Com a solução do cfr:
\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}
Responder1
Este é de longe o melhor que posso conseguir:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\usetikzlibrary{backgrounds}
% \usetikzlibrary{external}
% \tikzexternalize
% \tikzset{external/prefix=build/}
\makeatletter
\newbox\tikz@sand@box
\newcount\tikz@scope@depth
\newdimen\tikz@scope@shiftx
\newdimen\tikz@scope@shifty
\newdimen\tikz@scope@swx
\newdimen\tikz@scope@swy
\newdimen\tikz@scope@nex
\newdimen\tikz@scope@ney
\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
\pgfpointanchor{tikz@sand@box\the\tikz@scope@depth}{\tikz@scopenode@anchor}%
\tikz@scope@shiftx-\pgf@x%
\tikz@scope@shifty-\pgf@y%
\tikz@scopenode@at%
\advance\tikz@scope@shiftx\pgf@x%
\advance\tikz@scope@shifty\pgf@y%
\pgftransformshift{\pgfpoint{\tikz@scope@shiftx}{\tikz@scope@shifty}}
% the background path
% lengthy, tedious calculation
% someone please improve this
\pgfpointanchor{tikz@sand@box\the\tikz@scope@depth}{south west}
\tikz@scope@swx\pgf@x\advance\tikz@scope@swx\tikz@scope@shiftx
\tikz@scope@swy\pgf@y\advance\tikz@scope@swy\tikz@scope@shifty
\pgfpointanchor{tikz@sand@box\the\tikz@scope@depth}{north east}
\tikz@scope@nex\pgf@x\advance\tikz@scope@nex\tikz@scope@shiftx
\tikz@scope@ney\pgf@y\advance\tikz@scope@ney\tikz@scope@shifty
\path(\tikz@scope@swx,\tikz@scope@swy)coordinate(tempsw)
(\tikz@scope@nex,\tikz@scope@ney)coordinate(tempne);
\path[#1](tempsw)rectangle(tempne);
% typeset the content for real
\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);%
% compatible code for matrix
\expandafter\pgf@nodecallback\expandafter{\tikz@scopenode@name@smuggle}%
}
\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,name=aaa,anchor=south] {
\draw[blue](0,0)--(1,1)circle(.2);
};
&
\scopenode[draw=yellow,fill=green,name=bbb,anchor=north] {
\draw[black](0,1)--(1,0)circle(.1);
};
&
\scopenode[fill=cyan,name=ccc,anchor=east,scale=.8] {
\draw(0,0)--(1,1)circle(.3)--(2,0);
};
\\
\node(aaaa){};
&
\node(bbbb){};
&
\node(cccc){};
\\
};
\draw[->](2,2)node[above]{this is the orange scopenode}to[bend left](aaa.east);
\draw[->](-2,-2)node[below]{this is the green scopenode}to[bend left](bbb.west);
\draw[->](3,-1)node[right]{this is the cyan scopenode}to[bend left](ccc.south);
\end{tikzpicture}
\end{document}
Informações de importação
Querido futuro eu:
Para sua informação, o conteúdo da matriz é composto apenas uma vez em um hbox. E então eles são movidos para a célula correspondente. E então toda a matriz é movida para a posição desejada. O primeiro movimento é feito por \pgf@matrix@shift@nodes@initial
e o segundo por \pgf@matrix@shift@nodes@secondary
. Eles estão apenas se aplicando \pgf@shift@node
a uma lista de nós. Para registrar o scopenode, você adicionou a linha
\expandafter\pgf@nodecallback\expandafter{\tikz@scopenode@name@smuggle}%
então o scopenode também é movido.
Atualmente tudo no scopenode será digitado duas vezes. Para o scopenode aninhado, as coisas são compostas com 2 tempos de profundidade . Isso é realmente frustrante. A propósito, talvez alguém possa melhorar isso, TikZ lida com matriz.
(No entanto, a matriz não pode ser aninhada. Você ganha!)
Além disso, você mudou
\global\let\tikz@fig@name\tikz@fig@name
para
\global\let\tikz@fig@name@\tikz@fig@name
para que o nome do scopenode não possa ser acessado em outro lugar.
Em particular, TikZ será aplicado \pgf@shift@node
à própria matriz. Se a matriz não tiver nome, o último scopenode será deslocado, o que é indesejado. Você gastou duas horas apenas para encontrar esse bug estúpido. APRENDA A LIÇÃO.
Além disso, você codificou o backgroundpath do scopenode para que agora seja preenchido/desenhado antes do conteúdo do escopo. (Daí o nomefundocaminho) Mas o cálculo é demorado e aparentemente redundante. Espero que alguém possa melhorá-lo.
No entanto, você evitou usar pgfonlayer
. Isso é ótimo.