6

1.I want to change the forest height dynamic,like this

for tree={
    grow=0,reversed, % tree direction
    parent anchor=east,child anchor=west, % edge anchors
edge path={none},
    minimum width=15mm, 
    draw=black,
    outer sep=0pt,% node shape
    l sep=0mm, % level distance
    s sep=0mm % level distance
} 

change to enter image description here like enter image description here

2.Flowing code copy from forest_doc

\documentclass[10pt,a4paper]{article}
\usepackage[latin1]{inputenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{graphicx}
\usepackage[left=1.00cm, right=1.00cm, top=1.00cm, bottom=1.00cm]{geometry}
\usepackage{forest}
\begin{document}
    \forestset{box/.style={
            draw, 
            no edge, 
            l=0, 
            l sep=0mm,
            s sep=0mm,
            calign=first, 
            anchor=base west,
            content format={\strut\forestoption{content}},
            if n children=0{}{
                after packing node={
                    minimum width/.pgfmath=
                    {s("!l")+max_x("!l")-s("!1")-min_x("!1")},
                    for children/.wrap pgfmath arg={s+={##1}}{0},
                    typeset node}}}}
    \begin{forest} 
        for tree={box} [total
        [home[saso[Download][TeX]][alja][joe]]
        [usr[bin][share]]]
    \end{forest}

\end{document}
2

2 Answers 2

6

This is a supplement to Zarko's answer which avoids hard-coding so many dimensions. Although the code is a bit more complex, the result is more flexible in that it is easier to extend and/or adapt the tree e.g. by adding additional layers or nodes with multi-line texts.

\documentclass[tikz,border=10pt,multi]{standalone}
\usepackage{forest}
\usetikzlibrary{backgrounds}
% modified from Zarko's answer at https://tex.stackexchange.com/a/314181/
\begin{document}

\tikzset{
  L0/.style={fill=green},
  L1/.style={fill=orange},
  L2/.style={fill=yellow},
  L3/.style={fill=pink},
  basic/.style={line width=1pt, draw=white},
}

\begin{forest}
  for tree={
    grow'=0,
    parent anchor=children,
    child anchor=parent,
    edge path={none},
    minimum width=15mm,
    l sep=0pt,
    s sep=0pt,
    if n children=0{
      minimum height=8mm,
    }{},
  },
  before drawing tree={
    where n children=0{
      tikz+/.wrap pgfmath arg={
        \scoped[on background layer]{\path [L#1, basic] (.north west) rectangle (.south east);}
      }{level()},
    }{
      tikz+/.wrap pgfmath arg={
        \scoped[on background layer]{\path [L#1, basic] (.west |- !L.south) rectangle (.east |- !F.north);}
      }{level()},
    }
  }
  [AAAA
    [EEEE, minimum height=32mm
        [EEEE
            [YYYY]
            [AAAA]
        ]
        [DDDD
            [DDDD]
            [KKKK]
        ]
    ]
    [OOOO
        [PPPP
            [KKKK]
            [KKKK]
        ]
    ]
  ]
\end{forest}
\end{document}

output

4
  • (+1)! as I said, I still learn forest ... :-)
    – Zarko
    Commented Jun 11, 2016 at 17:10
  • @Zarko For a one-off tree which won't change, your code is simpler and shorter. I just wanted to demonstrate how to get the fills automatically sized as that's a more flexible solution if the tree changes, for example. (The 8mm is still hard-coded, of course, as that can't be automatically determined - it is a matter of preference.)
    – cfr
    Commented Jun 11, 2016 at 19:21
  • Arguably, it would be easier as a table. I wonder if Bernard's seen this?
    – cfr
    Commented Jun 11, 2016 at 19:22
  • thanks a lot,it's my purpose completely。thanks for share.
    – andrew
    Commented Jun 12, 2016 at 1:35
3

Something like this:

enter image description here

I still in phase of learning forest, so there should be more elegant solution ...

\documentclass[border=3mm]{standalone}
\usepackage{forest}

\begin{document}

\forestset{
    line width=1pt,
  L1/.style={fill=green,minimum height=48mm,yshift=4mm},
  L2/.style={fill=orange,minimum height=16mm},
  L3/.style={fill=yellow,minimum height=16mm},
  L4/.style={fill=pink,minimum height=8mm},
}

\begin{forest}
    for tree={
        grow=0,reversed, % tree direction
        parent anchor=east,child anchor=west, % edge anchors
    edge path={none},
        minimum width=15mm, draw=white,outer sep=0pt,% node shape
        l sep=0mm, % level distance
        s sep=0mm % level distance
    }
  [AAAA,L1
    [EEEE,L2,minimum height=32mm
        [EEEE,L3
            [YYYY,L4][AAAA,L4]
        ]
        [DDDD,L3
            [DDDD,L4][KKKK,L4]
        ]
    ]
    [OOOO,L2
        [PPPP,L3
            [KKKK,L4][KKKK,L4]
        ] 
    ]   
  ]
\end{forest}
\end{document}

I change the tree as it was determined in MWE so, that is more likely to provided image.

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .