4

Using \fbox around forest creates unwanted horizontal paddings to my graphs:

\documentclass{scrbook}

\usepackage{tikz}
\usepackage[linguistics]{forest}

\begin{document}
\begin{figure}[bth]
    \fboxsep = 4pt % Does not matter
    \fbox{\begin{forest}
        [A [B [D [I] [J] [K] ]
              [E [L] [M] ] ]
           [C [F] [G] [H] ] ]
    \end{forest}}
\end{figure}
\end{document}

Using fbox with forest

Removing \fbox aligns the diagram properly. However, I cannot get TikZ's background library to work with forest.

Using

\usetikzlibrary{backgrounds}
% ...
\tikzset{every picture/.style={show background rectangle}}

fails with the error "Dimension too large".

I now use TikZ nodes to create a frame around the forest environment:

\begin{forest}
    tikz+={\draw
        (current bounding box.south west) rectangle (current bounding box.north east);
    },
    [A [B [D [I] [J] [K] ]
          [E [L] [M] ] ]
       [C [F] [G] [H] ] ]
\end{forest}

Using tikz nodes to draw a frame

This works fine, but seems way too complicated and not very clean, especially when a custom inner padding is required (I solved this using xshift/yshift on the rectangle).

Are there better ways to achieve a frame around every TikZ and forest image?

Update: Working solution for all forest environments

This solution from the comments creates the desired frame for all forest environments:

\forestset{default preamble={tikz+={\tikzset{show background rectangle}}}}

Of course, \tikzset{every picture/.style={show background rectangle}} still results in the above mentioned error, requiring show background rectangle being set in every picture.

Update 2: Bugfixes as of 2019-07-29:

As mentioned in the comments:

  1. the issue preventing tikzset could be fixed with the next release of forest,
  2. there is a pull request to remove the leaking space leading to the padding in TikZ/PGF.
  • 1
    With your third solution you can just use \forestset{default preamble={tikz+={\tikzset{show background rectangle}}}} to make every tree boxed. That seems like the simplest solution. BTW, the warning not to use \fbox with TikZ is about using \fbox inside a TikZ node, not about wrapping a TikZ picture in an \fbox. – Alan Munn Jul 24 '19 at 15:40
  • 2
    The problem is not \fbox. Add a A just before your forest environment and you get an unwanted horizontal padding... – Paul Gaborit Jul 24 '19 at 16:52
  • 1
    The "Dimension too large" error occurs because forest typesets each node separately in a tikzpicture environment (to measure it), but defers its positioning (see 106.2.3 Deferred Node Positioning in PGF manual 3.1.3) so that the node's picture is empty. As you can check, an empty tikzpicture environment produces the same error. I will address this problem in the next release of forest by locally resetting every picture, so that it will apply only to the entire tree. – Sašo Živanović Jul 25 '19 at 8:37
  • 3
    Horizontal space is due to a leaking space in PGF. See my pull request at github.com/pgf-tikz/pgf/pull/723 – Sašo Živanović Jul 28 '19 at 0:40
  • 1
    Merged into master. – Sašo Živanović Jul 30 '19 at 5:51
1

The issue is due to a bug in PGF/TikZ which has now been fixed. The pull request by Sašo Živanović at https://github.com/pgf-tikz/pgf/pull/723 has now been merged into the master as per his comment at Easy border for tikz and forest.

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.