2

I used the following code from this link

The origianl \documentclass was {standalone}. When I used {article}, part of the graph was not displayed. To resize it, I tried putting it inside a {minipage} but I could not figure out how to resize it.

enter image description here

\documentclass{article}
\usepackage {tikz}
\usepackage{forest}
\usetikzlibrary{shadows}
\begin{document}
\forestset{
  dir switch tree 2/.style={
    for tree={
      if level=1{
        edge path={
          \noexpand\path [\forestoption{edge}] (!u.parent anchor) -- ++(0,-1.5em) -| (.child anchor)\forestoption{edge label};
        },
        for descendants={
          child anchor=west,
          align=left,
          edge path={
            \noexpand\path [\forestoption{edge}] (!u.parent anchor) -- ++(-.5em,0) |- (.child anchor)\forestoption{edge label};
          },
          before computing xy={
            l=.5em
          }
        },
        before computing xy={
          l=3em
        },
        for tree={
          parent anchor=west,
          anchor=mid west,
          grow'=0,
          font=\sffamily,
          if n children=0{}{
            delay={
              prepend={[,phantom, calign with current]}
            }
          },
        },
      }{},
    },
  },
}
\tikzset{
  colour me/.style={
      draw=colour#1,
      fill=colour#1!20!white,
      text=colour#1!25!black,
  }
}

\colorlet{colour0}{blue}
\colorlet{colour1}{green}
\colorlet{colour2}{red}
\colorlet{colour3}{orange}
\colorlet{colour4}{yellow}
\colorlet{colour5}{magenta}

