1

I am trying to draw an edge which starts from the edge between nodes A and B in the picture below to a new node J as shown below. I am able to draw an edge from the edge between B and C to a new node K but for some reason I cannot get the edge to J to look right.

enter image description here

Code below:

\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
    [sibling distance=16em,level distance=8em, every node/.style={shape=rectangle,draw,align=center}]
    \tikzstyle{level 3}=[sibling distance=20em]
    \tikzstyle{level 4}=[sibling distance=8em] 
    
    \node (A){A}
        child{node(B){B}
            child{node{C}
                child{node{D}
                    child{node{E}}
                    child{node{F}}
                }
                child{node{G}
                    child{node{H}}
                    child{node{I}}
                }
            }
        };
    
    \path (A) -- node[right=7em] (J){J} (A-1) (J) edge (J-|A); 
    \path (B) -- node[right=7em] (K){K} (B-1) (K) edge (K-|B);   
\end{tikzpicture}
\end{document}

Thanks!

1 Answer 1

0

Edit: Since OP change the question ... new solution can be simply derived from original answer:

Try

\documentclass{standalone}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}[
        node distance = 7em,
        every node/.style={shape=rectangle,draw,align=center},
        sibling distance=16em,
        level distance=8em, 
        level 3/.style = {sibling distance=20em},
        level 4/.style = {sibling distance=8em}
                    ]
%
    \node (a) {A}
        child{node (b) {B}  % <---
            child{node (c) {C} % <---
                child{node{D}
                    child{node{E}}
                    child{node{F}}
                }
                child{node{G}
                    child{node{H}}
                    child{node{I}}
                }
            }
        };

\path (a) -- coordinate (j1)
        node[draw, right=7em] (j2) {J} (b)  (j2) edge[draw] (j1);
\path (b) -- coordinate (k1)
        node[draw, right=7em] (k2) {K} (c)  (k2) edge[draw] (k1);
\end{tikzpicture}
\end{document}

enter image description here

Addendum: Far more simple code is if you use forest package:

\documentclass[margin=5mm]{standalone}
\usepackage{forest}

\begin{document}
    \begin{forest}
for tree = {
% nodes
    draw,
% tree
    edge  = {-, semithick},
    s sep = 4em,
    l sep = 4em, 
            }
%
[A, name=a
    [B, name=b
        [C, name=c
            [D
                [E]
                [F]
            ]
            [G
                [H]
                [I]
            ]
        ]
    ]
]
\path (a) -- coordinate (j1) 
        node[draw, right=7em] (j2) {J} (b)  (j2) edge[draw] (j1);
\path (b) -- coordinate (k1)
        node[draw, right=7em] (k2) {K} (c)  (k2) edge[draw] (k1);
    \end{forest}
\end{document}

enter image description here

5
  • Thanks for your help! I updated the image in my original post to reflect the final figure I am trying to create. The edge connecting to J should start between A and B.
    – loperamide
    Commented Oct 15, 2021 at 4:16
  • Well, how many time you will change your problem? Is this really last version?
    – Zarko
    Commented Oct 15, 2021 at 4:21
  • Yes, my apologies. The figure I originally posted was what was generated by my incorrect code. I tried to describe in words the final result I was aiming for.
    – loperamide
    Commented Oct 15, 2021 at 4:32
  • See edited answer and added new example!
    – Zarko
    Commented Oct 15, 2021 at 4:33
  • Thank you very much!
    – loperamide
    Commented Oct 15, 2021 at 4:34

You must log in to answer this question.

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