1

I made a wide tree with Forest and I want use resizebox to scale it down to match the text length. I noticed that the space on the left of the tree is oddly bigger, specially if compared to the space on the right. How I can fix it?

enter image description here

\documentclass{article}

\usepackage{graphicx}
\usepackage{forest}

\begin{document}

Some text
\begin{center}
        \resizebox{\textwidth}{!}{
            \fbox{             
                \begin{forest}
                    for tree = {l sep = 20 mm}
                    [Root
                        [A long label for a inner node
                            [A long label for a leaf]
                            [A long label for a leaf]
                        ]
                        [A long label for a inner node
                            [A long label for a leaf]
                            [A long label for a leaf]
                        ]
                        [A long label for a inner node
                            [A long label for a leaf]
                            [A long label for a leaf]
                        ]
                        [A long label for a inner node
                            [A long label for a leaf]
                            [A long label for a leaf]
                        ]
                    ]
                \end{forest}
            }
        }
\end{center}
other text.

\end{document} 

Here is the output:enter image description here

  • 2
    Welcome! Did you update your installation recently? There was a bug that added space, see here, but it got fixed. So you may want to update your installation. – user194703 Mar 10 '20 at 22:54
  • you are adding space with \resizebox{\textwidth}{!}{SPACE \fbox{SPACE but as far as I can see you are adding two at each end so that doesn't really account for the asymmetry – David Carlisle Mar 10 '20 at 22:54
  • @DavidCarlisle Do you get the same space as the OP when you compile the code? I do not, and I do think it could be this bug. – user194703 Mar 10 '20 at 22:57
  • @Schrödinger'scat no extra space on the left in tl2018/tl2019/tl2020 – David Carlisle Mar 10 '20 at 23:16
  • @DavidCarlisle Yes, this is consistent (to the best of my knowledge). At a given point in 2019 forest got updated, and used a pgf function that had spurious spaces in. Later the bug in the pgf function got fixed. So I think that if you have versions only before the forest update or after the pgf update, you won't see these spaces. The simplest way to test this could be that Fabio tries out the patch under the link and checks whether the spaces go away. – user194703 Mar 10 '20 at 23:19
3

A hack: write this after loading forest.

\makeatletter
\preto\forest@pack{\nullfont}
\makeatother

This makes forest switch to the null font before starting to pack. (Packing is the heart of forest, where the positions of nodes are computed. There's a lot of computation going on there. In particular, a lot of line intersections are computed, using PGF's machinery, and this is where we typically get the leaking spaces.)

While the hack eats up the leaking spaces, it introduces an empty line (maybe a paragraph break?) on top of \fbox. I don't know why precisely this happens (I hope somebody can explain). A hack within hack is to add the comment signs at the beginning of \resizebox and \fbox arguments -- this removes the space tokens.

\documentclass{article}

\usepackage{graphicx}
\usepackage{forest}

\makeatletter
\preto\forest@pack{\nullfont}
\makeatother

\begin{document}

Some text
\begin{center}
        \resizebox{\textwidth}{!}{% without these comments
            \fbox{% we get some extra vertical space
                \begin{forest}
                    for tree = {l sep = 20 mm}
                    [Root
                        [A long label for a inner node
                            [A long label for a leaf]
                            [A long label for a leaf]
                        ]
                        [A long label for a inner node
                            [A long label for a leaf]
                            [A long label for a leaf]
                        ]
                        [A long label for a inner node
                            [A long label for a leaf]
                            [A long label for a leaf]
                        ]
                        [A long label for a inner node
                            [A long label for a leaf]
                            [A long label for a leaf]
                        ]
                    ]
                \end{forest}
            }
        }
\end{center}
other text.

\end{document} 

Note that while the hack works in this particular example, it will not do in general. It is sure to fail if you use typeset node after packing, because the null font is still in effect.

If I manage to iron out these kinks, I might even consider integrating it into forest itself -- after all, it is inavoidable that some future version of PGF will introduce some new leaking spaces ...

1

Better than make note text unreadable with scaling of forest tree diagram is consider to rotate it with grow=east and (if the nodes text is really long) enables nodes' texts to break into more lines. For example:

\documentclass{article}
%---------------- show page layout. don't use in a real document!
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{0.15pt}
\renewcommand*\ShowFrameColor{\color{red}}
%---------------------------------------------------------------%

\usepackage{forest}

\begin{document}

Some text
\begin{center}
\fbox{
    \begin{forest}
        for tree = {
        font=\small,
        grow'=east,
        child anchor=west,
        l sep = 12 mm,
        s sep =2mm,
        where level = {0}{}{text width=width("A long label ")}
                    }
        [Root
            [A long label for a inner node
                [A long label for a leaf]
                [A long label for a leaf]
            ]
            [A long label for a inner node
                [A long label for a leaf]
                [A long label for a leaf]
            ]
            [A long label for a inner node
                [A long label for a leaf]
                [A long label for a leaf]
            ]
            [A long label for a inner node
                [A long label for a leaf]
                [A long label for a leaf]
            ]
        ]
    \end{forest}
}
\end{center}
other text.

\end{document} 

enter image description here

(red lines indicate text borders)

  • This doesn't answer the question, does it? If the OP has a TeX installation from a certain period, it will still have the spaces in. – user194703 Mar 11 '20 at 0:26
  • Thank you for the answer! My real labels have different wides and I would like have something like tier=word for horizontal layout. Do you or @Schrödinger's cat how to do that (maybe I should open a new question)? – Fabio T. Mar 11 '20 at 16:10
  • @FabioT. No one knows these things better than Sašo Živanović. – user194703 Mar 11 '20 at 16:17
  • @Sašo Živanović can you help me? – Fabio T. Mar 11 '20 at 23:26
  • @FabioT., You have his answer below ... – Zarko Mar 12 '20 at 1:45

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.