我正在使用 TikZ 和库 graphs 以及 graphdrawing 以及网格布局来生成图,其中节点由库自动放置。除了一件事之外,一切都运行良好。因为标准节点类型(矩形、菱形、椭圆形……)在一个特殊但常见的情况下不符合我的需求,所以我需要创建一个自定义节点 - 而不是 TikZ 节点。
所以我尝试将 tikzpicture 放在节点内并创建所需的形状。在图形外部,这正如预期的那样工作,但在图形内部则完全不行。
原因似乎是从 tikzpicture/scope 继承的选项。为了解决这个问题,我尝试找到一种方法
- 试图阻止继承。但没有找到办法。
- 回到节点内新的 tikzpicture 里面的“默认”布局。但没有找到要覆盖继承的布局的默认布局名称。
- 为该节点使用自己的 pgf 层。
- 节点内的矩阵(其中每个元素被识别为一个节点,因此被视为图节点......)
- 我不再记得的事情……
以上方法都不起作用。所以我的问题是:如何让嵌入节点作为 TikZ 图表库的一个节点出现?或者,作为替代方案,还有其他方法可以解决这个问题吗(创建具有可变内容的自己的节点形状)?
\RequirePackage{luatex85}
\documentclass[tikz,border=1pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes}
\usetikzlibrary{arrows,arrows.meta}
\usetikzlibrary{graphs,graphdrawing}
\usetikzlibrary{fit}
\usetikzlibrary{positioning}
\usegdlibrary{layered}
\newcommand{\ToNode}{edge[->, looseness=0.5, out=east, in=west]}
\tikzset{NodeStyle1/.style = {draw, anchor=east, rectangle, fill=black!10, text height=2ex, text depth=0.25ex, minimum height=3ex}}
\tikzset{NodeStyle3a/.style = { anchor=east, rectangle, text height=2ex, text depth=0.25ex, minimum height=3ex}}
\tikzset{NodeStyle3b/.style = {draw, anchor=east, rectangle, fill=yellow!20, minimum width=5em, text centered, inner sep=0, rounded corners}}
\tikzset{NodeStyleRaw/.style = { }}
\tikzset{PicureOptions/.style = {%
layered layout,
level sep=1.5cm,
level distance=0.0cm,
sibling sep=1.5em,
sibling distance=1.0cm,
grow'=right,
components go down left aligned
}}
\pgfdeclarelayer{sub1}
\pgfdeclarelayer{sub2}
\pgfsetlayers{sub2,sub1,main}
\newcommand{\NodeInputNumName}[2]{%
\begin{tikzpicture}
\begin{pgfonlayer}{main}
\node[NodeStyle3a] (number) {No. #1};
\node[NodeStyle3a, right = 0mm of number] (name) {#2};
\draw (number.north east) -- (number.south east);
\end{pgfonlayer}
\begin{pgfonlayer}{sub1}
\node[NodeStyle3b] (frame) [fit = (number) (name)] {};
\end{pgfonlayer}
\end{tikzpicture}
}
\begin{document}
\begin{tikzpicture}[]
\begin{scope}[PicureOptions]
\node[NodeStyle1] (A) {Graph-Node A};
\node[NodeStyle1] (B) {Graph-Node B};
\node[NodeStyle1] (C) {Graph-Node C};
\node[NodeStyleRaw] (D) {
\begin{tikzpicture}
\begin{pgfonlayer}{main}
\node[NodeStyle3a] (number) {No. X};
\node[NodeStyle3a, right = 0mm of number] (name) {Graph-Node D, inside Graph};
\draw (number.north east) -- (number.south east);
\end{pgfonlayer}
\begin{pgfonlayer}{sub1}
\node[NodeStyle3b] (frame) [fit = (number) (name)] {};
\end{pgfonlayer}
\end{tikzpicture}
};
\node (ABCD) {Result ABCD};
\draw (A) \ToNode (ABCD);
\draw (B) \ToNode (ABCD);
\draw (C) \ToNode (ABCD);
\draw (D) \ToNode (ABCD);
\end{scope}
%%%%%%%
\begin{scope}[]
\node[NodeStyleRaw] at (0cm, -5cm) () {
\begin{tikzpicture}
\begin{pgfonlayer}{main}
\node[NodeStyle3a] (number) {No. x};
\node[NodeStyle3a, right = 0mm of number] (name) {Graph-Node, outside graph};
\draw (number.north east) -- (number.south east);
\end{pgfonlayer}
\begin{pgfonlayer}{sub1}
\node[NodeStyle3b] (frame) [fit = (number) (name)] {};
\end{pgfonlayer}
\end{tikzpicture}
};
\end{scope}
%%%%%%%%%%
\begin{scope}[]
\node[NodeStyleRaw] at (0cm, -10cm) () {\NodeInputNumName{5}{Graph-Node as Command, outside Graph}};
\end{scope}
\end{tikzpicture}
\end{document}