尝试绘制图表问题框架纸。
我如何在左侧创建边框并在右角添加带有标签的附加框。是否也可以在两侧添加附加线?
它看起来应该是这样的:
我现在停留在这里:
\documentclass[tikz]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc,arrows,shapes.geometric,positioning}
\tikzset{element/.style={
draw,
thick,
node distance=1.5cm,
minimum width=1.5cm,
minimum height=1cm
},
desinedDomain/.style={element},
machine/.style={element},
requirement/.style={element,circle,dotted}
}
\begin{document}
\begin{figure}[htbp]
\centering
\begin{tikzpicture}
\node (m) [machine] {machine};
\node (d1) [desinedDomain, above right=of m] {domain1};
\node (d2) [element, below right=of m] {domain2};
\node (r1) [requirement, below right=of d1] {domain3};
\draw (m) -- (d1) node[midway,left] {a};
\draw (m) -- (d2) node[midway,above] {b};
\draw[dashed,->] (r1) -- (d1) node[midway,below] {c};
\draw[dashed,->] (r1) -- (d2) node[midway,above] {d};
\end{tikzpicture}
\caption{Problem Frames}
\label{fig:pf}
\end{figure}
\end{document}
答案1
另一个更复杂、更明显不同的解决方案(与我的第一个答案相比,这就是我决定采用新答案的原因)。
在此解决方案中,“子元素”被定义为节点“元素”中的内部标签。它们可以任意放置在节点内部,就像标准标签可以放置在节点外部一样,不同之处在于它们锚定在节点锚点的内部。对于节点“机器”的设计,我利用了multipart
节点和可能性,即将节点内容包含在其他节点参数之间。
因此,图片代码比第一个解决方案明显更简洁。完整的 MWE 现在是:
\documentclass[border=3mm,tikz]{standalone}
\usetikzlibrary{positioning,shapes.multipart}
\makeatletter
\def\tikzsavelastnodename#1{\let#1=\tikz@last@fig@name}
\makeatother
\tikzset{%
node distance=0mm and 15mm,
element/.style={%
draw, thick,
minimum size=11mm, inner xsep=5mm,
append after command={\pgfextra{\tikzsavelastnodename\tikzsavednodename}},#1
},
subelement/.style args={#1:#2}{%
append after command =
{node[draw,thick,minimum size=3mm,
inner sep=2pt] at (\tikzsavednodename.#1) [anchor=#1] {#2}}
},
machine/.style={%
shape=rectangle split, rectangle split horizontal,
rectangle split parts=3,
rectangle split empty part width=-3mm,
draw, thick,
minimum height=11mm, inner sep=2mm, outer sep=0mm,
node contents={\nodepart{three}\ #1\ }
},
requirement/.style={%
element, inner sep=2mm, circle, dotted}
}% end of tikzset
\begin{document}
\begin{tikzpicture}
\node (m) [machine=machine];
\node (d1) [element,
subelement=south east:C,
above right=of m] {domain1};
\node (d2) [element,
subelement=south east:B,
below right=of m] {domain2};
\node (r1) [requirement,
below right=of d1] {domain3};
%
\draw (m) -- node[above] {a} (d1)
(m) -- node[above] {b} (d2);
\draw[dashed,->] (r1) -- node[above] {c} (d1);
\draw[dashed,->] (r1) -- node[above] {d} (d2);
\end{tikzpicture}
\end{document}
获得的图片与我的第一个答案类似:
编辑:今天我发现,我已经上传了两次 MWE。所以我删除了多余的 MWE,并稍微(希望)改进了它的描述。
答案2
一个简单的解决方案是在“domain1”和“domain2”节点内添加两个小节点:
对于上图,我定义了subelement
小节点的样式。除此之外,我转到node distance
中的通用参数tikzset
,对其进行了轻微的更改(结果更类似于所讨论的推挤图像),对于所有具有矩形形状的节点,使用 样式element
。那就是:
编辑:哎呀,我忘了一个子问题。现在又加进去了。对于节点“machine”左侧的线条,我定义了新样式machine
,并在左侧大小的“machine”节点上添加了两个这样的节点。
\documentclass[border=3mm,tikz]{standalone}
\usetikzlibrary{calc,arrows,shapes.geometric,positioning}
\tikzset{%
node distance=3mm and 15mm,
element/.style={
draw,
thick,
minimum width=1.7cm,
minimum height=1cm
},
machine/.style={
element,
minimum width=0cm, outer sep=0pt,
subelement/.style={
draw,
thick,
inner sep= 1pt,
minimum size=3mm,
},
requirement/.style={element,circle,dotted}
}% end of tikzset
\begin{document}
\begin{tikzpicture}
\node (m) [element] {machine};
\node (d1) [element,above right=of m] {domain1};
\node[subelement,above left] at (d1.south east) {C};
\node (d2) [element,below right=of m] {domain2};
\node[subelement,above left] at (d2.south east) {B};
\node (r1) [requirement, below right=of d1] {domain3};
%
\draw (m) -- node[above] {a} (d1)
(m) -- node[above] {b} (d2);
\draw[dashed,->] (r1) -- node[above] {c} (d1);
\draw[dashed,->] (r1) -- node[above] {d} (d2);
\end{tikzpicture}
\end{document}
答案3
对于线条,您可以使用\pgfextra
节点的锚点来绘制线条。对于标签,您可以使用标签作为样式的参数。
输出
代码
\documentclass[tikz]{standalone}
\usetikzlibrary{calc,arrows,shapes.geometric,positioning}
\tikzset{
element/.style={
draw,
thick,
node distance=1.5cm,
minimum width=1.5cm,
minimum height=1cm
},
desinedDomain/.style={element,
label={[draw, anchor=south east, thick, inner sep=0, minimum size=3mm, shift={(\tikzlastnode.south east)}]#1}
},
machine/.style={element,append after command={\pgfextra{
\draw[thick] (\tikzlastnode.south west)++(0,.5\pgflinewidth) --++ (-1mm,0) |- ($(\tikzlastnode.north west)+(0,-.5\pgflinewidth)$);
\draw[thick] (\tikzlastnode.south west)++(0,.5\pgflinewidth) --++ (-2mm,0) |- ($(\tikzlastnode.north west)+(0,-.5\pgflinewidth)$);
}}},
requirement/.style={element,circle,dotted}
}
\begin{document}
\begin{tikzpicture}
\node (m) [machine] {machine};
\node (d1) [desinedDomain=c, above right=of m] {domain1};
\node (d2) [element, below right=of m] {domain2};
\node (r1) [requirement, below right=of d1] {domain3};
\draw (m) -- (d1) node[midway,left] {a};
\draw (m) -- (d2) node[midway,above] {b};
\draw[dashed,->] (r1) -- (d1) node[midway,below] {c};
\draw[dashed,->] (r1) -- (d2) node[midway,above] {d};
\end{tikzpicture}
\end{document}