答案1
有类似的问题这里表示圆形节点。原则上,您可以使用命令执行此\nodeparts
操作(请参阅手册第 17.3 节),但我更喜欢使用pic
(请参阅第 18.2 节)。
以前,我曾pic
像这样定义过需要许多参数的 ,但下面我习惯于\pgfkeys
编写一个带有默认值的键值接口,因为这样使用起来更容易。pic 的最简单用法如下
\draw (0,0) pic{splitSq};
生产:
我已经编写了pic{splitSq={key1=val1, key2=val2,...}
以下键值参数:
size
= 矩形的尺寸(以厘米为单位)(默认1cm
)fill
= 填充颜色(默认gray!10
)top
= 顶部区域中的文本left
= 左下区域中的文本right
= 右下区域中的文本name
= 节点的 tikz 名称。这还分别定义了顶部、左侧和右侧区域的名称name
-T、name
-L 和-Rname
因此,代码:
\begin{tikzpicture}
\draw (6,0) pic{splitSq};
\draw (0,0) pic{splitSq={top=T,left=L,right=R}};
\draw (4,0) pic{splitSq={top=Hi, name=Hi}};
\draw (3,2) pic{splitSq={left=/, fill=blue!10, name=Blue}};
\draw (1,2) pic{splitSq={right=*, fill=green!10, name=Green}};
\draw[->] (Green-R)--(Blue-L);
\draw[->] (Hi)->(Blue);
\end{tikzpicture}
生成:
以下是完整的 MWE:
\documentclass{minimal}
\usepackage{tikz}
\begin{document}
\pgfkeys{/splitSq/.cd,
size/.initial = 1, % size of square in centimeters
size/.get = \splitSqSize, % store in macro \splitSqSize
size/.store in = \splitSqSize,
fill/.initial=gray!10, % fill colour
top/.initial=, % top text
left/.initial=, % left text
right/.initial=, % right text
name/.initial =, % name
name/.get = \splitSqLabel,
name/.store in = \splitSqLabel
}
\tikzset{
pics/splitSq/.style args = {#1}{% #1=key-value pairs
code = {
\bgroup
\pgfkeys{/splitSq/.cd, #1}% set key-value pairs
\node (\splitSqLabel) at (0,0) [shape=rectangle,
rounded corners,
draw, inner sep=0pt,
fill=\pgfkeysvalueof{/splitSq/fill},
minimum size = \splitSqSize cm] {};
\pgfmathsetmacro\quart{\splitSqSize/4}%
\draw(-2*\quart,0)--++(\splitSqSize,0);% and the lines
\draw(0,0)--++(0,-2*\quart);
\node(\splitSqLabel-L) at (-\quart,-\quart){\pgfkeysvalueof{/splitSq/left}};
\node(\splitSqLabel-R) at (\quart,-\quart) {\pgfkeysvalueof{/splitSq/right}};
\node(\splitSqLabel-T) at (0,\quart) {\pgfkeysvalueof{/splitSq/top}};
\egroup
}
}
}
\begin{tikzpicture}
\draw (0,0) pic{splitSq={top=T,left=L,right=R}};
\draw (4,0) pic{splitSq={top=Hi, name=Hi}};
\draw (3,2) pic{splitSq={left=/, fill=blue!10, name=Blue}};
\draw (1,2) pic{splitSq={right=*, fill=green!10, name=Green}};
\draw[->] (Green-R)--(Blue-L);
\draw[->] (Hi)->(Blue);
\end{tikzpicture}
\end{document}