Einfuhrinformationen

Einfuhrinformationen

Kontext:Diese Frage ist eine Folgefrage vonBest Practice zum Erstellen von TikZ-Bildern mit verschachtelten Elementen.Symbol 1bereitgestellteine Antwortauf diese Frage, wo \scopenodedefiniert ist.

\scopenodesind Bereiche, die in Knoten umgewandelt werden, d. h. man kann den Bereich durch benennen name=foo, den Bereich durch positionieren at=(somewhere)und die Position durch anpassen anchor=something. Sie sind grundsätzlich großartig, da sie verschachtelt werden können.

Dann inWie kann die Verwendung von Tikzexternalize und Saveboxes kompatibel gemacht werden?,cfrbereitgestellteine AntwortVerbesserung dieser \scopenodes durch die Aktivierung der Anzeige sowohl \scopenodedes Hintergrunds von als auch des Inhalts von \scopenode. (Der Scopenode-Hintergrund würde tatsächlich gezeichnetüberder Inhalt andernfalls.)

Problem:Ich habe versucht, \scopenodein einem TikZ. matrixIch habe jedoch einige Probleme:

  • Mit der Lösung von Symbol 1 \scopenodesind die s zwar gut positioniert, ihr Inhalt wird jedoch nicht angezeigt, da er hinter der Füllfarbe verborgen ist.
    Bildbeschreibung hier eingeben

  • Mit der Lösung von cfr wird der Inhalt angezeigt (und gut positioniert), aber \scopenodedie Zeichen werden durcheinandergebracht.
    Bildbeschreibung hier eingeben

Frage:So machen Sie es \scopenodemit Ti kompatibelkZ matrix?


MWEs
(Das Beispiel erzeugt eine Matrix miteinsZeile und zwei Spalten. In beiden Zellen (A1 und B1) ist ein Scopnope ausgefüllt. A (rot-orange) ist im Süden verankert und B (gelb-grün) ist im Norden verankert. In jedem Scopnope ist ein Pfad von (0,0) nach (1,1) gezeichnet.

 _______
| A |   |
|---|---|  <-- baseline
|___|_B_|

Mit der Lösung von Symbol 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}

Mit der Lösung von 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}

Antwort1

Das ist bei weitem das Beste, was ich bekommen kann:

\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}

Einfuhrinformationen

Liebes zukünftiges Ich:

Zu Ihrer Information: Der Inhalt der Matrix wird nur einmal in einer Hbox gesetzt. Und dann werden sie in die entsprechende Zelle verschoben. Und dann wird die gesamte Matrix an die gewünschte Position verschoben. Die erste Verschiebung erfolgt durch \pgf@matrix@shift@nodes@initialund die zweite durch \pgf@matrix@shift@nodes@secondary. Sie werden lediglich \pgf@shift@nodeauf eine Knotenliste angewendet. Um den Scope-Knoten zu registrieren, haben Sie die Zeile hinzugefügt

\expandafter\pgf@nodecallback\expandafter{\tikz@scopenode@name@smuggle}%

daher wird auch der Scopnode verschoben.


Derzeit wird alles im ScopeNode zweimal gesetzt. Bei verschachtelten ScopeNodes werden die Dinge zweimal tief gesetzt . Das ist wirklich frustrierend. Vielleicht kann das übrigens jemand verbessern, TikZ befasst sich mit der Matrix.

(Matrizen können jedoch nicht verschachtelt werden. Sie gewinnen!)



Außerdem hast du dich geändert

\global\let\tikz@fig@name\tikz@fig@name

Zu

\global\let\tikz@fig@name@\tikz@fig@name

sodass auf den Namen des Scopnodes nicht anderswo zugegriffen werden kann.

Insbesondere TikZ wird \pgf@shift@nodeauf die Matrix selbst angewendet. Wenn die Matrix unbenannt ist, wird der letzte Scope-Knoten verschoben, was unerwünscht ist. Sie haben zwei Stunden damit verbracht, nur diesen dummen Fehler zu finden. LERNEN SIE DARAUS.



Außerdem haben Sie den Hintergrundpfad des Scope-Knotens fest codiert, sodass er jetzt vor dem Inhalt des Scope ausgefüllt/gezeichnet wird. (Daher der NameHintergrundPfad) Aber die Berechnung ist langwierig und scheinbar redundant. Ich hoffe, jemand kann sie verbessern.

Trotzdem haben Sie die Verwendung vermieden pgfonlayer. Das ist großartig.

verwandte Informationen