我正在为一个更大的项目尝试一些实验。(但我不知道从哪里开始)我想编写一些宏来帮助我生成类似这样的内容
基于以下代码(语法不是最终的,因此可能会发生变化)。
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\begin{document}
\begin{figure}
\begin{tikzpicture}
\tikzset{
entity/.style={
rectangle,
draw
},
port/.style={
draw,
rectangle,
minimum height=1cm,
minimum width=1cm
}
}
\node[draw,rectangle,minimum width=8cm,minimum height=4.5cm] (parent) {
parent
\node[port,west] {A};
\node[port,west] {B};
\node[port,west] {C};
\node[port,east] {D};
\node[port,east] {E};
\node[entity,minimum width=5cm,minimum height=2cm] (subentity-1) {
subentity-1
\node[port,west] {1};
\node[port,east] {2};
}
\node[entity,minimum width=5cm,minimum height=2cm] (subentity-2) {
subentity-2
\node[port,west] {1};
\node[port,east] {2};
}
};
\end{tikzpicture}
\end{figure}
\end{document}
我正在努力解决的事情:
节点内有节点(
entity
嵌套)我如何
entity
在另一个中定义一个entity
?(也许深度有好几个层次)。节点嵌套(以及定位
west
和east
)如果我将一个放在
port
一个里面entity
,我能发现父级entity
(即包含它的父级)并将其相应地定位在parent.east
或吗parent.west
?将所有
port
节点均匀分布在west
(或east
)边缘上我想计算实体的高度
parent
,然后沿着边缘扩展该实体内的端口。
伪代码中的简单算法如下所示:
parent = this.parent
height = parent.minimum height
westports = parent.get-children().filter('west')
for idx, port in westports:
offset = height/count(westports) * idx
port.position = parent.north west - offset
欢迎对现有问题/答案提出好的建议。(我没有找到任何有用的东西,主要是因为我不知道要寻找什么)
编辑:对我的问题添加了说明。
免责声明:接受的答案并未回答上述问题,但为我的问题提供了有效的解决方案。
答案1
编辑:自动化版本
我创建了三个命令\createBox
,\subBox
分别\smallBox
创建大实体矩形、节点内的节点(子实体)和小端口节点,这些节点可以放置在线上。
- 创建框:
\createBox{<minimum width[cm]>}{<minimum height[cm]>}{<name>}{<text inside>}{<color>}
- 子框:
\subBox{<minimum width [cm]>}{<minimum height[cm]>}{<xshift[cm]>}{<yshift[cm]>}{<node name>}{<node anchor name>}{<text inside>}{<color>}
笔记: <xshift[cm]>
以及<yshift[cm]>
关于<node anchor name>.center
。
- 小盒子:
\smallBox{<node anchor name>}{<angle/position>}{<text inside>}{<color>}
笔记: 节点位置:节点锚点name.〈angle〉
,角度在0(=东)和360之间,逆时针测量。<xshift[cm]>
并<yshift[cm]>
相对于<node anchor name>.center
。
梅威瑟:版本 2(自动)
\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning,calc}
\tikzset{
entity/.style={
rectangle,
draw
},
port/.style={
draw,
rectangle,
minimum height=1cm,
minimum width=1cm
},
box/.style={draw,minimum width=0.5cm,minimum height=0.5cm,align=center,fill=white}
}
\newcommand{\createBox}[5]
{
\node[#5,entity,minimum width=#1 cm,minimum height=#2 cm](#3) {#4};
}
\newcommand{\subBox}[8]
{
\node[#8,port,minimum width=#1 cm,minimum height=#2 cm,xshift = #3 cm ,yshift = #4 cm](#5) at (#6.center) {#7};
}
\newcommand{\smallBox}[4]
{
\node[#4,box] at (#1.#2) {#3};
}
\begin{document}
\begin{tikzpicture}
\createBox{6.5}{3.5}{Box_A}{My Box: A}{red}
\smallBox{Box_A}{165}{A}{blue}
\smallBox{Box_A}{180}{B}{blue}
\smallBox{Box_A}{195}{C}{blue}
\smallBox{Box_A}{10}{D}{blue}
\smallBox{Box_A}{-10}{E}{blue}
\subBox{3}{0.75}{0}{1}{Sub_Box_1}{Box_A}{My subbox: 1}{green}
\smallBox{Sub_Box_1}{0}{2}{blue}
\smallBox{Sub_Box_1}{180}{1}{blue}
\subBox{3}{0.75}{0}{-1}{Sub_Box_2}{Box_A}{My subbox: 2}{orange}
\smallBox{Sub_Box_2}{0}{2}{blue}
\smallBox{Sub_Box_2}{180}{1}{blue}
\end{tikzpicture}
\end{document}
第一个答案:基本的
基本上,您可以使用\usetikzlibrary{positioning}
和\usetikzlibrary{calc}
进行定位。
梅威瑟:版本 1
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\tikzset{
entity/.style={
rectangle,
draw
},
port/.style={
draw,
rectangle,
minimum height=1cm,
minimum width=1cm
}
}
\node[port](A) {A};
\node[port,below= 0.5cm of A](B) {B};
\node[port,below= 0.5cm of B](C) {C};
\node[port,right= 1cm of A](A1) {a1};
\node[port,right= 2cm of A1](A2) {a2};
\node[port,right= 1cm of C,yshift=0.3cm](C1) {c1};
\node[port,right= 2cm of C1](C2) {c2};
\node[port,right= 1cm of A2,yshift=-0.2cm](D) {D};
\node[port,right= 1cm of C2,yshift=0.2cm](E) {E};
\draw (A1.north) |- + (3,0.5) |- (A2.north);
\draw (A1.south) |- + (3,-0.5) |- (A2.south);
\node (S1) at ($(A1)!0.5!(A2)$) {subentity-1};
\draw (C1.north) |- + (3,0.5) |- (C2.north);
\draw (C1.south) |- + (3,-0.5) |- (C2.south);
\node (S2) at ($(C1)!0.5!(C2)$) {subentity-2};
\node (Ec) at ($(S1)!0.5!(S2)$) {entity};
\draw (A.north) |- + (7,1) |- (D.north);
\draw (A.south) -- (B.north);
\draw (B.south) -- (C.north);
\draw (C.south) |- + (7,-1) |- (E.south);
\draw (D.south) -- (E.north);
\end{tikzpicture}
\end{document}