文件夹树看起来像森林,需要在顶部对齐

文件夹树看起来像森林,需要在顶部对齐

我希望将水平文件夹结构类型的树顶部对齐。但不幸的是,顶部对齐无法产生良好的效果。

我从之前的帖子中摘取了一些代码(制作(简单)目录树) 生成树状文件夹结构。它在底部看起来完美对齐,如下所示:

底部精美对齐

当我希望使用 begin draw/.code={\begin{tikzpicture}[baseline=(current bounding box.north)]} 或 顶部对齐使用 进行顶部对齐时,\begin{adjustbox}{valign=t}我得到:

顶部对齐丑陋

完整代码:

%% Compile and read me!
\documentclass[a4paper,12pt]{article}
\usepackage{forest}

\newcommand{\fff}[1]{%\begin{adjustbox}{valign=t}
\begin{forest}
  for tree={
    font=\ttfamily,
    s sep=.5em,
    inner sep=1,
    grow'=0,
    child anchor=west,
    parent anchor=south,
    anchor=west,
%     node distance=1.2cm,
    calign=first,
%   align=top,
    edge path={
      \noexpand\path [draw, \forestoption{edge}]
      (!u.south west) +(7.5pt,0) |- node[fill,inner sep=1.25pt] {} (.child anchor)\forestoption{edge label};
    },
    before typesetting nodes={
      if n=1
        {insert before={[,phantom]}}
        {}
    },
    fit=band,
    before computing xy={l=15pt},
    begin draw/.code={\begin{tikzpicture}[baseline=(current bounding box.north)]}
  }
#1\end{forest}
}

\begin{document}

\begin{figure}
\centering
% \begin{tabular}{ccc}
% \fff{ [Forward ]}&
% \fff{ [repeat [Forward ] ]}&
% \fff{ [if \textit{PathAhead} [Forward] [TurnLeft] ]}
% \end{tabular}
\fff{ [Forward ]}
\fff{ [repeat [Forward ] ]}
\fff{ [if \textit{PathAhead} [Forward] [TurnLeft] ]
}
\caption{Partial correct programs}
\label{tree:partial-correct}
\end{figure}

\end{document}

答案1

如果所有内容的高度和深度都小于或等于 a ,则\strut在 a 中添加 a可以解决偏移问题:font\strut

%% Compile and read me!
\documentclass[a4paper,12pt]{article}
\usepackage{forest}

\newcommand{\fff}[1]{%\begin{adjustbox}{valign=t}
\begin{forest}
  for tree={
    font=\strut\ttfamily,
    s sep=.5em,
    inner sep=1,
    grow'=0,
    child anchor=west,
    parent anchor=south,
    anchor=west,
%     node distance=1.2cm,
    calign=first,
%   align=top,
    edge path={
      \noexpand\path [draw, \forestoption{edge}]
      (!u.south west) +(7.5pt,0) |- node[fill,inner sep=1.25pt] {} (.child anchor)\forestoption{edge label};
    },
    before typesetting nodes={
      if n=1
        {insert before={[,phantom]}}
        {}
    },
    fit=band,
    before computing xy={l=15pt},
    begin draw/.code={\begin{tikzpicture}[baseline=(current bounding box.north)]}
  }
#1\end{forest}
}

\begin{document}

\begin{figure}
\centering
% \begin{tabular}{ccc}
% \fff{ [Forward ]}&
% \fff{ [repeat [Forward ] ]}&
% \fff{ [if \textit{PathAhead} [Forward] [TurnLeft] ]}
% \end{tabular}
\fff{ [Forward ]}
\fff{ [repeat [Forward ] ]}
\fff{ [if \textit{PathAhead} [Forward] [TurnLeft] ]
}
\caption{Partial correct programs}
\label{tree:partial-correct}
\end{figure}

\end{document}

在此处输入图片描述

答案2

我会做一些更简单的事情,利用当前的 Forestedges库并避免滥用font。此外,输入的内容也更少。

就我个人而言,除非您有数十个任务要做,否则我不会费心创建它\fff,因为不使用它更清楚,而且几乎不需要输入任何内容。但是,如果您愿意,当然可以。请注意,您的原始定义引入了空格。我不确定这是否是想要的,但我在这里省略了它们。

\documentclass[]{standalone}
\usepackage[edges]{forest}

\forestset{
  fff/.style={
    for tree={folder, grow'=0, delay={+content=\strut}, edge label={node [midway, inner sep=1.25pt, fill] {}}, font=\ttfamily},
    baseline=t
  }
}
\newcommand\fff[1]{\Forest{fff#1}}% personally, I wouldn't bother with this
\begin{document}

\Forest{fff[Forward]}
\Forest{fff[repeat[Forward]]}
\Forest{fff[if \textit{PathAhead} [Forward][TurnLeft]]}

% if you must
\fff{ [Forward ]}
\fff{ [repeat [Forward ] ]}
\fff{ [if \textit{PathAhead} [Forward] [TurnLeft] ]}

\end{document}

双重麻烦:

两种方式

相关内容