3

I wish to top align a horizontal folder structure type of tree. However, top aligning does not produce a good result unfortunately.

I have taken some code from a previous post (Making a (simple) directory tree) to generate a folder structure type of tree. It looks perfectly aligned at the bottom as shown here:

beautifully aligned bottom

When I wish to topalign using either begin draw/.code={\begin{tikzpicture}[baseline=(current bounding box.north)]} or top align using \begin{adjustbox}{valign=t} I get:

ugly aligned top

Full code:

%% 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}
  • Welcome! Why are you using this old code when Forest now provides the edges library? That's easier, more flexible and less fragile. – cfr Aug 30 '18 at 00:22

2 Answers2

4

I would do something much simpler, taking advantage of current Forest's edges library and avoiding abuse of font. Also, it is less to type.

I wouldn't, personally, bother creating \fff unless you have tens to do, at least, because it is much clearer not to use it and hardly more typing. However, you can, of course, if you wish. Note that your original definition introduces spaces. I'm not sure if that's wanted or not, but I've omitted them here.

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

Double trouble:

two ways

cfr
  • 198,882
  • I don't see how this is less to type, I've added 6 bytes to the existing code, [edges] are 7 bytes. Your solution is more elegant and most likely better though :) Have my upvote. – Skillmon Aug 30 '18 at 06:13
  • The code looks lovely and clean! I'm a newby to forest, so this is great to know. Thanks! – Milo Buwalda Aug 31 '18 at 12:35
  • @Skillmon I didn't mean in comparison with what you'd added, but that using [edges] is less to type than the old way of doing it, which is in the OP's original code, to be sure. 'Less to type' is intended absolutely and not relatively. – cfr Sep 02 '18 at 00:23
  • @Skillmon Apparently, I can't upvote your answer as I've done it already. – cfr Sep 02 '18 at 00:24
3

Adding a \strut to the font does remedy the offset if all your contents are of less or equal height and depth than a \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}

enter image description here

Skillmon
  • 60,462