0

I am trying to write a quite big tree. I found out about the forest package, and was able to generate one. However, I have got some problems with the size of the whole thing, since I would like it to fit in one page but I have not I don't really get how to do it.

I found the first part of the code online and tried using it, but I don't know how to edit it in order to resize the tree.

\documentclass [12pt,a4paper,twoside]{book}
\usepackage{fontspec}
\usepackage{forest}
\usepackage[left=3cm,right=3cm, bottom=3cm]{geometry}
\setmainfont{Times New Roman}

\begin{document}
\forestset{
  dir node/.style={
    parent anchor=south west,
    child anchor=west,
    anchor=west,
    inner ysep=0pt,
    align=left,
  },
  dir tree/.style={
    for tree={
      grow'=0,
      dir node,
      edge path={
        \noexpand\path [draw, \forestoption{edge}] (!u.parent anchor) ++(1em,0) |- node[fill,inner sep=1.00pt] {} (.child anchor)\forestoption{edge label};
      },
      if n children=0{}{
        delay={
          prepend={[text 1, dir node, phantom, calign with current]}
        }
      },
      fit=band,
      before computing xy={
        l=2em,
      }
    },
  }
}

\begin{forest}
  dir tree,
  for tree = {
    font=\ttfamily
  }
  [EpisodioStagione <= 10.50
    [NEU <= 0.73
      [Personaggi <= 35.00
        [NEG <= 0.23
          [DurataScene <= 7.38
            [Durata <= 52.45
                [Classe: 8.0 - 8.2]
            ]
            [Durata >  52.45
                [Classe 8.3 - 8.5]
            ]
          ]
          [DurataScene >  7.38
            [Tagli <= 357.50
                [Tagli <= 241.50
                    [Classe: 7.7 - 7.9]
                ]
                [Tagli >  241.50
                    [Durata <= 42.34
                        [Parole <= 2155.00
                            [Episodio <= 39.00
                                [Classe: 8.3 - 8.5]
                            ]
                            [Episodio >  39.00
                                [Classe: 8.3 - 8.5]
                            ]
                        ]
                        [Parole >  2155.00
                            [Classe: 8.0 - 8.2]
                        ]
                    ]
                    [Durata >  42.34
                        [Classe: 8.0 - 8.2]
                    ]
                ]
            ]
            [Tagli >  357.50
                [Classe: 7.7 - 7.9]
            ]
          ]
        ]
        [NEG >  0.23
                [EpisodioStagione <= 1.50
                [Classe: 8.0 - 8.2]
            [EpisodioStagione >  1.50
                [NEU <= 0.62
                    [Tagli <= 291.50
                        [Classe: 8.3 - 8.5]
                    ]
                    [Tagli >  291.50
                        [Classe: 8.3 - 8.5]
                    ]
                ]
                [NEU > 0.62
                    [Classe: 8.3 - 8.5]
                ]
            ]
        ]
      ]
      [Personaggi >  35.00
        [Classe: 8.6 - 8.8]
      ]
    ]
    [NEU >  0.73
      [NEU <= 0.73
        [Classe: 8.6 - 8.8]
     ]
     [NEU >  0.73
        [Classe: 7.7 - 7.9]
     ]
    ]
   ]
   [EpisodioStagione >  10.50
    [RecensioniUomini <= 327.50
        [Classe: 8.3 - 8.5]
     ]
     [RecensioniUomini >  327.50
        [Classe 8.9 - 9.1]
     ]
    ]
   ]
\end{forest}
\end{document}

Also, the <= and >= combination of signs is not appearing in the tree. Is this normal? I choose this way of plotting a tree because it seemed the only solution of plotting such a huge tree, but maybe I am wrong. If anyone knows a better way of plotting it, please let me know! Any help is appreciated!

2
  • Please merge your code snippets in one small but complete document, which we can compile as it is. We don't know, which document class you use,, what is your document layout, which font size you use etc. Help us to help you.
    – Zarko
    Jun 26, 2022 at 1:04
  • @Zarko sure, thank you
    – Mauro
    Jun 26, 2022 at 2:01

1 Answer 1

0

You are facing the problem of how to put an elephant in a woman's purse, so there is not much to do. Some possibilities are:

  • enlarge the text area on page with this diagram or simply for complete document, for example by adding \usepackage[vmargin=22mm]{geometry} to document preamble
  • reduce font size in diagram to \small
  • reduce s sep to zero

Edit:

An example with locally changed page layout where is diagram:

\documentclass [12pt,a4paper,twoside]{book}
% preamble
\usepackage[hmargin=3cm, bottom=3cm]{geometry}
%---------------- show page layout. don't use in a real document!
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{0.15pt}
\renewcommand*\ShowFrameColor{\color{red}}
%---------------------------------------------------------------%
\usepackage{fontspec}
\usepackage{forest}
%\setmainfont{Times New Roman}
\usepackage{newtxtext, newtxmath}

% rest of preamble

%

\newgeometry{vmargin=20mm}
\begin{forest}
  dir tree,
  for tree = {
    font=\small\ttfamily,
    s sep=0pt
  }
% body of diagram
\end{forest}
\restoregeometry

By this changes your diagram can fit on one page:

enter image description here

(red lines indicate page layout)

Do you liked such result? I would consider about different way for presenting of diagram contents (it is still not clear to me, what you like to tell readers with this diagram).

6
  • Hello! Thank you very much for your help! You actually solved the problem, but sadly I forogot to mention that I am limited with \usepackage[left=3cm,right=3cm, bottom=3cm]{geometry}. The tree is respresenting a decision tree, a machine learning method for classification. The nodes are the different variables and the condition the values of the instances need to have to enter a specific path. The leafs are the output classes, the classification the predictor gives to an instance. I would love to represent the tree differently, but I thought it would be worsen the size of the elephant!
    – Mauro
    Jun 26, 2022 at 6:44
  • However, I tried setting the margins like you did (although I don't know what vmrgins are) but I am not able to run it... could it be that the first part of the code you removed is necessary? The compiler tells me that he does not know key dir tree.
    – Mauro
    Jun 26, 2022 at 7:02
  • 1
    vmargin, probably meaning vertical margins, denote top and bottom margins, see the manual in package geometry: mirror.marwan.ma/ctan/macros/latex/contrib/geometry/…
    – MS-SPO
    Jun 26, 2022 at 7:20
  • About tree alternatives: You could split the tree. E.g. the natural split which may come to mind is to replace the lower one "EpisodioStagione" by e.g. "more" and draw a second tree for "more". But in terms of clarity it may be better to single out "Personaggi". This way the major classifications are visually evident, and both trees show a similar amount of visual complexity. // Wrt. "Classe" there's also some redundancy, which may or may not be important. E.g. there is no counter-case sometimes, just one "alternative".
    – MS-SPO
    Jun 26, 2022 at 7:25
  • @Mauro, I edit my answer. Now the page, where is your diagram, locally enlarged. Other pages will preserve your original settings. About use of geometry package please read package documentation. To change your diagram ... i need first to understand, what text and in it means. For example, I guess, that you diagram will tell the same, if for example [Episodio > 39.00 [Classe: 8.3 - 8.5] change to `[Tagli; Classe: 7.7 - 7.9]`` )and similarly all similar end nodes. But this is very opinion based.
    – Zarko
    Jun 26, 2022 at 9:02

You must log in to answer this question.

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