文件系统树形图显示奇怪的迭代行为

文件系统树形图显示奇怪的迭代行为

我在TeXample.net
我根据相同的代码制作了右边的那个。我希望我可以编写一个脚本来映射一个真正的文件系统。我决定从小处着手。我尝试用一​​个\foreach循环重新创建它,遍历名称列表,但发生了一些奇怪的事情,它最终看起来像第三个(下面)我不明白为什么它会这样嵌套。 在此处输入图片描述

\documentclass{minimal}
\usepackage{tikz}
%%%<
\usepackage{verbatim}
\usepackage[active,tightpage]{preview}
\PreviewEnvironment{tikzpicture}
\setlength\PreviewBorder{5pt}%
%%%>
\usetikzlibrary{trees}
\begin{document}
\tikzstyle{every node}=[draw=black,thick,anchor=west]
\tikzstyle{selected}=[draw=red,fill=red!30]
\tikzstyle{optional}=[dashed,fill=gray!50]
\begin{tikzpicture}[%
  grow via three points={one child at (0.5,-0.7) and
  two children at (0.5,-0.7) and (0.5,-1.4)},
  edge from parent path={(\tikzparentnode.south) |- (\tikzchildnode.west)}]
  \node {texmf}
    child { node {doc}}     
    child { node {fonts}}
    child { node {source}}
    child { node [selected] {tex}
      child { node {generic}}
      child { node [optional] {latex}}
      child { node {plain}}
    }
    child [missing] {}              
    child [missing] {}              
    child [missing] {}              
    child { node {texdoc}};
\end{tikzpicture}
\end{document}

\begin{tikzpicture}[%
  grow via three points={one child at (0.5,-0.7) and two children at (0.5,-0.7) and (0.5,-1.4)}, 
  edge from parent path={(\tikzparentnode.south) |-(\tikzchildnode.west)}]
  \node {/}
    child { node {bin}} 
    child { node {dev}} 
    child { node {etc}}
    child { node {home}}    
    child { node {lib}}
    child { node {media}}   
    child { node {mnt}} 
    child { node {opt}} 
    child { node {proc}}
    child { node {root}}    
    child { node {sbin}};
\end{tikzpicture}

\begin{tikzpicture}[%
  grow via three points={one child at (0.5,-0.7) and two children at (0.5,-0.7) and (0.5,-1.4)},
  edge from parent path={(\tikzparentnode.south) |-(\tikzchildnode.west)}]
  \node {/} \foreach \x in {bin,dev,etc,home,lib,media,mnt,opt,proc,root,sbin}{
    child { node {\x}}};
\end{tikzpicture}

答案1

也许

\node{/} \foreach \x in {bin,dev,etc,home,lib,media,mnt,opt,proc,root,sbin}{
    child { node {\x}}};
\node {/} child [] foreach \name in {bin,dev,etc,home,lib,media,mnt,opt,proc,root,sbin} 
     {node {\name}};

更多内容请参阅 TikZ 手册第 21 章。完整工作示例:

\documentclass{minimal}
\usepackage{tikz}
%%%<
\usepackage{verbatim}
\usepackage[active,tightpage]{preview}
\PreviewEnvironment{tikzpicture}
\setlength\PreviewBorder{5pt}%
%%%>
\usetikzlibrary{trees}
\begin{document}
\tikzstyle{every node}=[draw=black,thick,anchor=west]
\tikzstyle{selected}=[draw=red,fill=red!30]
\tikzstyle{optional}=[dashed,fill=gray!50]
\begin{tikzpicture}[%
  grow via three points={one child at (0.5,-0.7) and
  two children at (0.5,-0.7) and (0.5,-1.4)},
  edge from parent path={(\tikzparentnode.south) |- (\tikzchildnode.west)}]

  \node {/} child [] foreach \name in {bin,dev,etc,home,lib,media,mnt,opt,proc,root,sbin} 
      {node {\name}};

\end{tikzpicture}
\end{document}

答案2

这只是提到可以使用forest。如果您确实需要将其包装在列表中,也可以这样做。

\documentclass[border=3mm]{standalone}
\usepackage[edges]{forest}
\makeatletter
\newcommand{\myforwrapper}[1]{[#1]}
\newcommand{\myfor}[2]{%
\edef#2{}%
\pgfutil@for\my@item:={#1}\do{%
\edef#2{#2\myforwrapper{\my@item}}%
}}
\newcommand{\MyDirectoryTree}[2][]{%
\myfor{#2}{\temp}%
\edef\temp{\noexpand\begin{forest}
for tree={grow'=east,folder,draw}
[/
\temp
]
\noexpand\end{forest}}%
\temp}
\makeatother
\begin{document}
\MyDirectoryTree{bin,dev,etc,home,lib,media,mnt,opt,proc,root,sbin}
\end{document}

在此处输入图片描述

相关内容