如何在 TikZ 中为网络图创建自定义节点和箭头?

如何在 TikZ 中为网络图创建自定义节点和箭头?

我正在为我的关键路径方法讲座创建一个箭头上的活动项目图。我可以为其创建自定义节点,如附图中黑色所示。但是,我想在现有代码的基础上构建,以合并附加组件,如附图中红色所示。我无法实现。红色表示预期结果,答案的颜色不必是红色。请帮助我。

在此处输入图片描述

\documentclass[border=12pt]{standalone}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows}
\usetikzlibrary{chains}
\usetikzlibrary{positioning}

\tikzset{sectors/.style n args={5}{%
circle,
draw,
minimum width=#4,
append after command={%
\pgfextra{ %
 \draw (\tikzlastnode.center) -- (\tikzlastnode.south);
\draw (\tikzlastnode.west)   -- (\tikzlastnode.east);
\path (\tikzlastnode.center) -- node[#5] {#1} (\tikzlastnode.north);  
\path (\tikzlastnode.center) -- node[#5] {#2} (\tikzlastnode.south west); 
\path (\tikzlastnode.center) -- node[#5] {#3} (\tikzlastnode.south east);}}}}

\newcommand*\circled[1]{\tikz[baseline=(char.base)]{
        \node[shape=circle,draw,inner sep=1pt] (char) {#1};}}

\begin{document}
\begin{tikzpicture}[node distance=7cm,ultra thick]
\node [circle split,
     draw,
     minimum width=2cm,
     append after command={%
     \pgfextra{\draw (\tikzlastnode.center) -- (\tikzlastnode.south) ;
              } 
              }]  (a)  {};
\node[yshift=1.5em] at (a.center) {\LARGE \textbf{1}};  
\node[xshift=-1.2em,yshift=-1.2em] at (a.center) {\LARGE $a$}; 
\node[xshift= 1.2em,yshift=-1.2em] at (a.center) {\LARGE $b$};  
\node [circle split,
     draw,
     minimum width=2cm,
     append after command={%
     \pgfextra{\draw (\tikzlastnode.center) -- (\tikzlastnode.south) ;
              } 
              }]  (b) [right of=a] {};
\node[yshift=1.5em] at (b.center) {\LARGE \textbf{2}};  
\node[xshift=-1.2em,yshift=-1.2em] at (b.center) {\LARGE $c$}; 
\node[xshift= 1.2em,yshift=-1.2em] at (b.center) {\LARGE $d$};
\draw[->,ultra thick] (a) -- (b);  
\path[]
(a) edge [above] node {\LARGE a \qquad \textbf{A}\qquad b} (b)
(a) edge [below] node {\LARGE b \quad $\mathrm{D}_{1-2}$ \quad a} (b);
\end{tikzpicture}
\end{document}

答案1

这是一项提议。它使用fitlabel等等。也许最重要的一点是

\pgfextra{\draw (\tikzlastnode.center) -- (\tikzlastnode.south) ;
          } 

被取代了

 append after command={%
 (\tikzlastnode.center) edge (\tikzlastnode.south) 
          },

为什么?pgf 手册 v3.1.5 在第 170 页提到\pgfextra

请注意,此操作仅应由真正的专家使用,并且仅应在巧妙的宏内部深处使用,而不能在正常路径上使用。

我已经看到这种情况变得非常糟糕。(我知道这不是你的错。这种做法在很多帖子中都有,除了少数作者外,大多数作者都停止使用它。所以这句话不是针对提问的原贴作者,而是针对那些尽管收到警告但\pgfextra仍继续传播这种不良做法的人。)\pgfextra

\documentclass[border=12pt]{standalone}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{calc,fit,positioning,shapes.multipart}
\begin{document}
\begin{tikzpicture}[ultra thick,font=\LARGE]
\node [circle split,
     draw,
     minimum width=2cm,
     append after command={%
     (\tikzlastnode.center) edge (\tikzlastnode.south) 
              },
     label={[alias=a-ne]above right:{$b$}},
     label={[alias=a-nw]above left:{$a$}},
     label={[alias=a-sw]below left:{$a$}},
     label={[alias=a-se]below right:{$b$}},
     ]  (a)  {$\boldsymbol{1}$\nodepart{lower}$a\quad b$};
\path node[fit=(a-ne)(a-sw)(a-ne)(a-se),draw=none](F0){}
  let \p1=($(F0.north east)-(a.center)$),
    \p2=($(a.center)-(F0.south west)$),
    \n1={max(\x1,\x2,\y1,\y2)} in
  node[fit={([xshift=-\n1,yshift=-\n1]a.center)([xshift=\n1,yshift=\n1]a.center)},
    inner sep=0pt,draw] (F){}
  foreach \X in {west,south,east,north} {(a.\X) edge (F.\X)};    
\node [circle split,
     draw,
     minimum width=2cm,
     append after command={%
      (\tikzlastnode.center) edge (\tikzlastnode.south) 
              }]  (b) [right=6cm of a] {$\boldsymbol{2}$\nodepart{lower}$c\quad d$};
\draw[-latex,ultra thick,nodes={text height=1.2em,text depth=0.4ex}] (a) -- 
 node[pos=0.25,above=0.5ex,draw]{$a$}
 node[pos=0.25,below=0.5ex,draw]{$b$}
 node[pos=0.5,above=0.5ex]{$\boldsymbol{A}$}
 node[pos=0.5,below=0.5ex]{$D_{1-2}$}
 node[pos=0.75,above=0.5ex,draw]{$b$}
 node[pos=0.75,below=0.5ex,draw]{$a$}
(b);  
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容