3

I want to draw a minimal box which contains all the grandchilds (pink boxes), enter image description here and move the text of deep green box to the bottom of the new box. How could I do it?

codes:

\documentclass{standalone}
\usepackage{forest}
\usetikzlibrary{arrows.meta,shapes,positioning,shadows,trees}

\tikzset{
    basic/.style  = {draw, text width=2cm, drop shadow, font=\sffamily, rectangle},
    basic1/.style  = {draw,  drop shadow, font=\sffamily, rectangle},
    root/.style   = {basic, rounded corners=2pt, thin, align=center,                    fill=green!30},
    onode/.style = {basic, thin, rounded corners=2pt, align=center, fill=green!60,text width=3cm,},
    tnode/.style = {basic1, thin, align=left, fill=pink!60},
    edge from parent/.style={draw=black, edge from parent fork right}
}
%
\begin{document}
\title{Structure of Book}
\begin{forest} for tree={anchor=base west,
    grow=east,
    growth parent anchor=east,
    parent anchor=east,
    child anchor=west,
    edge path={\noexpand\path[\forestoption{edge},->, >={latex}] 
         (!u.parent anchor) -- +(5pt,0pt) |- (.child anchor)
         \forestoption{edge label};}
}
[Nonlinear Stochastic Systems, root
       [{Applications to Complex Systems, NCSs, GRNs}, onode
       [Chapter 8\\ Complex Networks, tnode]
        [Chapter 7\\ Networked Control Systems, tnode] 
        [Chapter 5\\Gene Regulatory Networks, tnode] ] 
]
\end{forest}
\end{document}
  • You want to make to deep green box a child of the main root? Sorry but your question is unclear for me... – Enthusiastic Engineer Jul 10 '15 at 7:58
  • Thanks for your attention, @Alenanno has solved my problem. – wayne Jul 10 '15 at 9:08
4

Update

Here's the new version using two boxes. I changed the space between the nodes to make it possible to have multiple dashed boxes.

Output

figure 1

Code

\documentclass[margin=10pt]{standalone}
\usepackage{forest}
\usetikzlibrary{arrows.meta,shapes,positioning,shadows,fit,trees}

\tikzset{
    basic/.style  = {draw, text width=2cm, drop shadow, font=\sffamily, rectangle},
    basic1/.style  = {draw,  drop shadow, font=\sffamily, rectangle},
    root/.style   = {basic, rounded corners=2pt, thin, align=center,                    fill=green!30},
    onode/.style = {basic, thin, rounded corners=2pt, align=center, fill=green!60,text width=3cm,},
    tnode/.style = {basic1, thin, align=left, fill=pink!60},
    edge from parent/.style={draw=black, edge from parent fork right}
}
%
\begin{document}
\title{Structure of Book}
\begin{forest} for tree={anchor=base west,
    grow=east,
    growth parent anchor=east,
    parent anchor=east,
    child anchor=west,l sep+=1em,s sep=5mm,
    edge path={\noexpand\path[\forestoption{edge},->, >={latex}] 
         (!u.parent anchor) -- +(1em,0pt) |- (.child anchor)
         \forestoption{edge label};}
}
[Nonlinear Stochastic Systems, root
       [Chapter 8\\ Complex Networks, tnode, name=eight]
        [Chapter 7\\ Networked Control Systems, tnode, name=seven] 
        [Chapter 5\\Gene Regulatory Networks, tnode, name=five]
        [Chapter 11\\randomly varying nonlinearities, tnode, name=eleven]
        [Chapter 6 \, 10\\missing measurement, tnode] 
        [Chapter 9\\randomly occurring nonlinearities, tnode, name=nine] ] ] 
%
\node [draw, dashed, black!40, inner sep=.4em, fit={(eight) (five) (seven)}, label={[align=left]0:Applications to\\Complex Systems,\\NCSs, GRNs}] {};
\node [draw, dashed, black!40, inner sep=.4em, fit={(eleven) (nine)}, label={[align=left]0:Analysis of\\something complex\\more text}] {};
\end{forest}
\end{document}
  • @Alenanno I understand why I can't work it out it seems the \node line can only be placed at the bottom of the codes. – wayne Jul 10 '15 at 14:25
  • 1
    @WangyanLi Well, if you're referencing nodes then the referenced nodes must appear before. If you say place this node right of n1, but n1 appears afterwards, then Latex will say "I don't know this node you're referring to". – Alenanno Jul 10 '15 at 14:28
  • Yes, I understand. I notice that the \node[]{}; was placed at the bottom of the code instead of, for example, chapter 5 and chapter 11 which is my way to do it ending with errors. – wayne Jul 10 '15 at 14:34
  • 1
    @WangyanLi I don't think you can do that with forest, but even if you could, I prefer to organise the code where everything similar appears grouped in the same place so that it's easier to find them and also it's less prone to errors. – Alenanno Jul 10 '15 at 14:37
  • 1
    You can specify them within the tree... ;). – cfr Jul 10 '15 at 16:35
