6

I have a simple tree drawn with the forest package. When putting it in a wrapfigure environment, it goes out of the margin.

I have an MWE and corresponding result, where you can see part of the tree going out of the textwidth area. How can this be fixed?

\documentclass{article}
\usepackage{forest}
\usepackage{wrapfig}
\usepackage{blindtext}
\begin{document}
\begin{wrapfigure}{r}{0.25\textwidth}
    \centering
    \begin{forest}
        for tree={circle,draw,calign=center,edge=->,grow'=north}
        [A  [B [C] [D] ]    [E [F] [G] [H] ]    ]
    \end{forest}
    \caption{A tree}
    \label{fig:simple_tree}
\end{wrapfigure}
\blindtext
\end{document}

Enter image description here

3 Answers 3

5

The answer depends on what matters most to you: The amount of space your tree is taking up on the page or the size of the tree as produced by the forest package:

In the first case (where you would want the space the tree uses on the page to be limited to 0.25\textwidth) you could make the tree smaller with the use of the graphicx package with resizebox:

\documentclass{article}
\usepackage{forest}
\usepackage{wrapfig}
\usepackage{blindtext}
\usepackage{graphicx}
\begin{document}
\begin{wrapfigure}{r}{0.25\textwidth}
    \centering
     \resizebox{\linewidth}{!}{\begin{forest}
        for tree={circle,draw,calign=center,edge=->,grow'=north}
        [A  [B [C] [D] ]    [E [F] [G] [H] ]    ]
    \end{forest}}
    \caption{A tree}
    \label{fig:simple_tree}
\end{wrapfigure}
\blindtext
\end{document}

Here is the output:

enter image description here

In the second case (where you would want the tree to keep it's default size), you can give the tree more space on the page and thereby preventing your tree from leaving the margin:

\begin{wrapfigure}{r}{0.4\textwidth}\centering

Herbert described in his answer, how to do this and also gave an account on doing this without the need for try-and-error. Donald Arseneau described in his answer below another very convenient solution: As long as the figure and the caption are not "special" (e.g. complicated expansions) you can simply use 0pt as the width and let warpfigure do the measuring:

\begin{wrapfigure}{r}{0pt}\centering

Picture: enter image description here

6
\begin{wrapfigure}{r}{0.4\textwidth}\centering

If you want it not by trial and error, write the contents first in a box:

\documentclass{article}
\usepackage{forest}
\usepackage{wrapfig}
\usepackage{blindtext}
\usepackage{graphicx}
\newsavebox\ForestBox
\begin{document}

\savebox\ForestBox{%
    \begin{forest}%
        for tree={circle,draw,calign=center,edge=->,grow'=north}
        [A  [B [C] [D] ]    [E [F] [G] [H] ]    ]
    \end{forest}}

\begin{wrapfigure}{r}{\dimexpr\wd\ForestBox+2em}
\centering
\usebox\ForestBox
\caption{A tree}\label{fig:simple_tree}
\end{wrapfigure}
\blindtext
\end{document}

enter image description here

1
  • I incorporated your approach into my answer, as OP already accepted my answer, while I think that yours might be considered more suitable.
    – Wamseln
    Commented Dec 17, 2016 at 14:00
1

Late answer to old question, but wrapfig will do the measurement itself if the width is specified as zero (0pt). That might fail if the figure plus caption are unusual, but works fine here.

1
  • Very convenient! I included your solution above if that's ok.
    – Wamseln
    Commented Apr 11, 2020 at 8:06

You must log in to answer this question.

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