i am new to latex. how can i make a hierarchy tree diagram as shown below?
[Tex/LaTex] To draw a hierarchy tree diagram
tikz-pgf
Related Solutions
Here is a simplification using the tikz intersections library to cut down on brute-force calculation:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{intersections}
\begin{document}
\begin{tikzpicture}
\coordinate (A) at (-5,0) {};
\coordinate (B) at ( 5,0) {};
\coordinate (C) at (0,7) {};
\draw[name path=AC] (A) -- (C);
\draw[name path=BC] (B) -- (C);
\foreach \y/\A in {0/G,1/F,2/E,3/D,4/C,5/B,6/A} {
\path[name path=horiz] (A|-0,\y) -- (B|-0,\y);
\draw[name intersections={of=AC and horiz,by=P},
name intersections={of=BC and horiz,by=Q}] (P) -- (Q)
node[midway,above] {\A};
}
\end{tikzpicture}
\end{document}
Notice the combination of level numbers and labels as in Gonzalo's comment.
Here is another solution using the tikz calculations library:
\foreach \y/\A in {0/G,1/F,2/E,3/D,4/C,5/B,6/A} {
\draw ($(A)!\y/7!(C)$) -- ($(B)!\y/7!(C)$)
node[midway,above] {\A};
}
The coordinate ($(A)!\y/7!(C)$)
is \y/7
of the way between the coordinates A
and C
. The advantage here is total DRY: Change any of the coordinates A
, B
, or C
, and the pyramid will transform accordingly.
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\coordinate (A) at (-5,0) {};
\coordinate (B) at ( 5,1) {};
\coordinate (C) at (0,3) {};
\draw (A) -- (C);
\draw (B) -- (C);
\foreach \y/\A in {0/G,1/F,2/E,3/D,4/C,5/B,6/A} {
\draw ($(A)!\y/7!(C)$) -- ($(B)!\y/7!(C)$) node[midway,above] {\A};
}
\end{tikzpicture}
\end{document}
Forest can automatically lay out the tree, avoiding most clashes. (Although labels on branches can potentially conflict, most conflicts between node content and edges etc. within the tree are automatically avoided.) It can also use the content of the tree's nodes to create the labels.
For example,
\documentclass[border=12pt,12pt]{standalone}
\usepackage{forest}
\begin{document}
\begin{forest}
for tree={
math content,
calign=fixed edge angles,
},
before typesetting nodes={
for descendants={
delay={content/.wrap value={\strut #1}},
inner sep=1.5pt,
where={
>OOw+n< {n}{!u.n children}{(#1+1)/2}
}{
edge label/.process={
OOw2 {content}{!u.content} {node [midway, left, anchor=base east] {$\frac{\partial #2}{\partial #1}$}}
}
}{
edge label/.process={
OOw2 {content}{!u.content} {node [midway, right, anchor=base west] {$\frac{\partial #2}{\partial #1}$}}
}
},
},
},
[z
[x
[s]
[t]
]
[y
[s]
[t]
]
]
\end{forest}
\end{document}
Best Answer
Updated answer (current Forest)
This version uses the
edges
library. If you get errors, use the code below or, preferably, update your TeX installation.Original answer
A slightly different
forest
solution which usestikz
to draw the line under the root. This also lines things up in a way which I think is neater than shown in the target image but, obviously, tastes differ so your kilometres may vary.