[Tex/LaTex] Directory tree on multiple pages using Forest package

forestpage-breakingtikz-pgf

I am looking for a way to split a long directory tree across multiple pages.

I've found this post: Automatic page break with Forest? (Spanning a tree over multiple pages.) but I find these examples too complex to analyze.

I have also come across this question: Breaking pictures across multiple pages and used the proposed solution to forest.

The result is as follows, which is almost what I want, except the fact the tree starts on a new page instead of just under the text.

The code is as follows:

\documentclass[a4paper]{article}
\usepackage[edges]{forest}
\usepackage{adjustbox}
\usepackage{blindtext}

\newsavebox{\mysavebox}
\newlength{\myrest}
\begin{document}

\blindtext

\begin{lrbox}{\mysavebox}%
\begin{forest}
  for tree={folder,grow'=0,fit=band}
  [ XYZ
      [ XYZ
          [ XYZ
              [ XYZ
                  [ XYZ ]
              ]
          ]
          [ XYZ
              [ XYZ
                  [ XYZ ]
              ]
          ]
          [ XYZ
              [ XYZ
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
              ]
              [ XYZ
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
              ]
              [ XYZ
                  [ XYZ ]
              ]
              [ XYZ
                  [ XYZ ]
                  [ XYZ ]
              ]
          ]
          [ XYZ
              [ XYZ
                  [ XYZ ]
                  [ XYZ ]
              ]
          ]
          [ XYZ
              [ XYZ
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
              ]
              [ XYZ
                  [ XYZ ]
              ]
              [ XYZ
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
              ]
          ]
          [ XYZ
              [ XYZ
                  [ XYZ ]
              ]
          ]
          [ XYZ
              [ XYZ
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
              ]
          ]
          [ XYZ
              [ XYZ
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
              ]
          ]
          [ XYZ
              [ XYZ
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
              ]
              [ XYZ
                  [ XYZ ]
                  [ XYZ ]
              ]
              [ XYZ
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
              ]
          ]
          [ XYZ
              [ XYZ
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
              ]
              [ XYZ
                  [ XYZ ]
              ]
              [ XYZ
                  [ XYZ ]
              ]
          ]
          [ XYZ
              [ XYZ
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
              ]
              [ XYZ
                  [ XYZ ]
              ]
          ]
          [ XYZ
              [ XYZ
                  [ XYZ ]
                  [ XYZ ]
              ]
          ]
          [ XYZ
              [ XYZ
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
              ]
              [ XYZ
                  [ XYZ ]
              ]
          ]
          [ XYZ
              [ XYZ
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
              ]
              [ XYZ
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
              ]
          ]
          [ XYZ
              [ XYZ
                  [ XYZ ]
              ]
              [ XYZ
                  [ XYZ ]
              ]
          ]
          [ XYZ
              [ XYZ
                  [ XYZ ]
              ]
          ]
          [ XYZ
              [ XYZ
                  [ XYZ ]
              ]
          ]
          [ XYZ
              [ XYZ
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
              ]
              [ XYZ
                  [ XYZ ]
              ]
          ]
          [ XYZ
              [ XYZ
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
              ]
              [ XYZ
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
              ]
              [ XYZ
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
              ]
              [ XYZ
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
              ]
          ]
          [ XYZ
              [ XYZ
                  [ XYZ ]
                  [ XYZ ]
              ]
          ]
          [ XYZ
              [ XYZ
                  [ XYZ ]
                  [ XYZ ]
              ]
          ]
          [ XYZ
              [ XYZ
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
              ]
              [ XYZ
                  [ XYZ ]
              ]
          ]
      ]
  ]
\end{forest}
\end{lrbox}


%
\ifdim\ht\mysavebox>\textheight
    \setlength{\myrest}{\ht\mysavebox}%
    \loop\ifdim\myrest>\textheight
        \newpage\par\noindent
        \clipbox{0 {\myrest-\textheight} 0 {\ht\mysavebox-\myrest}}{\usebox{\mysavebox}}%
        \addtolength{\myrest}{-\textheight}%
    \repeat
    \newpage\par\noindent
    \clipbox{0 0 0 {\ht\mysavebox-\myrest}}{\usebox{\mysavebox}}%
\else
    \usebox{\mysavebox}%
\fi

\end{document}

enter image description here

Best Answer

You requested a minimal example without styling. This answer provides such a minimal example. However, do note that the vast majority of the code in the answer you commented on had nothing to do with the styling. Most of the code was simply splitting the tree across pages. Since that is what you wish to do, there is no more minimal example to be given. (Well, there may be a more elegant solution, of course. That's another matter.)

Although you didn't mention the problem, but only said the styling code confused you, the automatic breaking in my earlier answer doesn't work for your example because you have nodes with more children than will fit on a page, whereas the code assumes that at most one split is required among any node's children.

I think the following should work. It avoids the need to accept split node content/words across pages.

However, as you will no doubt expect, it is only minimally tested.

Caveat emptor.

\documentclass[a4paper]{article}
\usepackage{geometry,blindtext}% geometry or similar is needed for correct A4 layout
\usepackage[edges]{forest}

% addaswyd o gôd Sašo Živanović: http://tex.stackexchange.com/a/296771/
\def\hiddenparcommand{\par}
\newcommand\otherhiddenparcommand{\par\noindent}
\newcommand\hiddencommacommand{, }
\forestset{%
  declare keylist register={split here ids},% the list of nodes to split the tree at
  split here ids={},
  declare keylist register={split here interjects},% the list of comments to put in between the tree parts
  split here interjects={},
  declare keylist={split here auto siblings}{},% a list to hold the siblings which need edge restoration
  declare toks register=split here toks,
  declare dimen register=tmpdima,
  tmpdima'=0pt,
  declare dimen register=tmpdimb,
  tmpdimb'=0pt,
  declare dimen register=tmpdimc,
  tmpdimc'=0pt,
  to widest/.style={
    tikz+={\path (\forestregister{tempdima}, \forestoption{y}) -- (\forestregister{tempdimb}, \forestoption{y});},
  },
  hide commas/.style={%
    split here toks+={\hiddencommacommand},
    split here toks+={#1},
  },
  split dir tree pre/.style={%
    label={[text=gray, anchor=north, font=\scriptsize]below:{[cont.]}{}},
  },
  split dir tree post/.style={%
    label={[font=\scriptsize, anchor=south, text=gray]above:{[cont.]}{}},
  },
  split dir tree auto post/.style={% this gets applied to the first node after a break
    split dir tree post,
    tempkeylistc'={},
    tmpdimb/.option=y,
    for nodewalk={
      while={
        > ORw2+d _+d < On=! & {y}{tmpdimb}{##2-##1} {\textheight-#1} {n'}{1}%
      }{
        next,
        tempkeylistc/.option=name
      }%
    }{},
    % save the list
    split here auto siblings/.register=tempkeylistc,
    tikz+/.process={% this tries to redraw the edges to the following siblings
      OOw2{edge}{id}%
      {%
        \path [##1] (!u.parent anchor |- .north) ++(\forestregister{folder indent},1ex) coordinate (before ##2) |- (.child anchor);
        \edef\tempa{\foresteoption{split here auto siblings}}
        \foreach \i in \tempa \path [##1] (before ##2) |- ({forest cs:\i.child anchor});
      }%
    },
  },
  split dir tree/.code={%
    \forestset{%
      draw tree stage/.style={
        for root'={
          tempdima/.min={%
            >OOw2+d{x}{min x}{####1+####2}%
          }{tree},
          tempdimb/.max={%
            >OOw2+d{x}{max x}{####1+####2}%
          }{tree},
          for tree={%
            to widest,
          },
        },
        tempcountb'=-1,
        do until={%
          strequal((split_here_ids),"")
        }{%
          tempkeylistb'={},
          tempkeylista'={},
          split register={split here ids}{,}{tempcounta,tempkeylistb+},
          split register={split here interjects}{,}{temptoksa,tempkeylista+},
          split here ids'/.register=tempkeylistb,
          split here interjects'/.register=tempkeylista,
        % Sašo Živanović: http://chat.stackexchange.com/transcript/message/28484520#28484520
         for nodewalk={%
           draw tree processing order/.style={%
             filter={tree}{> ORw+n< OR> & {id}{tempcounta}{########1+1}{id}{tempcountb}}%
           }%
         }{},
          for root'={draw tree},
          TeX/.process={Rw{temptoksa}{\otherhiddenparcommand ####1\hiddenparcommand}},
          tempcountb'/.register=tempcounta,
        },
        for nodewalk={%
          draw tree processing order/.style={%
            filter={tree}{>OR>{id}{tempcountb}}%
          }%
        }{},
        for root'={draw tree},
      },
    }%
  },
  split dir here auto/.style n args=2{%
    split dir tree pre,
    !next node.split dir tree auto post=#2,
    split here ids+/.option=id,
%     !next node.split resume here ids+/.option=id,
    split={#1}{,}{split here toks,hide commas},
    split here interjects/.register=split here toks,
  },
  split dir tree auto/.style={%
    split dir tree,
    before drawing tree={%
      tempdima/.max={y}{tree},
      tempdimc/.register=tempdima,
      tempdimd/.min={y}{tree},
      tempdima-/.register=tempdimd,
      tempdimb'=\textheight,
      tmpdima'=10ex,
      tmpdimc'=\pagetotal,
      while={%
        >RR>{tempdima}{tempdimb}%
      }{%
        for nodewalk={%
          root',
          until={%
            > ROw2+d RRw2+d > {tempdimc}{y}{##1-##2} {tmpdima}{tmpdimc}{\textheight-##2-##1}%
          }{next node},
          previous node,
          split dir here auto/.process={R_w2{tmpdima}{continued}{{##2}{##1}}},
          next node,
          tempdima/.option=y,
          tempdimc/.register=tempdima,
          tempdima-/.register=tempdimd,
          tmpdima'=15ex,
          tmpdimc'=0pt
        }{},
      },
    },
  },
}
\begin{document}
\blindtext

\begin{forest}
  for tree={
    folder,
    grow'=0,
    fit=band,
  },
  split dir tree auto,
  [ XYZ
      [ XYZ
          [ XYZ
              [ XYZ
                  [ XYZ ]
              ]
          ]
          [ XYZ
              [ XYZ
                  [ XYZ ]
              ]
          ]
          [ XYZ
              [ XYZ
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
              ]
              [ XYZ
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
              ]
              [ XYZ
                  [ XYZ ]
              ]
              [ XYZ
                  [ XYZ ]
                  [ XYZ ]
              ]
          ]
          [ XYZ
              [ XYZ
                  [ XYZ ]
                  [ XYZ ]
              ]
          ]
          [ XYZ
              [ XYZ
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
              ]
              [ XYZ
                  [ XYZ ]
              ]
              [ XYZ
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
              ]
          ]
          [ XYZ
              [ XYZ
                  [ XYZ ]
              ]
          ]
          [ XYZ
              [ XYZ
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
              ]
          ]
          [ XYZ
              [ XYZ
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
              ]
          ]
          [ XYZ
              [ XYZ
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
              ]
              [ XYZ
                  [ XYZ ]
                  [ XYZ ]
              ]
              [ XYZ
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
              ]
          ]
          [ XYZ
              [ XYZ
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
              ]
              [ XYZ
                  [ XYZ ]
              ]
              [ XYZ
                  [ XYZ ]
              ]
          ]
          [ XYZ
              [ XYZ
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
              ]
              [ XYZ
                  [ XYZ ]
              ]
          ]
          [ XYZ
              [ XYZ
                  [ XYZ ]
                  [ XYZ ]
              ]
          ]
          [ XYZ
              [ XYZ
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
              ]
              [ XYZ
                  [ XYZ ]
              ]
          ]
          [ XYZ
              [ XYZ
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
              ]
              [ XYZ
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
              ]
          ]
          [ XYZ
              [ XYZ
                  [ XYZ ]
              ]
              [ XYZ
                  [ XYZ ]
              ]
          ]
          [ XYZ
              [ XYZ
                  [ XYZ ]
              ]
          ]
          [ XYZ
              [ XYZ
                  [ XYZ ]
              ]
          ]
          [ XYZ
              [ XYZ
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
              ]
              [ XYZ
                  [ XYZ ]
              ]
          ]
          [ XYZ
              [ XYZ
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
              ]
              [ XYZ
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
              ]
              [ XYZ
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
              ]
              [ XYZ
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
              ]
          ]
          [ XYZ
              [ XYZ
                  [ XYZ ]
                  [ XYZ ]
              ]
          ]
          [ XYZ
              [ XYZ
                  [ XYZ ]
                  [ XYZ ]
              ]
          ]
          [ XYZ
              [ XYZ
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
                  [ XYZ ]
              ]
              [ XYZ
                  [ XYZ ]
              ]
          ]
      ]
  ]
\end{forest}

\end{document}

auto-split tree

Related Question