\begin{minipage}{\textwidth}
{
\begin{forest}
  dir switch tree 2,
  for tree={
    if level=0{
      colour me=0
    }{
      if level=1{
        node options/.wrap pgfmath arg={colour me=#1}{n()}
      }{
        node options/.wrap 2 pgfmath args={colour me=#1!#2!black}{n("!u")}{100*(n_children("!u")-n()/2)/(n_children("!u"))}
      }
    },
    rounded corners,
    thick,
    drop shadow,
    edge={thick}
  }
  [Super Long Title With No Meaning
    [Criteria 1
      [Attribute\\Number 1]
      [Attribute 2]
      [Attribute 3]
      [Attribute 4]
      [Another\\Attribute]
    ]
    [Criteria 2
      [Attribute 1]
      [Attribute 2]
      [Third\\Criteria]
      [Longtext-\\criteria]
    ]
    [Criteria 3
      [Attribute\\two lines]
      [Attribute 2]
      [Attribute 3]
    ]
    [Criteria 4
      [Attribute 1]
      [Attribute 2]
    ]
    [Criteria 5
      [First\\Attribute]
      [Attribute 2]
      [Third\\Criteria]
      [Longtext-\\criteria]
    ]
  ]
\end{forest}
}
\end{minipage}
\end{document}
4
  • @cfr Thank you. I did not know about tex.stackexchange link. Would you tell me how to use edge path. How to add another levels without getting out of margins; still using portrait orientation. – Hany Dec 16 '19 at 4:46
  • 1
    Thanks for changing the link. I meant to say I'd use edge path' rather than edge path and so on as it is less faff. Do you mean you want to add a further branch horizontally e.g. 'criterion 6'? It is possible to make it fit in various ways. If you don't want to rotate it (which would be the obvious suggestion), you could make it smaller. What I would probably try first would be to move one of the branches below the others e.g. so criterion 6 was below 'another attribute', but with the edge still coming from the 'super long title ...'. However, turning it might be easier for readers. – cfr Dec 16 '19 at 6:05
  • 1
    But I wouldn't rotate the entire page. I'd just rotate the figure using e.g. sidewaysfigure from the rotating package. That way, your page still has its ordinary orientation (headers, footers etc.) and only the tree gets turned. But it is hard to know without more detail about what you're doing. – cfr Dec 16 '19 at 6:06
  • 1
    Some of what is taking the space is the background to the nodes. If you don't mind something less pretty, you could fit more criteria across the top. Or you can allow this tree to exceed the usual width of the text block, by temporarily altering the margins of the page. Or one of Schrödinger's cat's suggestions. The question is: what do you want to make different to make it fit? You have so much room and so much stuff: if you could have the code do anything at all, what would it do? – cfr Dec 16 '19 at 6:11
3

If you are willing to increase the page width a tiny bit (just loading geometry is sufficient), then adjusting l sep makes it possible to decrease the distance between the folders. I also added a fix that removes spurious spaces, in future versions of pgf this will no longer be needed.

\documentclass{article}
\usepackage{geometry}
\usepackage{forest}
\usetikzlibrary{shadows}
\makeatletter
% remove the stray space https://tex.stackexchange.com/a/513549
\patchcmd{\pgfutilsolvetwotwoleqfloat}
  { \noexpand\pgfmathfloatdivide@}
  {\noexpand\pgfmathfloatdivide@}
  {}{}
\makeatother
\begin{document}
\forestset{
  dir switch tree 2/.style={
    for tree={
      if level=1{
        edge path={
          \noexpand\path [\forestoption{edge}] (!u.parent anchor) -- ++(0,-1.5em) -| (.child anchor)\forestoption{edge label};
        },
        for descendants={
          child anchor=west,
          align=left,
          edge path={
            \noexpand\path [\forestoption{edge}] (!u.parent anchor) -- ++(-.5em,0) |- (.child anchor)\forestoption{edge label};
          },
          before computing xy={
            l=.5em
          }
        },
        before computing xy={
          l=3em,
        },
        for tree={
          parent anchor=west,
          anchor=mid west,
          grow'=0,
          font=\sffamily,
          if n children=0{}{
            delay={
              prepend={[,phantom, calign with current]}
            }
          },
        },
      }{},
    },
  },
}
\tikzset{
  colour me/.style={
      draw=colour#1,
      fill=colour#1!20!white,
      text=colour#1!25!black,
  }
}

\colorlet{colour0}{blue}
\colorlet{colour1}{green}
\colorlet{colour2}{red}
\colorlet{colour3}{orange}
\colorlet{colour4}{yellow}
\colorlet{colour5}{magenta}

\noindent
\begin{forest}
  dir switch tree 2,
  for tree={l sep=-2em,
    if level=0{
      colour me=0
    }{
      if level=1{
        node options/.wrap pgfmath arg={colour me=#1}{n()}
      }{
        node options/.wrap 2 pgfmath args={colour me=#1!#2!black}{n("!u")}{100*(n_children("!u")-n()/2)/(n_children("!u"))}
      }
    },
    rounded corners,
    thick,
    drop shadow,
    edge={thick},
  }
  [Super Long Title With No Meaning
    [Criteria 1
      [Attribute\\Number 1]
      [Attribute 2]
      [Attribute 3]
      [Attribute 4]
      [Another\\Attribute]
    ]
    [Criteria 2
      [Attribute 1]
      [Attribute 2]
      [Third\\Criteria]
      [Longtext-\\criteria]
    ]
    [Criteria 3
      [Attribute\\two lines]
      [Attribute 2]
      [Attribute 3]
    ]
    [Criteria 4
      [Attribute 1]
      [Attribute 2]
    ]
    [Criteria 5
      [First\\Attribute]
      [Attribute 2]
      [Third\\Criteria]
      [Longtext-\\criteria]
    ]
  ]
\end{forest}
\end{document}

enter image description here

If one loads geometry with showframe, \usepackage[showframe]{geometry}, one sees that the tree easily fits now.

enter image description here

5
  • Thank you for your solution. But what about adding one more level without getting out of margins; still using portrait orientation. That is why I would like to resize it. – Hany Dec 16 '19 at 4:48
  • 1
    @Hany You can only fit a certain number. If you add a few more criteria, you need either make the font smaller, the page wider, or both. This is not specific to forest, if you had a table with too many columns, you'd have the same constraints. – user194703 Dec 16 '19 at 4:50
  • Thank you for this clarification. If I would like to add more criteria; What should I use other than forest. – Hany Dec 16 '19 at 4:58
  • 1
    @Hany As I said, this is not specific to forest. If you want to have many columns, you need to have space for many columns. You can make the font smaller and/or decrease the text width. You can also squeeze the above tree a bit more. Usually if you add too much information, these diagrams or trees lose their purpose because one can no longer understand what they want to tell us. – user194703 Dec 16 '19 at 5:02
  • Thank you for your suggestions. – Hany Dec 16 '19 at 5:04
1

No need of minipage. There are two three ways of achieving what you want.

  • Add hspace*{0.5in} before \begin{forest} to shift the diagram.

  • Add \footnotesize before \begin{forest} to reduce the size of the diagram.

  • Another way to accommodate large figures and tables is rotating them. Please refer to the example given below.

\documentclass{article}
\usepackage{lscape}
\usepackage {tikz}
\usepackage{forest}
\usetikzlibrary{shadows}

\begin{document}
\forestset{
  dir switch tree 2/.style={
    for tree={
      if level=1{
        edge path={
          \noexpand\path [\forestoption{edge}] (!u.parent anchor) -- ++(0,-1.5em) -| (.child anchor)\forestoption{edge label};
        },
        for descendants={
          child anchor=west,
          align=left,
          edge path={
            \noexpand\path [\forestoption{edge}] (!u.parent anchor) -- ++(-.5em,0) |- (.child anchor)\forestoption{edge label};
          },
          before computing xy={
            l=.5em
          }
        },
        before computing xy={
          l=3em
        },
        for tree={
          parent anchor=west,
          anchor=mid west,
          grow'=0,
          font=\sffamily,
          if n children=0{}{
            delay={
              prepend={[,phantom, calign with current]}
            }
          },
        },
      }{},
    },
  },
}
\tikzset{
  colour me/.style={
      draw=colour#1,
      fill=colour#1!20!white,
      text=colour#1!25!black,
  }
}

\colorlet{colour0}{blue}
\colorlet{colour1}{green}
\colorlet{colour2}{red}
\colorlet{colour3}{orange}
\colorlet{colour4}{yellow}
\colorlet{colour5}{magenta}
\begin{landscape}
\begin{figure}[h!]
\centering
    \begin{forest}
        dir switch tree 2,
        for tree={
            if level=0{
                colour me=0
            }{
            if level=1{
                node options/.wrap pgfmath arg={colour me=#1}{n()}
            }{
            node options/.wrap 2 pgfmath args={colour me=#1!#2!black}{n("!u")}{100*(n_children("!u")-n()/2)/(n_children("!u"))}
        }
    },
    rounded corners,
    thick,
    drop shadow,
    edge={thick}
}
[Super Long Title With No Meaning
[Criteria 1
[Attribute\\Number 1]
[Attribute 2]
[Attribute 3]
[Attribute 4]
[Another\\Attribute]
]
[Criteria 2
[Attribute 1]
[Attribute 2]
[Third\\Criteria]
[Longtext-\\criteria]
]
[Criteria 3
[Attribute\\two lines]
[Attribute 2]
[Attribute 3]
]
[Criteria 4
[Attribute 1]
[Attribute 2]
]
[Criteria 5
[First\\Attribute]
[Attribute 2]
[Third\\Criteria]
[Longtext-\\criteria]
]
]
\end{forest}
\caption{My figure}
\label{one}
\end{figure}
\end{landscape}
\end{document}

PS - I've added figure environment for caption and labeling.

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.