节点、范围和 pgfonlayer

节点、范围和 pgfonlayer

请问我可以在 pgfonlayer 中使用节点吗?有问题的节点在以下内容中被注释掉:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{scopes}
\usetikzlibrary{backgrounds}
\begin{document}
\begin{tikzpicture}
[
  my line/.style={line width=2.5pt},
  put circle/.style={circle,draw=blue!50,fill=blue!20,thick,
    inner sep=0pt,minimum size=5mm},
  execute at end picture={
    \begin{pgfonlayer}{background}
      \path[fill=yellow!25,rounded corners,my line,draw=red]
        (current bounding box.south west) rectangle
        (current bounding box.north east);
      %\node at (2,2) [my line] {
      {
        [magenta]
        \draw (0mm,10mm) -- (15mm,10mm);
        \draw (0mm,8mm) -- (15mm,8mm);
      }
        \draw (0mm,6mm) -- (15mm,6mm);
      {
        [green]
        \draw (0mm,4mm) -- (15mm,4mm);
        \draw (0mm,2mm) -- (15mm,2mm);
        \draw[blue] (0mm,0mm) -- (15mm,0mm);
      };
    %};
    \end{pgfonlayer}
  }
]
\node at (0,0) [put circle] {X};
\node at (4,1) [put circle] {Y};
\end{tikzpicture}
\end{document}

答案1

你可以这样做

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{scopes}
\usetikzlibrary{backgrounds}
\begin{document}
\begin{tikzpicture}
[
  my line/.style={line width=2.5pt},
  put circle/.style={circle,draw=blue!50,fill=blue!20,thick,
    inner sep=0pt,minimum size=5mm},
  execute at end picture={
    \begin{pgfonlayer}{background}
      \path[fill=yellow!25,rounded corners,my line,draw=red]
        (current bounding box.south west) rectangle
        (current bounding box.north east);
      % you can draw the next lines with yshift (or with the calc libraty)
      % and (current bounding box.center)
      % without the node !
      \node at (current bounding box.center) {\tikz  [my line]
        {\draw [magenta]
        (0mm,10mm) -- (15mm,10mm)
        (0mm,8mm) -- (15mm,8mm);
        \draw[black] (0mm,6mm) -- (15mm,6mm);
        \draw[green]  (0mm,4mm) -- (15mm,4mm)
                      (0mm,2mm) -- (15mm,2mm);
        \draw[blue] (0mm,0mm) -- (15mm,0mm);}
      }; 
    \end{pgfonlayer}
  }
]
\node at (0,0) [put circle] {X};
\node at (4,1) [put circle] {Y};
\end{tikzpicture}
\end{document} 

在此处输入图片描述

可以在另一个 tikz 环境中使用 tikz 环境,但您需要小心谨慎,并且最好在可能的情况下避免这种情况。

没有实习生 tikz 环境

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc,scopes}
\usetikzlibrary{backgrounds}
\begin{document}
\begin{tikzpicture}
[
  my line/.style={line width=2.5pt},
  put circle/.style={circle,draw=blue!50,fill=blue!20,thick,
    inner sep=0pt,minimum size=5mm},
  execute at end picture={
    \begin{pgfonlayer}{background}
      \path[fill=yellow!25,rounded corners,my line,draw=red]
        (current bounding box.south west) rectangle
        (current bounding box.north east);
     {[my line]
            \draw [magenta]
            ([shift={(-7.5mm,0mm)}]current bounding box.center) --++ (15mm,0mm);  
     }
    \end{pgfonlayer}
  }
]
\node at (0,0) [put circle] {X};
\node at (4,1) [put circle] {Y};
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容