9

I'm new to tikz and I'm trying to get my head around the \foreach function. Here's my code:

\documentclass[border=10pt]{standalone}
\usepackage{tikz}\usetikzlibrary{calc,shapes,positioning,arrows,positioning,shapes.geometric,fit,trees}
\begin{document}

\tikzstyle{block} = [diamond, draw, text width=6em, text badly centered, node distance=3cm, inner sep=0pt] 

\begin{tikzpicture}[subs/.style={grow=down,xshift=1em,anchor=west,edge from parent path={(\tikzparentnode.south) |- (\tikzchildnode.west)}},
blk/.style={rectangle,draw},
first/.style={level distance=6ex},second/.style={level distance=12ex},third/.style={level distance=18ex},
fourth/.style={level distance=24ex},fifth/.style={level distance=30ex},sixth/.style={level distance=36ex},
seventh/.style={level distance=42ex},eigth/.style={level distance=48ex},ninth/.style={level distance=54ex},
level 1/.style={sibling distance=20em}]
%UAS
    \coordinate
    node[blk]{UAS}
    [edge from parent fork down]
%UAV
    child{node[blk] {UAV}
    child[subs,first] {node[blk]{Power}}
    child[subs,second] {node[blk]{Flight computer}}
    child[subs,third] {node[blk] {Landing gear}}
    child[subs,fourth]{node[blk]{Flight controls}}
    child[subs,fifth]{node[blk]{Airframe}}
    child[subs,sixth]{node[blk]{Engine (propulsion/fuel)}}
    child[subs,seventh]{node[blk]{Communication/navigation}}
    child[subs,eigth]{node[blk]{Electrical/hydraulics/environmental}}
    child[subs,ninth]{node[blk]{Environmental control}}
    }
\end{tikzpicture}
\end{document}

As you can see, I've create nine styles to shift each child now the page. This seems very impractical and clunky. I was looking into using the \foreach function to automate this shifting process based on the number of children, but couldn't get anything to work. Can anyone offer some help?

7

Well, this was more difficult that expected. I think I managed...

Full code:

\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc,shapes,positioning,arrows,positioning,shapes.geometric,fit,trees}
\begin{document}

\tikzstyle{block} = [diamond, draw, text width=6em, text badly centered, node distance=3cm, inner sep=0pt] 

\begin{tikzpicture}[%
     subs/.style={grow=down,xshift=1em,anchor=west,edge from parent path={(\tikzparentnode.south) |- (\tikzchildnode.west)}},
     blk/.style={rectangle,draw},
     level 1/.style={sibling distance=20em}
]

\coordinate
node[blk]{UAS}
[edge from parent fork down]
child { node[blk] {UAV}
    child [subs,style={level distance=6 ex}]
    foreach [count=\i,
             evaluate={\j=6*(\i-1);}] \p in 
        {Power,computer,Landing gear,Flight controls,Airframe,Engine (propulsion/fuel),Communication/navigation,Electrical/hydraulics/environmental,Environmental control}
        {child[subs,style={level distance=\j ex}] {node[blk]{\p}}}
    };
\end{tikzpicture}
\end{document}

Result:

enter image description here

|improve this answer|||||

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.