\documentclass[tikz,border=5pt]{standalone}
\usetikzlibrary{positioning}
\usetikzlibrary{fit}
\begin{document}
\tikzset{
box/.style = {draw, text width=4cm, rounded corners=2pt, align=center, fill=white, fill opacity=0.8, text opacity=1},
}
\begin{tikzpicture}[node distance=0.25cm]
\node [box] (A) {A};
\node [box, below right=3cm of A] (B) {B} edge (A.east);
\node [fit=(A)(B), draw, inner sep=0.75cm] {} node [above left, fill=yellow!20, draw] {Box title};
\end{tikzpicture}
\end{document}
产生
我希望边缘从 运行A.east
到B.west
但我不知道如何用 来实现这一点edge
。
我知道我可以使用\draw (A.east) -- (B.west);
,但是我有这么多节点,所以我认为最好在绘制节点时绘制边,而不是在绘制所有节点后绘制所有线。
此外,我怎样才能将带有文本的节点放置Box title
在适合的框的左上角?
答案1
只需使用
(B.west) edge (A.east)
平均能量损失
\documentclass[tikz,border=5pt]{standalone}
\usetikzlibrary{positioning}
\usetikzlibrary{fit}
\begin{document}
\tikzset{
box/.style = {draw, text width=4cm, rounded corners=2pt, align=center, fill=white, fill opacity=0.8, text opacity=1},
}
\begin{tikzpicture}[node distance=0.25cm]
\node [box] (A) {A};
\node [box, below right=3cm of A] (B) {B} (B.west) edge (A.east); % <---
\node [fit=(A)(B), draw, inner sep=0.75cm] (fit) {} ;
\node[draw,fill=yellow!20,anchor=north west,outer sep=0.15cm] at (fit.north west) {Box Title};
\end{tikzpicture}
\end{document}
答案2
如果对直线不是要求的话,可以使用edge[out=west,in=east]
。
\documentclass[tikz,border=5pt]{standalone}
\usetikzlibrary{positioning}
\usetikzlibrary{fit}
\begin{document}
\tikzset{
box/.style = {draw, text width=4cm, rounded corners=2pt, align=center, fill=white, fill opacity=0.8, text opacity=1},
}
\begin{tikzpicture}[node distance=0.25cm]
\node [box] (A) {A};
\node [box, below right=3cm of A] (B) {B} edge[out=west,in=east] (A.east);
\node (box) [fit=(A)(B), draw, inner sep=0.75cm] {} node [below right=of box.north west, fill=yellow!20, draw] {Box title};
\end{tikzpicture}
\end{document}