1

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!

Superman
  • 1,615
  • You should never put a forest tree in a node of a tikzpicture. –  Apr 20 '20 at 05: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 05:59
  • Then put it in a \savebox and the \savebox in a node. –  Apr 20 '20 at 06: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 06:04
  • almost. \newsavebox\SomethingWithBackslash \savebox\SomethingWithBackslash{<tree>} \begin[tikzpicture} \node(tree){\usebox\SomethingWithBackslash}; \end{tikzpicture}. –  Apr 20 '20 at 06:06
  • Ok. Can you post a sample solution showing two copies of the same tree but of different colors on nodes and edges as well as the the edge labels on slanted edges based on what I want for my results? – Superman Apr 20 '20 at 06:07

1 Answers1

3

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 06:15
  • Just add draw=red for this one and remove draw=red from for tree. –  Apr 20 '20 at 06: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 06:20
  • It defines a style and spares you from repeating the same thing over and over. –  Apr 20 '20 at 06:21
  • Ok, that’s for the first question. What about the second question? What could be some problems that can arise in the case of what I did originally? – Superman Apr 20 '20 at 06:50
  • 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: https://tex.stackexchange.com/questions/47377/proper-nesting-of-tikzpicture-environments-reset-all-pgf-values-to-their-defaul – Rmano Apr 21 '20 at 07:25