7

I need to do a "work breakdown structure". As it has to many items on the second level i would like to arrange it horizontally? On http://www.texample.net/tikz/examples/work-breakdown-structure/ I found following code by Gonzalo Medina:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows,shapes,positioning,shadows,trees}

\tikzset{
  basic/.style  = {draw, text width=2cm, drop shadow, font=\sffamily, rectangle},
  root/.style   = {basic, rounded corners=2pt, thin, align=center,
                   fill=green!30},
  level 2/.style = {basic, rounded corners=6pt, thin,align=center, fill=green!60,
                   text width=8em},
  level 3/.style = {basic, thin, align=left, fill=pink!60, text width=6.5em}
}

\begin{document}
\begin{tikzpicture}[
  level 1/.style={sibling distance=40mm},
  edge from parent/.style={->,draw},
  >=latex]

% root of the the initial tree, level 1
\node[root] {Drawing diagrams}
% The first level, as children of the initial tree
  child {node[level 2] (c1) {Defining node and arrow styles}}
  child {node[level 2] (c2) {Positioning the nodes}}
  child {node[level 2] (c3) {Drawing arrows between nodes}};

% The second level, relatively positioned nodes
\begin{scope}[every node/.style={level 3}]
\node [below of = c1, xshift=15pt] (c11) {Setting shape};
\node [below of = c11] (c12) {Choosing color};
\node [below of = c12] (c13) {Adding shading};

\node [below of = c2, xshift=15pt] (c21) {Using a Matrix};
\node [below of = c21] (c22) {Relatively};
\node [below of = c22] (c23) {Absolutely};
\node [below of = c23] (c24) {Using overlays};

\node [below of = c3, xshift=15pt] (c31) {Default arrows};
\node [below of = c31] (c32) {Arrow library};
\node [below of = c32] (c33) {Resizing tips};
\node [below of = c33] (c34) {Shortening};
\node [below of = c34] (c35) {Bending};
\end{scope}

% lines from each level 1 node to every one of its "children"
\foreach \value in {1,2,3}
  \draw[->] (c1.195) |- (c1\value.west);

\foreach \value in {1,...,4}
  \draw[->] (c2.195) |- (c2\value.west);

\foreach \value in {1,...,5}
  \draw[->] (c3.195) |- (c3\value.west);
\end{tikzpicture}
\end{document}

Which leads to following result:

WBS
(source: texample.net)

How can the code be modified in a way that the diagram will expand from the left to the right?

0

1 Answer 1

13

This answer provides two solutions, the first using Tikz, and the second one, using the forest package.

Tikz

enter image description here

Code

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,shapes,positioning,shadows,trees}

\tikzset{
    basic/.style  = {draw, text width=2cm, drop shadow, font=\sffamily, rectangle},
    root/.style   = {basic, rounded corners=2pt, thin, align=center,
                     fill=green!30},
    onode/.style = {basic, thin, align=center, fill=green!60,text width=3cm,},
    tnode/.style = {basic, thin, align=left, fill=pink!60, text width=6.5em},
    edge from parent/.style={->, >={latex}, draw=black, edge from parent fork right}
}

\begin{document}
\begin{tikzpicture}[%
    grow=right,
    anchor=west,
    growth parent anchor=east,
    parent anchor=east,
    level 1/.style={sibling distance=4cm},
    level 2/.style={sibling distance=2.5em},
    level distance=1cm]

\node[root] (root) {Drawing diagrams}
    child {node[onode] (c1) {Defining node and arrow styles}
        child {node[tnode] (c11) {Setting shape}}
        child {node[tnode] (c12) {Choosing color}}
        child {node[tnode] (c13) {Adding shading}}
    }
    child {node[onode] (c2) {Positioning the nodes}
        child {node[tnode] (c21) {Using a Matrix}}
        child {node[tnode] (c22) {Relatively}}
        child {node[tnode] (c23) {Absolutely}}
        child {node[tnode] (c24) {Using overlays}}
    }
    child {node[onode] (c3) {Drawing arrows between nodes}
        child {node[tnode] (c31) {Default arrows}}
        child {node[tnode] (c32) {Arrow library}}
        child {node[tnode] (c33) {Resizing tips}}
        child {node[tnode] (c34) {Shortening}}
        child {node[tnode] (c35) {Bending}}
};
\end{tikzpicture}
\end{document}

Forest

forest figure

Code

\documentclass{article}
\usepackage{forest}
\usetikzlibrary{arrows.meta,shapes,positioning,shadows,trees}
%
\tikzset{
    basic/.style  = {draw, text width=2cm, 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 = {basic, thin, align=left, fill=pink!60, text width=6.5em},
    edge from parent/.style={draw=black, edge from parent fork right}
}
%
\begin{document}
\begin{forest} for tree={
    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};}
}
[Drawing diagrams, root
    [Defining node and arrow styles, onode
        [Setting shape, tnode]
        [Choosing color, tnode]
        [Adding shading, tnode] ]
    [Positioning the nodes, onode
        [Using a Matrix, tnode]
        [Relatively, tnode]
        [Absolutely, tnode] 
        [Using overlays, tnode] ]
    [Drawing arrows between nodes, onode
        [Default arrows, tnode]
        [Arrow library, tnode]
        [Resizing tips, tnode] 
        [Shortening, tnode]
        [Bending, tnode] ] ]
\end{forest}
\end{document}
3
  • That is exactly what I was looking for. If I may ask one follow up question: How do you adjust the sibling distance individually not only for the levels but also for the individual item?
    – Thomas
    Commented May 13, 2015 at 14:36
  • @Thomas I'm posting an edit.
    – Alenanno
    Commented May 13, 2015 at 15:18
  • @Thomas You can [yshift=<length>] single nodes if you want. :)
    – Alenanno
    Commented May 13, 2015 at 15:21

You must log in to answer this question.

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