使包含大量文本的森林树变得更窄

使包含大量文本的森林树变得更窄

我无法让一forest棵树适合整个页面。我想知道有什么技巧可以挤压下面的树,这样它就不会占用太多的水平空间。我的想法(但无法执行)是:

1)强制节点具有最大宽度(因此文本流动到多行)

2)允许树中同一级别的节点在页面上具有不同的垂直位置(以便它们可以重叠)。

任何建议都将不胜感激!如果我在这种情况下使用了完全错误的包,请告诉我。

\documentclass[a4paper,12pt]{article}

\usepackage{forest}

\begin{document}

\begin{forest}
    [What could prevent a transaction?
        [Customer can't top up
            [Customer has insufficient funds
                [Seasonal/short term issue]
                [Poor customer selection]
                [Change in circumstances]
            ]
            [Can't execute top up
                [Customer doesn't know where to go]
                [Top up process unclear to customer]
                [Top up merchant does not know the process]
                [Merchant fraud]
                [Agent fraud]
            ]
        ]
        [Customer doesn't want to top up
            [Dissatisfied with unit
                [Performance below expectations
                    [Unit degradation]
                    [Poor installation]
                ]
                [Unresolved maintenance issue]
                [Unit stolen or vandalised]
            ]
        ]
    ]
\end{forest}

\end{document}

编辑: 对于将来遇到此问题的人,以下是我认为最有用的答案。对于经验丰富的森林使用者来说,这些答案可能很明显,抱歉...

1)水平而不是垂直地生长一棵树,在之后使用此代码\begin{forest}

for tree={
child anchor=west,
parent anchor=east,
grow'=east}

2)控制文本宽度,只需在上面提到的text width=内容中添加即可for tree={}

3)为节点创建单独的样式:(name/.style={}参见cfr的答案)

4) 改变单个节点的对齐方式,使得它相当于树中的不同级别:放入空白行(即嵌套在内[ ])。

非常感谢您提供的非常有帮助的回复。

答案1

换行没问题,但说实话,如果在同一层级上有这么多水平信息,垂直换行会很困难。我建议水平布局,而且你还得选择较小的字体或稍微缩放一下。也许像下面这样:

\documentclass[a4paper,12pt]{article}

\usepackage{forest}

\begin{document}

\resizebox{\textwidth}{!}{%
\begin{forest}
  for tree={
    child anchor=west,
    parent anchor=east,
    grow'=east,
  %minimum size=1cm,%new possibility
  text width=4cm,%
    draw,
    anchor=west,
    edge path={
      \noexpand\path[\forestoption{edge}]
        (.child anchor) -| +(-5pt,0) -- +(-5pt,0) |-
        (!u.parent anchor)\forestoption{edge label};
    },
  }
    [What could prevent a transaction?
        [Customer can't top up
            [Customer has insufficient funds
                [Seasonal/short term issue]
                [Poor customer selection]
                [Change in circumstances]
            ]
            [Can't execute top up
                [Customer doesn't know where to go]
                [Top up process unclear to customer]
                [Top up merchant does not know the process]
                [Merchant fraud]
                [Agent fraud]
            ]
        ]
        [Customer doesn't want to top up
            [Dissatisfied with unit
                [Performance below expectations
                    [Unit degradation]
                    [Poor installation]
                ]
                [Unresolved maintenance issue]
                [Unit stolen or vandalised]
            ]
        ]
    ]
\end{forest}
}%
\end{document}

在此处输入图片描述

答案2

我并不是说这样做很明智,但是如果您使用风景并稍微重新考虑一下,您就可以垂直呈现这棵树。

例如:

\documentclass[a4paper,12pt]{article}
\usepackage{forest,pdflscape}
\pagestyle{empty}
\begin{document}
\forestset{
  skip one/.style={
    replace by={
      [,
        shape=coordinate,
        l*=.25,
        append
      ]
    },
  },
  customer/.style={
    content={Customer},
    draw=red,
    inner color=red!10,
    outer color=red!20,
    for descendants={
      draw=red,
      inner color=red!10,
      outer color=red!20,
    },
    before drawing tree={
      for descendants={
        edge={draw=red}
      },
    }
  },
  merchant/.style={
    content={Merchant},
    draw=blue,
    inner color=blue!10,
    outer color=blue!20,
    for descendants={
      draw=blue,
      inner color=blue!10,
      outer color=blue!20,
    },
    before drawing tree={
      for descendants={
        edge={draw=blue}
      }
    },
  },
  agent/.style={
    content={Agent},
    draw=green!50!black,
    inner color=green!50!black!10,
    outer color=green!50!black!20,
    for descendants={
      draw=green!50!black,
      inner color=green!50!black!10,
      outer color=green!50!black!20,
    },
    before drawing tree={
      for descendants={
        edge={draw=green!50!black}
      },
    }
  },
  nobody/.style={
    for descendants={
      draw=black,
      inner color=gray!10,
      outer color=gray!20,
    },
    before drawing tree={
      for descendants={
        edge={draw=black}
      },
    }
  },
}
\begin{landscape}
\noindent
\begin{forest}
    for tree={
      parent anchor=south,
      child anchor=north,
      draw,
      thick,
      font=\small\sffamily,
      align=center,
      delay={
        where content={}{
          shape=coordinate,
        }{},
      },
    },
    before packing={
      for tree={
        if={isodd(n_children())}{
          for children={
            if={equal(n,int((1+n_children("!u"))/2))}{
              calign with current,
            }{},
          },
        }{},
        edge path={
          \noexpand\path [draw, thick, \forestoption{edge}] (!u.parent anchor) -- ++(0,-5pt) |- ($(.child anchor) + (0,5pt)$) -- (.child anchor)\forestoption{edge label};
        },
      },
    },
    [What could prevent a transaction?, inner color=gray!10, outer color=gray!20,
      [, customer
        [Can't top up
            [Insufficient funds
                [Seasonal/short term issue, skip one, l*=2]
                [Poor selection, skip one]
                [Changed circumstances, skip one, l*=4]
            ]
            [Unable to top up
              [, customer
                [Process unclear, skip one]
                [Unsure where to go, skip one, l*=2]
              ]
              [, merchant
                [Process unfamiliar
                ]
                [Fraud
                ]
              ]
              [, agent
                [Fraud]
              ]
            ]
        ]
        [Doesn't want to top up
            [Dissatisfied with unit, nobody
              [, l*=2.5
                [Performance below expectations, skip one, l*=2
                    [Unit degradation]
                    [Poor installation]
                ]
                [Unresolved maintenance issue, skip one]
                [Unit stolen or vandalised, skip one, l*=2]
              ]
            ]
        ]
      ]
    ]
\end{forest}
\end{landscape}
\end{document}

反思树

相关内容