编辑

编辑

我正在寻找一种方法来创建文件格式规范的树形图,例如在 Minecraft Wiki 中。我尝试用 Forest 创建它,是我目前所拥有的。此解决方案工作正常,但我的大描述在文档的右端被截断。我尝试在 Forest 中添加换行符,但尚未实现。有没有办法在 LaTeX 中制作这样的图表(我不关心使用的软件包)?

我必须使用 LaTeX,因为可以在图表文本中添加链接和引用。

答案1

这是另一种不依赖于使用标签的方法。后者只有在标签被限制在一行时才会真正正常工作,因为 Forest 在打包树时无法容纳它们。对于短标签,它们在这种树中最为方便,但对于较长的标签,在我看来,它们不是最佳选择。

  • minecraft schematic设置树的样式;
  • icon向节点添加默认文件图标;
  • icon=<style><style>在节点中添加文件图标。

作为示例,我使用默认样式,可以通过更改icon/style/.styleicon/main/.styleicon/dim/.style来重新定义icon/elk/.style

你也可以选择麋鹿蓝

\documentclass[tikz,border=10pt,multi,dvipsnames,svgnames,x11names]{standalone}
\usepackage[edges]{forest}
\usepackage{calc}
\forestset{
  declare toks={my label}{},
  declare toks={my details}{},
  declare boolean={my icon}{0},
  minecraft schematic/.style={% addaswyd o ateb: http://tex.stackexchange.com/a/340354/
    for tree={% addaswyd o gôd Gonzalo Medina: http://tex.stackexchange.com/a/270761/
      folder,
      font=\sffamily,
      grow'=0,
      text width=75mm,
    },
    before typesetting nodes={
      for tree={
        split option={content}{:}{my label,my details},
        delay={
          content/.process={On=?_OOw3}{my icon}{1}{\makebox[\iconwidth+\iconmargin]{}}{}{my label}{my details}{##1\textbf{##2:} ##3},
        },
      },
    },
  },
  icon/.style={
    my icon,
    tikz+={
      \pic at ([xshift=\iconmargin,yshift=-.1*\baselineskip].north west) {my file={#1}};
    },
    edge path'/.expanded={% addaswyd o forest-lib-edges.sty
      ([xshift=\forestregister{folder indent}]!u.parent anchor) |- ([xshift=-.5*\iconmargin,yshift=-.5*\iconheight].north west)
    },
  }
}
\tikzset{
  my file/.pic={
    \draw [icon/.cd, style, #1] (0,-\iconheight) |- +([xshift=-.2*\iconwidth]\iconwidth,\iconheight) edge +([yshift=-.2*\iconwidth]\iconwidth,\iconheight) |- +([yshift=-.2*\iconwidth]\iconwidth,\iconheight) |- cycle;
  },
  icon/.search also={/tikz},
  icon/.cd,
  width/.store in=\iconwidth,
  height/.store in=\iconheight,
  margin/.store in=\iconmargin,
  style/.style={fill=gray!50!blue!25},
  width=7.5pt,
  height=10pt,
  margin=2.5pt,
  main/.style={inner color=white, outer color=red},
  dim/.style={fill=gray!25},
  elk/.style={top color=blue, bottom color=blue, middle color=cyan},
}
\begin{document}
\begin{forest}
  minecraft schematic,
  where level<=2{icon}{},
  [Schematic:Schematic data., icon=main
    [Various:Things
      [Other:Various things
        [Dimensions:{All dimensions for everything in the entire universe should be stored, one per line, here.
          Authors should ensure that dimensions are listed in ascending order.
          Partial submissions will not be accepted.}, for tree={icon=dim}
          [tex:t1.1.1.1.1]
          [tex:t1.1.1.1.2]
          [tex:t1.1.1.1.3]
        ]
      ]
      [tex:t1.1.2
        [tex:t1.1.2.1]
      ]
      [tex:t1.1.3
        [tex:t1.1.3.1]
      ]
    ]
    [Elks:Many elks are quite friendly once you get to know them., for tree={icon=elk}
      [Faint praise:Damning with]
      [Elk blues:Moose jazz]
    ]
  ]
\end{forest}
\end{document}

编辑

请注意,问题中的适当示例可以阐明您想要什么,并避免浪费大量时间和精力。您说您尝试调整上述代码并遇到了问题,但您没有提供您尝试过的细节,也没有提供关于您的需求的最少附加信息。

以下内容可能与您尝试过的一样,也可能不一样,可能是您想要的,也可能不是您想要的,可能有用,也可能没用。真的,我不知道。

我们添加了额外的森林风格icon square img

  icon square img/.style={
    my icon,
    tikz+={
      \node [anchor=north west] at ([xshift=-.5*\iconmargin,yshift=.2*\iconwidth].north west) {\includegraphics[width=\iconwidth]{example-grid-100x100pt}};
    },
    edge path'/.expanded={% addaswyd o forest-lib-edges.sty
      ([xshift=\forestregister{folder indent}]!u.parent anchor) |- ([xshift=-.5*\iconmargin,yshift=-.5*\iconwidth].north west)
    },
  },

然后我们可以调整原始树以使用新样式。

\begin{forest}
  minecraft schematic,
  where level<=2{icon square img}{},
  /tikz/icon/width=10pt,
  [Schematic:Schematic data.
    [Various:Things
      [Other:Various things
        [Dimensions:{All dimensions for everything in the entire universe should be stored, one per line, here.
          Authors should ensure that dimensions are listed in ascending order.
          Partial submissions will not be accepted.}
          [tex:t1.1.1.1.1]
          [tex:t1.1.1.1.2]
          [tex:t1.1.1.1.3]
        ]
      ]
      [tex:t1.1.2
        [tex:t1.1.2.1]
      ]
      [tex:t1.1.3
        [tex:t1.1.3.1]
      ]
    ]
    [Elks:Many elks are quite friendly once you get to know them.
      [Faint praise:Damning with]
      [Elk blues:Moose jazz]
    ]
  ]
\end{forest}

用外部图像替代

我注意到,你链接到其他地方的示例中没有一个图标是正方形的。每个图标都是标准的文件形状图标。图标中的文件永远不会是正方形的,即使它们包含的图标是正方形的。因此,我甚至不认为我需要我的水晶球,它在最好的情况下也是不可靠的。

答案2

如果描述不会超过几行(这样树布局就不必更改),您只需添加text width=3cm(或其他宽度)即可my label/.style

假设我们改变以下两件事在此代码中:我们替换

  my label/.style={
    label={[font=\sffamily]right:{#1}},
  },

经过

  my label/.style={
    label={[font=\sffamily,text width=3cm]right:{#1}},
  },

我们替换

      [tex:t1.1.1.1.1]

经过

      [tex:t1.1.1.1.1 long long long long long text]

然后我们得到:

在此处输入图片描述

答案3

您可以在节点标签内使用 添加换行符align=left,更多详细信息可在此处找到回答

完整代码

\documentclass[tikz,border=10pt,multi,dvipsnames,svgnames,x11names]{standalone}
\usepackage[edges]{forest}

\begin{document}
\begin{forest}
  my label/.style={
    label={[font=\sffamily,align=left]right:{#1}},% align=left added
  },
  for tree={% 
    folder,
    font=\sffamily,
    text=white,
    minimum height=0.75cm,
    if level=0{fill=ForestGreen}{fill/.wrap pgfmath arg={SlateBlue#1}{int(4-(mod((level()-1),4)))}},
    rounded corners=4pt,
    grow'=0,
    edge={ForestGreen,rounded corners,line width=1pt},
    fit=band,
  },
  before typesetting nodes={
    for tree={
      split option={content}{:}{content,my label},
    }
  }
  [tex:{t1\\ bla bla bla}
    [tex:t1.1
      [tex:t1.1.1
        [tex:t1.1.1.1
          [tex:t1.1.1.1.1]
          [tex:t1.1.1.1.2]
          [tex:t1.1.1.1.3]
        ]
      ]
      [tex:t1.1.2
        [tex:t1.1.2.1]
      ]
      [tex:t1.1.3
        [tex:t1.1.3.1]
      ]
    ]
    [tex:t1.2
      [tex:t1.2.1]
      [tex:t1.2.2]
    ]
  ]
\end{forest}
\end{document}

相关内容