调整森林绘图的大小

调整森林绘图的大小

我使用了以下代码此链接

原始版本\documentclass{standalone}。当我使用 时 {article},部分图表未显示。为了调整其大小,我尝试将其放入 中,{minipage}但我不知道如何调整其大小。

在此处输入图片描述

\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}

答案1

如果你愿意稍微增加页面宽度(只需加载geometry就足够了),那么调整l sep可以减少文件夹之间的距离。我还添加了一个修复删除虚假空格的问题,在 pgf 的未来版本中将不再需要这个。

\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}

在此处输入图片描述

如果加载geometryshowframe就会\usepackage[showframe]{geometry}发现树现在很容易适应。

在此处输入图片描述

答案2

不需要minipage。有两三种方法可以实现你想要的。

  • 在移动图表hspace*{0.5in}之前添加。\begin{forest}

  • 添加\footnotesize之前\begin{forest}以减小图表的尺寸。

  • 另一种容纳大型图形和表格的方法是旋转它们。请参考下面给出的示例。

\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-我添加了图形环境以用于标题和标签。

相关内容