4
\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}

which produces

enter image description here

I want the edge to run from A.east to B.west but I don't know how to accomplish this with edge.

I know I can use \draw (A.east) -- (B.west); but I have so many nodes, so I think it's better to draw the edges when I'm also drawing the nodes instead of drawing all the lines after I have drawn all the nodes.

Besides, how can I place the node with text Box title in the upper left corner of the fitted box?

0

2 Answers 2

6

Simply use

(B.west) edge (A.east)

enter image description here

MWE

\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}
3
  • Thank you! I have nodes which is drawn after other nodes, so sometimes my edges will be drawn on top of other nodes. Can I somehow tell tikz to always have nodes on top even though some edges are drawn afterwards?
    – Jamgreen
    Jun 21, 2015 at 9:59
  • @Jamgreen, good question. I'm not sure since the code is read from the top to bottom. So if you insert a node and edge that cross an already drawn edge... I don't know.
    – Sigur
    Jun 21, 2015 at 10:01
  • @Jamgreen Take a look at this answer: tex.stackexchange.com/questions/20425/z-level-in-tikz Jun 22, 2015 at 10:10
2

If straight lines is not a requirement, you can use edge[out=west,in=east].

enter image description here

\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}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .