0

Here is the code:

\begin{figure}[H]\centering
    \begin{tikzpicture}
        \node (tree)
        {
            \begin{forest}
                for tree = {circle, draw, minimum width = 2.25em}
                    [\textcolor{red}{$S$}
                        [$A$, edge label = {node [midway, above left, font = \scriptsize] {\textcolor{red}{$1$}}}
                            [$B$, edge label = {node [midway, above left, font = \scriptsize] {$4$}}
                                [$D_1$, edge label = {node [midway, left, font = \scriptsize] {$7$}}
                                    [$G_1$, edge label = {node [midway, left, font = \scriptsize] {$10$}}]
                                ]
                            ]
                            [$C$, edge label = {node [midway, above right, font = \scriptsize] {$2$}}
                                [$D_2$, edge label = {node [midway, above left, font = \scriptsize] {$3$}}
                                    [$G_2$, edge label = {node [midway, left, font = \scriptsize] {$6$}}]
                                ]
                                [$G_3$, edge label = {node [midway, above right, font = \scriptsize] {$4$}}]
                            ]
                        ]
                        [$G_4$, edge label = {node [midway, above right, font = \scriptsize] {$12$}}]
                   ]
        \end{forest}
    };
\end{tikzpicture}

Here is the output:

enter image description here

Here are a couple of questions concerning this:

  1. I would like to color the boundary of nodes (circles) red, as well as make some of the edges red. Is there a way to do so?

  2. For the edge labels, as for horizontal and vertical positions as in node[midway, right] or node[midway, above], this is ok for vertical and horizontal edges, respectively, but for slanted edges, as they can take on any angle, I would like to label the edges such that they are of the same distance as the horizontal and vertical labels, but in a perpendicular direction to the slanted edges. How does one code it up?

  3. How to make the edge lengths longer? For all the edges? For some edges?

Any help would be appreciated. Thank you in advance!

  • You should never put a forest tree in a node of a tikzpicture. – user194703 Apr 20 '20 at 5:52
  • Hmm what I am trying to do is to illustrate some search algorithms (e.g. Depth First Search, Breadth First Search, A* Search, etc.) so I want to have the trees but in a sequence of colors. What's wrong with putting a forest tree in a node of a tikzpicture? – Superman Apr 20 '20 at 5:59
  • Then put it in a \savebox and the \savebox in a node. – user194703 Apr 20 '20 at 6:00
  • So would this work: \begin{document} \savebox{forest1}{%tree content} \begin[tikzpicture} \node(tree){\usebox{forest}} \end{tikzpicture} \end{document} Also, do you know how one can color the tree's nodes and edges as well as label the edges like what I want for slanted edges? – Superman Apr 20 '20 at 6:04
  • almost. \newsavebox\SomethingWithBackslash \savebox\SomethingWithBackslash{<tree>} \begin[tikzpicture} \node(tree){\usebox\SomethingWithBackslash}; \end{tikzpicture}. – user194703 Apr 20 '20 at 6:06
2

Well this is one forest with red circle nodes (draw=red), a larger l sep (l sep+=2em) and circle-shaped edge labels to ensure a universal distance. I also color one edge blue. You can add s sep+=1em and so on.

\documentclass{article}
\usepackage[edges]{forest}
\begin{document}
\tikzset{eln/.style={midway, font = \scriptsize,circle,inner sep=2pt}}
\begin{forest}
for tree = {circle, 
    draw=red, %<-added =red
    minimum width = 2.25em,
    l sep+=2em
}
    [\textcolor{red}{$S$}
        [$A$, edge label = {node [above left,eln] {\textcolor{red}{$1$}}}
            [$B$, edge label = {node [above left,eln] {$4$}}
                [$D_1$, edge label = {node [left,eln] {$7$}}
                    [$G_1$, edge label = {node [left,eln] {$10$}}]
                ]
            ]
            [$C$, edge label = {node [above right,eln] {$2$}},edge=blue%<-blue edge
                [$D_2$, edge label = {node [above left,eln] {$3$}}
                    [$G_2$, edge label = {node [left,eln] {$6$}}]
                ]
                [$G_3$, edge label = {node [above right,eln] {$4$}}]
            ]
        ]
        [$G_4$, edge label = {node [above right,eln] {$12$}}]
   ]
\end{forest}
\end{document}

enter image description here

Or color just a specific node.

\documentclass{article}
\usepackage[edges]{forest}
\begin{document}
\tikzset{eln/.style={midway, font = \scriptsize,circle,inner sep=2pt}}
\begin{forest}
for tree = {circle, 
    draw,%
    minimum width = 2.25em,
    l sep+=2em
}
    [\textcolor{red}{$S$}
        [$A$, edge label = {node [above left,eln] {\textcolor{red}{$1$}}}
            [$B$, edge label = {node [above left,eln] {$4$}}
                [$D_1$, edge label = {node [left,eln] {$7$}}
                    [$G_1$, edge label = {node [left,eln] {$10$}}]
                ]
            ]
            [$C$,draw=red, edge label = {node [above right,eln] {$2$}},edge=blue%<-blue edge
                [$D_2$, edge label = {node [above left,eln] {$3$}}
                    [$G_2$, edge label = {node [left,eln] {$6$}}]
                ]
                [$G_3$, edge label = {node [above right,eln] {$4$}}]
            ]
        ]
        [$G_4$, edge label = {node [above right,eln] {$12$}}]
   ]
\end{forest}
\end{document}

enter image description here

  • I don't want to color all the nodes red. How can I color some select nodes red and others blue? – Superman Apr 20 '20 at 6:15
  • Just add draw=red for this one and remove draw=red from for tree. – user194703 Apr 20 '20 at 6:15
  • What does \tikzset{eln/.style={midway, font = \scriptsize,circle,inner sep=2pt}} do? Finally, what could go wrong if I include the forest tree inside a node in tikzpicture? – Superman Apr 20 '20 at 6:20
  • It defines a style and spares you from repeating the same thing over and over. – user194703 Apr 20 '20 at 6:21
  • 1
    @Superman nesting TikZ picture is not supported and will fail. If you search for "nesting tikzpicture" you have a lot of hits, but for example look here: tex.stackexchange.com/questions/47377/… – Rmano Apr 21 '20 at 7:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.