7

Apologies if this is a silly question - my first on stackexchange. I have built the tree below using the forest package. Unfortunately, the tree has got too "wide" for the page.

Is there a way to ask forest to put eg the pink set of boxes on a long line below the green boxes, so that the tree fits on the page? Or can you think of a way to typeset this graph so that it fits horizontally without looking too "squashed"?

enter image description here

Thanks!

\documentclass{article}

\usepackage{forest}
\usepackage{tikz}
\usetikzlibrary{shadows,arrows.meta}

%Defining the styles used in trees
%Note that the fill colour is not defined here.
\tikzset{parent/.style={align=center,text width=2cm,rounded corners=2pt},
    child/.style={align=center,text width=2cm,rounded corners=6pt},
    grandchild/.style={text width=2.3cm}
}

\begin{document}

\begin{forest}
for tree={%
    l sep=0.6cm,
    s sep=0.8cm,
    minimum height=0.8cm,
    minimum width=2cm,
    draw %Put lines around each
    }
[Company, name=Company, parent, fill=blue!30
    [Distributor, for tree={child, fill=green!30}  %Format everything below here as children
        [Sub distributor
            [Agent
                [Customer] {\draw[->,dotted] () to[out=north,in=north west] (Company);}
                [Installer]
                [Installer]
            ]
            [Agent]
        ]
        [Sub distributor
            [Agent]
            [Agent]
        ]
    ]
    [Distributor, for tree={child, fill=pink!50}
        [Sub distributor
            [Installer
                [Customer] {\draw[<->] () to[out=east,in=south] (Scratch);}
            ]
            [Scratchcard reseller, name=Scratch]
        ]
        [Sub distributor]
    ]
]
\end{forest}

\end{document}
David
  • 381

1 Answers1

10

You can adjust the text width for nodes and also adjust the l ans s sep to bring the nodes closer. Adding further, swap the Sub distributor nodes. You have to modify the dotted arrow slightly though.

\documentclass{article}

\usepackage{forest}
\usetikzlibrary{shadows,arrows.meta}

%Defining the styles used in trees
%Note that the fill colour is not defined here.
\tikzset{parent/.style={align=center,text width=2cm,rounded corners=2pt},
    child/.style={align=center,text width=2cm,rounded corners=6pt},
    grandchild/.style={text width=2.3cm}
}

\begin{document}

\begin{forest}
for tree={%
    l sep=1cm,
    s sep=0.1cm,
    minimum height=0.8cm,
    minimum width=2cm,
    draw %Put lines around each
    }
[Company, name=Company, parent, fill=blue!30
    [Distributor, for tree={child, fill=green!30}  %Format everything below here as children
        [Sub distributor
            [Agent]
            [Agent]
        ]
        [Sub distributor,
            [Agent,
                [Customer] {\draw[->,dotted] () -- ++(-4cm,0) to[out=north,in=west] (Company);}
                [Installer]
                [Installer]
            ]
            [Agent]
        ]
    ]
    [Distributor, for tree={child, fill=pink!50}
        [Sub distributor]
        [Sub distributor
            [Installer
                [Customer] {\draw[<->] () to[out=east,in=south] (Scratch);}
            ]
            [Scratchcard reseller, name=Scratch]
        ]
    ]
]
\end{forest}

\end{document}

enter image description here

I hope this is enough crunching to bring things inside the page ;-).

By using s=1cm in

[Company,s=1cm, name=Company,... 

the horizontal separation between green and pink gangs can be adjusted to get

enter image description here

but it will again increase the total width (which is what we were trying to reduce!).

Shifting pinks to the left will give

\documentclass{article}

\usepackage{forest}
\usetikzlibrary{shadows,arrows.meta}

%Defining the styles used in trees
%Note that the fill colour is not defined here.
\tikzset{parent/.style={align=center,text width=2cm,rounded corners=2pt},
    child/.style={align=center,text width=2cm,rounded corners=6pt},
    grandchild/.style={text width=2.3cm}
}

\begin{document}

\begin{forest}
for tree={%
    l sep=1cm,
    s sep=0.1cm,
    minimum height=0.8cm,
    minimum width=2cm,
    draw %Put lines around each
    }
[Company, name=Company, parent, fill=blue!30
    [Distributor, for tree={child, fill=pink!50}
        [Sub distributor
            [Installer
                [Customer] {\draw[<->] () to[out=east,in=south] (Scratch);}
            ]
            [Scratchcard reseller, name=Scratch]
        ]
        [Sub distributor]
    ]
    [Distributor, for tree={child, fill=green!30}  %Format everything below here as children
        [Sub distributor
            [Agent]
            [Agent]
        ]
        [Sub distributor,
            [Agent,
                [Customer] {\draw[->,dotted] () --++(0,-1cm) -- ++(6.5cm,0) to[out=north,in=east] (Company);}
                [Installer]
                [Installer]
            ]
            [Agent]
        ]
    ]
]
\end{forest}

\end{document}

enter image description here

As can be seen this doesn't improve much.

  • Thanks for the suggestions - they were all really helpful! One last one if you don't mind - I think it would be more readable if the pink boxes lived in their own vertical space (not overlapping with the greens). Is there a way to create a "virtual divider" or shuffle all the pinks to the right? – David Feb 04 '15 at 11:26
  • @David You can but it will make the tree wider again. (I'm assuming you meant 'horizontal' rather than 'vertical' since that's what moving them right would do.) – cfr Feb 04 '15 at 13:20
  • @cfr Thanks for the correction, I did mean horizontal space. Sorry I'm being slow but how would I do that? – David Feb 04 '15 at 14:29
  • @David Answer updated and this is what can be done at most IMO. You may try to grow the tree to the right (instead of down) if you have some juice left in you ;-) –  Feb 04 '15 at 14:46
  • @David option fit=rectangle (page 29 in forest documentation) in one of distributior nodes, will avoid overlapping between pink and green subtrees. – Ignasi Feb 04 '15 at 14:57
  • 1
    @Ignasi All roads lead to Rome :-). It will again increase the width. –  Feb 04 '15 at 15:01
  • If you don't mind the 'Agent' nodes being smaller, you could change the minimum width for those. It is the total width of 6 nodes on that tier of the tree which is the obstacle here: separating the pink from the green requies spreading those out to create the gap and that space has to come from somewhere. You can't have your space and eat it! – cfr Feb 04 '15 at 16:17
  • This is great advice. If it still doesn't get the tree narrow enough, you could scale the whole tree down with \scalebox. – Jason Zentz Feb 04 '15 at 16:55
  • Thanks all for your advice - you've been really helpful :-) – David Feb 05 '15 at 10:33