2

This responds to queries in the comments on Alenanno's answer.

If you want something like this:

groups

It is not necessary to name the nodes explicitly if we use relative node names and specify the additional \node commands within the tree itself. This can be done using the tikz key or the afterthought key. In the following, I use the former. I've also pruned the unnecessary style onode (which originally stood for 'orange node', by the way). I've also tidied up a bit and improved the spacing which was a bit squashed in the version I posted earlier.

fitbox specifies the style used to draw the dashed boxes. I've made all nodes have rounded corners as it looked odd to me to have a mixture but you can obviously adapt this.

fitting is a style which takes one argument. This specifies the nodes to be included in the dashed box. You can either use explicitly named nodes e.g. (nine) and name a node name=nine or use relative node names as I have here. (!n) is the next sibling, (!nn) is the next sibling's sibling and so on.

This makes the code a bit cleaner and easier to read.

\documentclass[margin=10pt]{standalone}
\usepackage{forest}
\usetikzlibrary{arrows.meta,shadows,fit}

\tikzset{
    basic/.style  = {draw, text width=2cm, drop shadow},
    basic1/.style = {draw,  drop shadow, rectangle},
    root/.style   = {basic, rounded corners=2pt, thin, align=center, fill=green!30},
    tnode/.style = {basic1, thin, align=left, fill=pink!60, rounded corners=2pt},
    fitbox/.style = {draw, gray, dashed, inner ysep=5pt, inner xsep=7.5pt, rounded corners=2pt},
}
\forestset{
  fitting/.style={
    tikz={\node [fitbox, fit=#1] {};},
    no edge,
    inner ysep=0pt,
  }
}
\begin{document}
\title{Structure of Book}
\begin{forest}
  for tree={
    anchor=base west,
    grow=east,
    parent anchor=east,
    parent anchor=east,
    child anchor=west,
    l sep+=1em,
    align=left,
    font=\sffamily,
    edge={->, >={latex}},
    edge path={
      \noexpand\path[\forestoption{edge}]  (!u.parent anchor) -- +(1em,0pt) |- (.child anchor)\forestoption{edge label};
    },
  }
  [Nonlinear\\Stochastic\\Systems, root
    [{Applications to\\Complex Systems,\\NCSs, GRNs}, fitting={(!nnn) (!n) (!nn)}]
    [Chapter 8\\ Complex Networks, tnode]
    [Chapter 7\\ Networked Control Systems, tnode]
    [Chapter 5\\Gene Regulatory Networks, tnode]
    [{Analysis and Synthesis\\for Systems with\\Incomplete Information},  fitting={(!n) (!nn) (!nnn)}]
    [Chapter 11\\randomly varying nonlinearities, tnode]
    [{Chapter 6, 10\\something},tnode]
    [{Chapter 9\\randomly occurring somethings},tnode]
  ]
\end{forest}
\end{document}
  • 1
    Just omit () in the argument to fit if you don't want the title in the dashed box. – cfr Jul 10 '15 at 14:17
  • Your solution is more clever, I'm enjoying it. Thanks indeed. – wayne Jul 10 '15 at 15:16
  • I have noticed some changes in your codes, but I can't understand it well, for example, fit=() (*) (*) (*) has modified to fitting={(!nnn) (!n) (!nn)}, could you explain it a bit to me or give me some hints which I can refer to. – wayne Jul 12 '15 at 6:34
  • 1
    @WangyanLi fitting is just a style I added for convenience, whereas fit is the underlying style provided by the fit library. So, fitting is just like fit except that it also adds no edge, inner ysep=0pt and applies the fitbox style. The node names I've used are relative node names. (!n) is the next sibling, (!nn) is the next sibling's sibling and so on. It just means I don't have to name the nodes and that the code will adapt automatically if I insert an additional sibling. Does that help? – cfr Jul 12 '15 at 10:56
  • 1
    @WangyanLi Oh, and () refers to the current node, so I omitted it because I realised you didn't want the titles within the dashed boxes. – cfr Jul 12 '15 at 11:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.