我想将 tikz shape.multipart 节点拆分为 9 个相等的部分,以绘制 PERT 节点/网络。我可以拆分为三行,但不能拆分为三列。我想将其拆分为三列,每个部分大小相同。
\documentclass[border=12pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning,shadows,matrix,shapes.multipart}
\pgfdeclarelayer{foreground}
\pgfdeclarelayer{background}
\pgfsetlayers{background,main,foreground}
\makeatletter
\def\tikz@extra@preaction#1{
{%
\pgfsys@beginscope%
\setbox\tikz@figbox=\box\voidb@x%
\begingroup\tikzset{#1}\expandafter\endgroup%
\expandafter\def\expandafter\tikz@preaction@layer
\expandafter{\tikz@preaction@layer}%
\ifx\tikz@preaction@layer\pgfutil@empty%
\path[#1];% do extra path
\else%
\begin{pgfonlayer}{\tikz@preaction@layer}%
\path[#1];%
\end{pgfonlayer}
\fi%
\pgfsyssoftpath@setcurrentpath\tikz@actions@path% restore
\tikz@restorepathsize%
\pgfsys@endscope%
}%
}
\let\tikz@preaction@layer=\pgfutil@empty
\tikzset{preaction layer/.store in=\tikz@preaction@layer}
\makeatother
\tikzset{
mp/.style = {
rectangle split,
rectangle split parts=3,
rectangle split part fill={#1},
% rectangle split horizontal,
% rectangle split parts=3,
draw, rounded corners, text width=2cm,
align=center, text=white,
preaction layer=background,
drop shadow}
}
\begin{document}
\begin{tikzpicture}[->,shorten >=1pt,auto,node distance=1cm,thick]
\node (n1) [mp={red,blue,green}]
{
\nodepart[align=left]{one} 1 \\
\nodepart[align=left]{two} 2 \\
\nodepart[align=left]{three} 3
};
\end{tikzpicture}
\end{document}
答案1
我不会尝试进一步破解多部分节点。相反,使用 似乎更自然matrix
。可以使用 进行样式设置path picture
。
\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{matrix,shadows.blur}
\begin{document}
\begin{tikzpicture}
\matrix[matrix of nodes,column sep=-\pgflinewidth,row sep=-\pgflinewidth,
inner sep=0pt,draw,very thick,rounded corners,blur shadow,
path picture={
\path (path picture bounding box.north west) coordinate(TL) --
(path picture bounding box.south east) coordinate(BR)
foreach \XX in {0,...,3} {coordinate[pos=\XX/3] (p\XX)};
\foreach \XX [count=\YY] in {red,blue,green!70!black}
{\fill[\XX,sharp corners] (TL|-p\the\numexpr\YY-1) rectangle (BR|-p\YY);}
\foreach \XX in {1,2}
{\draw[thick] (TL|-p\XX) -- (BR|-p\XX) (TL-|p\XX) -- (BR-|p\XX); }},
nodes={text width=2cm,minimum height=1cm,align=center,
text=white,anchor=center,sharp corners,inner sep=2pt,thin}]
{ 1 & 2 & 3 \\
1 & 2 & 3 \\
1 & 2 & 3 \\};
\end{tikzpicture}
\end{document}
可以使用矩阵节点作为一个整体,或者像其他节点一样使用节点的单元来附加箭头等。
\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{matrix,shadows.blur,positioning}
\begin{document}
\begin{tikzpicture}
\matrix[matrix of nodes,column sep=-\pgflinewidth,row sep=-\pgflinewidth,
inner sep=0pt,draw,very thick,rounded corners,blur shadow,
path picture={
\path (path picture bounding box.north west) coordinate(TL) --
(path picture bounding box.south east) coordinate(BR)
foreach \XX in {0,...,3} {coordinate[pos=\XX/3] (p\XX)};
\foreach \XX [count=\YY] in {red,blue,green!70!black}
{\fill[\XX,sharp corners] (TL|-p\the\numexpr\YY-1) rectangle (BR|-p\YY);}
\foreach \XX in {1,2}
{\draw[thick] (TL|-p\XX) -- (BR|-p\XX) (TL-|p\XX) -- (BR-|p\XX); }},
nodes={text width=2cm,minimum height=1cm,align=center,
text=white,anchor=center,sharp corners,inner sep=2pt,thin}] (mat)
{ 1 & 2 & 3 \\
1 & 2 & 3 \\
1 & 2 & 3 \\};
\node[right=2em of mat,draw,rounded corners,blur shadow,fill=red!20] (txt){Text};
\node[below right=1ex and 2em of mat,draw,rounded corners,blur
shadow,fill=blue!20] (txt2)
{More Text};
\draw[-stealth] (mat) -- (txt);
\draw[-stealth] (mat-1-3) -| (txt);
\draw[-stealth] (mat-3-2) |- (txt2);
\end{tikzpicture}
\end{document}