3

I would like to take a Forest like so:

Forest: enter image description here

MWE:

\documentclass[border=10pt,tikz,multi]{standalone}
\usepackage[edges]{forest}
\begin{document}

\begin{forest}
  styleA/.style={top color=green!20, bottom color=green!50},
  styleB/.style={top color=white, bottom color=red!20},
  for tree={
    rounded corners,
    draw,
    align=center,
    top color=white,
    bottom color=blue!20,
  },
  forked edges,
  [Blablabla
     [Blablabla,styleA]
     [Blablabla,styleA]
     [Blablabla
        [Blablabla]
        [Blablabla]
        [Blablabla
           [Blablabla
              [Blablabla]
              [Blablabla,styleB]
              [Blablabla]
              [Blablabla
                 [Blablabla,styleB]
                 [Blablabla]
              ]
           ]
           [Blablabla
              [Blablabla
                 [Blablabla
                    [Blablabla]
                    [Blablabla]
                    [Blablabla]
                    [Blablabla[Blablabla][Blablabla]]
                 ]
              ]
              [Blablabla
                 [Blablabla
                    [Blablabla
                       [Blablabla]
                       [Blablabla]
                       [Blablabla]
                       [Blablabla[Blablabla][Blablabla]]
                    ]
                 ]
                 [Blablabla[Blablabla[Blablabla[Blablabla
                             [Blablabla]
                             [Blablabla]
                             [Blablabla]
                 ]]]]
              ]
              [Blablabla
                 [Blablabla[Blablabla
                       [Blablabla]
                       [Blablabla]
                       [Blablabla]
                 ]]
                 [Blablabla[Blablabla[Blablabla[Blablabla
                             [Blablabla]
                             [Blablabla]
                             [Blablabla]
                 ]]]]
              ]
           ]
        ]
     ]
  ]
\end{forest}
\end{document}

And embed add images to the nodes/inside the nodes like so

enter image description here

Is there a way to achieve this?

7
  • Those don't appear to be inside the nodes. If they shouldn't affect the spacing, use tikz+= to add nodes on top containing the images. If they really should be inside the nodes, then you can just add them within the relevant nodes wherever appropriate.
    – cfr
    Apr 20, 2016 at 17:26
  • @cfr I can settle for fully inside. can you give a MWE with an example as I don't see how tikz+= can be used to embed a standard image file.
    – puk
    Apr 20, 2016 at 18:02
  • You don't need tikz+ if you put it inside the node, but only if you don't. I'll try to give an example with tikz+ in a bit if nobody else answers first.
    – cfr
    Apr 20, 2016 at 19:21
  • @cfr I don't understand, you mean just drop an includegraphics{...} command inside the node?
    – puk
    Apr 20, 2016 at 19:47
  • Yes. I assume it would work. You might need to use {blablabla \includegraphics[<options>]{filename}} but it should work, I'd think. It is just a TikZ node at the end of the day.
    – cfr
    Apr 20, 2016 at 20:18

1 Answer 1

6

You can either add the image within the node or add the image in its own node after the tree is drawn either directly or using tikz. Either way, you can do this either for specific nodes within the tree or for all nodes assigned a certain style. The image can be specified as part of the style or it can be determined when the style is used.

The following examples present most of these possibilities.

The simplest approach is just to add the image in the node content. For example:

    [Blablabla\\\includegraphics{info},styleA]

will add the info image from the notes package on a line below the node text Blablabla.

If we need to pass \includegraphics an optional argument, we need to protect it from Forest's bracket parser:

    [Blablabla  {\includegraphics[scale=.5]{warn}},styleA]

