forest
forest copied to clipboard
Q: structure like `folder` but with whole subtrees wrapped in boxes/nodes ?
Hello,
this package is great, thanks for making it!
I was wondering if it is possible to make a folder-like style but where the whole (sub)folder trees are boxed instead of just directory names. In ascii, something like:
.---------------------.
| folder |
| \ .----------------.|
| |\| subfolder ||
| | | \ .-----------.||
| | | |\| subfolder |||
| | | | | |||
| | | | '-----------'||
| | | \ .-----------.||
| | | \| subfolder |||
| | | | |||
| | | '-----------'||
| | '----------------'|
| \ .----------------.|
| \| anotherfolder ||
| '----------------'|
'---------------------'
Thanks for any help/hint! -mk
Can you provide an example to work with? This should be pretty straightforward using fit.
Hello,
yeah, I thought about something like this:
\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning, fit}
\begin{document}
\begin{tikzpicture}
\pgfdeclarelayer{background}
\pgfsetlayers{background,main}
\def\wrap#1#2{\begin{pgfonlayer}{background}\node[rounded corners=1ex, fit=#1, #2] {}; \end{pgfonlayer}}
\tikzstyle{bg}=[fill=cyan, inner sep=0pt, fill opacity=0.1, draw=cyan!50!black, draw opacity=0.2];
\node (/) {(root)};
\node[anchor=north west] (/a) at ([xshift=1em] /.south west) {test};
\node[anchor=north west] (/a/a) at ([xshift=1em] /a.south west) {something};
\node[anchor=north west] (/a/b) at (/a/a.south west) {something else};
\wrap{(/a/a)(/a/b)}{bg}
\wrap{(/a)(/a/a)(/a/b)}{bg}
\node[anchor=north west] (/b) at ([xshift=1em] /.south west |- /a/b.south) {folder};
\node[anchor=north west] (/b/a) at ([xshift=1em] /b.south west) {subfolder};
\wrap{(/b/a)}{bg}
\wrap{(/b)(/b/a)}{bg}
\wrap{(/)(/a)(/a/a)(/a/b)(/b)(/b/a)}{bg}
\end{tikzpicture}
\end{document}
..gives:
as the main issue, I don't know how to add proper margin&padding computation there (ideally, the background boxes would have a non-zero inner sep, but that breaks rendering at this point). I think that forest already has the necessary tools but not really sure how to use them.
(Also, my way of making the colors is suboptimal -- ideally one could select these per level or so -- but that can be done manually with much less hassle than the positioning.)
I was thinking of a Forest example rather than something completely different ....
Proof-of-concept. Sašo will provide something neater, smarter and quicker if he ever has time for Forest :-). I may try to tidy it up a bit later and do something with a colour series, but it does basically work.
Note this produces lurid, solid colours for testing ;).
\documentclass{standalone}
\usepackage[edges]{forest}
\usetikzlibrary{backgrounds}
\tikzset{%
box style/.style={%
draw=boxcolour,
rounded corners,
fill=boxcolour,
fill opacity=1,
draw opacity=1,
#1,
},
box colour/.code={%
\colorlet{boxcolour}{#1}%
},
box colour=black,
}
\forestset{%
declare toks={box colour}{black},
boxed dirs/.style={%
% am I using this wrong?
% draw tree tikz processing order/.nodewalk style=tree breadth-first,
for tree={
folder,
grow'=0,
s sep'+=2.5pt,
before typesetting nodes={%
tempcounta/.max={level}{tree},
if n children=0{%
/tikz/box colour/.option=box colour,
box style,
}{%
tikz+/.process={ORw2+nOw2{level}{tempcounta}{##2-##1}{box colour}
{%
\begin{scope}[on background layer]
\node [box style={box colour=##2,inner sep={##1*2.5pt}},fit to=tree] {};
\end{scope}
}%
},
},
},
},
},
}
\colorlet{colour0}{magenta}
\colorlet{colour1}{blue}
\colorlet{colour2}{cyan}
\begin{document}
\begin{forest}
boxed dirs,
for tree={box colour/.process={Ow{level}{colour#1}}},
[(root)
[test
[something]
[something else]
]
[folder
[subfolder]
]
]
\end{forest}
\end{document}