3

I'm trying to save space in diagrams, so I want to know if there's an easy way to make a forest tree folder grow to right and left sides as seen in the image below

enter image description here

Currently I have this but I've no idea on how to manipulate growing of forest.

\documentclass{article}

\usepackage[letterpaper,margin=20mm]{geometry}

\usepackage{tikz} \usepackage[edges]{forest} \usepackage{xcolor}

\usetikzlibrary{shapes.geometric, arrows.meta, calc, patterns, patterns.meta, shadows}

\pgfkeys{/forest, rect/.append style={rectangle, rounded corners=2pt, /tikz/align=center}, }

\definecolor{background}{RGB}{239, 239, 239} \definecolor{lightgreen}{RGB}{150, 240, 180} \definecolor{green}{RGB}{146, 208, 80} \definecolor{blue}{RGB}{4, 121, 181} \definecolor{linecol}{RGB}{92, 92, 92} \definecolor{lightpink}{RGB}{245, 160, 240} \definecolor{lightblue}{RGB}{176, 221, 255} \definecolor{lilac}{RGB}{174, 182, 211}

\begin{document} \begin{figure} \centering \begin{forest} for tree={ line width=0.5pt, draw=linecol, rect, calign=center, minimum size=25pt, text width=50mm, rounded corners=5pt, child anchor=north, parent anchor=south, drop shadow, l sep+=12.5pt, inner color=background, anchor=center, edge path={ \noexpand\path[color=linecol, rounded corners=5pt,>={Stealth[length=10pt]}, line width=0.5pt, ->, \forestoption{edge}] (!u.parent anchor) -- +(0,-5pt) -| (.child anchor)\forestoption{edge label}; }, if = {level == 1}{outer color=lightblue, text width=38mm}{}, if = {level == 2}{ outer color=lightgreen, for tree = { text width=28mm, child anchor=west } }{}, if = {level == 3}{outer color=lightpink, text width=12mm,}{} } [Clasificación, outer color=lilac, for tree = { for children = { grow'=0, folder } } [Térmicos [Adición [MSF][MED][MVC][HDH][SD] ] [Extracción [Frz]] ] [Membrana [RO][FO][ED][NF] ] [Químicos [I.Ex][LLE][G. Hyd][Otros] ] [Adsorción] ] \end{forest} \end{figure}

\end{document}

EDIT:

I think the solution could lie in the use of:

for children = {
    if={mod(n,2)==0}{circle}{}
}

But it doesn't appear to change when I use grow'=east nor grow'=west

[Adición,
    for children = {
        if={mod(n,2)==0}{
            for parent = {grow'=west}
        }{
            for parent = {grow'=east}
        }
    }
    [MSF][MED][MVC][HDH][SD]
]

enter image description here

And if it's possible, how to center "clasificación"

1 Answers1

4

I noticed that Sašo Živanović commented that one post contained the cleanest forest code on this site. This one certainly contains the filthiest. It uses an extremely mangled version of Sašo's answer. The mangling is partly a result of my not understanding the purpose of code and partly a product of my inability to modify it appropriately.

If it weren't for the fact nobody has answered this in well over a year, this answer would certainly be worthy of deletion. As it is, it surely deserves no better fate.

pretty product; filthy code

I've not drawn edges to the phantom children. Instead, I've modified their children's edges to draw from their grandparent. But something is very wrong with the way I've configured those children, since --- despite Sašo's positioning code, I still end up having to move them in before computing xy. Not only does this mean they are not accounted for when packing; all I've done is hard-code adjustments with no attempt to calculate anything because I don't understand why they end up where they do in the first place.

For what it's worth, I created a test document with a simple tree repeated over and over to experiment with different combinations of folder, grow/grow'/folder indent/l sep etc., but I still don't understand how to sensibly code a directory tree with the edge to the right of the children and a downward direction of growth. for tree={folder,grow=180} seems to work, so I used that, but dir subtree, grow subtree=180 did not produce similar results. This aspect of forest is simply beyond my ability to comprehend it.

\documentclass[varwidth=20cm,tikz,border=10pt]{standalone}
\usepackage[edges]{forest}
\usetikzlibrary{arrows.meta,shadows,ext.paths.ortho}
% ateb: https://tex.stackexchange.com/a/705346/
% addaswyd o ateb Sašo Živanović: https://tex.stackexchange.com/a/691730/
% addaswyd o gwestiwn Datboi: https://tex.stackexchange.com/q/691576/
\forestset{
  declare boolean={phantom child}{0},
  dir subtree/.style={%
    for tree={
      folder,
    },
    for descendants={
      edge path'/.expanded={
        (!uu.parent anchor) |- (.child anchor)
      },
    },
  },
  phantom children/.style={
    delay={
      for children={
        content/.option=!u.content,
        opacity=0,
        text opacity=0,
        draw,
        no edge,
        phantom child,
      },
    },
    after packing node={
      for children={
        l'=0pt,
        s'=0pt,
        get point anchor={\foresteoption{grow}+180}{l-}{s-},
      },
    },
  },
  grow subtree/.style={
    for tree={grow=#1}
  }, 
  grow' subtree/.style={
    for tree={grow'=#1}
  },
}

\makeatletter % Analogous to \pgfpointanchor, but also allows forest anchors ("parent first" % etc.) and forest anchor options ("parent anchor" etc.) as #1. \def\forestpointanchor#1{% #1 = anchor \forestanchortotikzanchor{#1}\forest@temp@anchor \forestoifdefined{@box}{% \forestoget{@box}\forest@temp \ifdefempty\forest@temp{% \forestpointanchor@callpgfpointanchor{name}% }{% \setbox0\hbox{% \begin{pgfpicture}% \forestpointanchor@callpgfpointanchor{later@name}% \end{pgfpicture}% }% }% }{% \forestpointanchor@callpgfpointanchor{name}% }% \forest@global@marshal } \def\forestpointanchor@callpgfpointanchor#1{% #1 = (later@)name \pgfpointanchor{\forestove{#1}}{\forest@temp@anchor}% \xdef\forest@global@marshal{% \global\pgf@x=\the\pgf@x\relax \global\pgf@y=\the\pgf@y\relax\relax }% } \csdef{forest@anchor@@}{\def\forest@temp@anchor{center}}% \def\forestPointanchor#1#2{% #1 = node id, #2 = anchor \forest@fornode{#1}{\forestpointanchor{#2}}% } \forestset{ % Get the coordinates of anchor #1 % and assign the resulting x and y to keys #2 and #3, respectively. get point anchor/.code n args=3{% \forestpointanchor{#1}% \forestset{#2=\pgf@x,#3=\pgf@y}% }, } \makeatother

\begin{document}

% cwestiwn Eduardo Jiménez:https://tex.stackexchange.com/q/650323/ \pgfkeys{/forest, rect/.append style={rectangle, rounded corners=5pt, /tikz/align=center}, level outer colour/.style={outer color={level #1 outer colour}}, }

\definecolor{background}{RGB}{239, 239, 239} \definecolor{lightgreen}{RGB}{150, 240, 180} \definecolor{green}{RGB}{146, 208, 80} \definecolor{blue}{RGB}{4, 121, 181} \definecolor{linecol}{RGB}{92, 92, 92} \definecolor{lightpink}{RGB}{245, 160, 240} \definecolor{lightblue}{RGB}{176, 221, 255} \definecolor{lilac}{RGB}{174, 182, 211} \colorlet{level 0 outer colour}{lilac} \colorlet{level 1 outer colour}{lightblue} \colorlet{level 2 outer colour}{lightgreen} \colorlet{level 3 outer colour}{lightpink} \colorlet{level 4 outer colour}{lightpink} \begin{forest} fork sep'=5pt, text width=50mm, for tree={ rect, line width=0.5pt, draw=linecol, l sep'+=12.5pt, }, before typesetting nodes={% where phantom child={% }{% level outer colour/.option=level, inner color=background, drop shadow, minimum size=25pt, }, }, for children={% text width=38mm, edge+={color=linecol, rounded corners=5pt,-{Stealth[length=10pt]}, line width=0.5pt}, forked edge, for tree={% grow'=0, folder, if level=2{% text width=28mm, }{% if level=3{% text width=12mm, }{}, }, }, }, [Clasificación [Térmicos [Adición, phantom children,parent anchor=south [, dir subtree,for children={before computing xy={l'+=30pt}} [MSF][MED][MVC] ] [, dir subtree, grow subtree=180,for children={before computing xy={l'-=100pt}} [HDH][SD] ] ] [Extracción [Frz]] ] [,phantom,coordinate,calign with current] [Membrana [RO][FO][ED][NF] ] [Químicos [I.Ex][LLE][G. Hyd][Otros] ] [Adsorción] ] \end{forest}

\end{document}

cfr
  • 198,882