This can also be added using a style. For example, we could adapt styleB to include the hand image:

  styleB/.style={%
    top color=white,
    bottom color=red!20,
    delay={%
      content/.wrap value={##1\\{\includegraphics[scale=.5]{hand}}}
    }
  },

Then our tree will look like this:

images in nodes

Alternatively, we could add the images in their own nodes after the tree is drawn. One way to do this is to use Forest's tikz or tikz+ key. For example, we might define an add icon style to do this conveniently as follows:

  add icon/.style={%
    !u.s sep+=5pt,
    tikz+={\node [draw, circle, inner sep=1pt, fill=white] at (.south west) {\includegraphics [height=\baselineskip]{#1}};}
  },

Then we can write

    [Blablabla, add icon=warn,styleA]
    [Blablabla, add icon=info,styleA]

to add the specified image to the designated node. Again, this could also be used in a modified styleB, for example.

styleB/.style={%
    top color=white,
    bottom color=red!20,
    add icon=hand,
  },

Then our tree will look like this:

badge-style addition of images to nodes

Complete code:

\documentclass[border=10pt,tikz,multi]{standalone}
\usepackage[edges]{forest}
\begin{document}

\begin{forest}
  styleA/.style={top color=green!20, bottom color=green!50},
  styleB/.style={%
    top color=white,
    bottom color=red!20,
    delay={%
      content/.wrap value={##1\\{\includegraphics[scale=.5]{hand}}}
    }
  },
  for tree={
    rounded corners,
    draw,
    align=center,
    top color=white,
    bottom color=blue!20,
  },
  forked edges,
  [Blablabla
    [Blablabla  {\includegraphics[scale=.5]{warn}},styleA]
    [Blablabla\\\includegraphics{info},styleA]
     [Blablabla
        [Blablabla]
        [Blablabla]
        [Blablabla
           [Blablabla
              [Blablabla]
              [Blablabla,styleB]
              [Blablabla]
              [Blablabla
                 [Blablabla,styleB]
                 [Blablabla]
              ]
           ]
           [Blablabla
              [Blablabla
                 [Blablabla
                    [Blablabla]
                    [Blablabla]
                    [Blablabla]
                    [Blablabla[Blablabla][Blablabla]]
                 ]
              ]
              [Blablabla
                 [Blablabla
                    [Blablabla
                       [Blablabla]
                       [Blablabla]
                       [Blablabla]
                       [Blablabla[Blablabla][Blablabla]]
                    ]
                 ]
                 [Blablabla[Blablabla[Blablabla[Blablabla
                             [Blablabla]
                             [Blablabla]
                             [Blablabla]
                 ]]]]
              ]
              [Blablabla
                 [Blablabla[Blablabla
                       [Blablabla]
                       [Blablabla]
                       [Blablabla]
                 ]]
                 [Blablabla[Blablabla[Blablabla[Blablabla
                             [Blablabla]
                             [Blablabla]
                             [Blablabla]
                 ]]]]
              ]
           ]
        ]
     ]
  ]
\end{forest}
\begin{forest}
  add icon/.style={%
    !u.s sep+=5pt,
    tikz+={\node [draw, circle, inner sep=1pt, fill=white] at (.south west) {\includegraphics [height=\baselineskip]{#1}};}
  },
  styleA/.style={top color=green!20, bottom color=green!50},
  styleB/.style={%
    top color=white,
    bottom color=red!20,
    add icon=hand,
  },
  for tree={
    rounded corners,
    draw,
    align=center,
    top color=white,
    bottom color=blue!20,
    inner sep=5pt
  },
  forked edges,
  [Blablabla
    [Blablabla, add icon=warn,styleA]
    [Blablabla, add icon=info,styleA]
     [Blablabla
        [Blablabla]
        [Blablabla]
        [Blablabla
           [Blablabla
              [Blablabla]
              [Blablabla,styleB]
              [Blablabla]
              [Blablabla
                 [Blablabla,styleB]
                 [Blablabla]
              ]
           ]
           [Blablabla
              [Blablabla
                 [Blablabla
                    [Blablabla]
                    [Blablabla]
                    [Blablabla]
                    [Blablabla[Blablabla][Blablabla]]
                 ]
              ]
              [Blablabla
                 [Blablabla
                    [Blablabla
                       [Blablabla]
                       [Blablabla]
                       [Blablabla]
                       [Blablabla[Blablabla][Blablabla]]
                    ]
                 ]
                 [Blablabla[Blablabla[Blablabla[Blablabla
                             [Blablabla]
                             [Blablabla]
                             [Blablabla]
                 ]]]]
              ]
              [Blablabla
                 [Blablabla[Blablabla
                       [Blablabla]
                       [Blablabla]
                       [Blablabla]
                 ]]
                 [Blablabla[Blablabla[Blablabla[Blablabla
                             [Blablabla]
                             [Blablabla]
                             [Blablabla]
                 ]]]]
              ]
           ]
        ]
     ]
  ]
\end{forest}
\end{document}
2
  • Thanks. This is an amazing answer. However, I think I am running into problems because I am on windows (not my choice) and the backslash is causing problems. I switched to global paths as ..\..\images was causing problems
    – puk
    Apr 20, 2016 at 21:05
  • 1
    You may need forward slashes - even on Windows. But that's a different question and, frankly, I'm not a good person to ask. I know almost nothing (as little as I can manage to get away with) about Windows ....
    – cfr
    Apr 20, 2016 at 21:47

You must log in to answer this question.

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