# How do I put labels on a tree diagram in tikz?

I am requesting help with a tree diagram in latex. I am preparing a handout on a mathematics course. In the handout I have a tree diagram which I modified from here: How can I reproduce this simple tree diagram?

While I am familiar with flow charts in tikz, I am not familiar with tree diagrams. What I am not able to put on the tree diagram are:

1. partial derivative as labels between variables, e.g.

$\frac{\partial z}{\partial x}$


between z and x, and similar partial derivatives between z and y, x and s, x and t, y and s and y and t.

2. the length of each branch

I found some material on labeling on a tree diagram here: How to label nodes on a tree diagram using tikz. But I am not able to modify the code to my needs. Besides separation between variables is not good, e.g. s and t are over written at the last level Assistance will be appreciated. I hope the document is readable, because I have problems inserting text which is mathematical equations. Below is my MWE.

\documentclass[12pt]{report}
\usepackage{tikz}
\begin{document}

$\textbf{The Chain Rule (Case 2)}$\\
Suppose that $z=f(x,y)$ is a differentiable function of x and y,\\
where $x=g(s,t)$ and $y=h(s,t)$ are both differentiable functions of s and t.\\
Then
$\frac{\partial z}{\partial s}=\frac{\partial z}{\partial x}\frac{\partial x}{s}+\frac{\partial z}{\partial y}\frac{\partial y}{\partial s}$\qquad $\frac{\partial z}{\partial t}=\frac{\partial z}{\partial x}\frac{\partial x}{\partial t}+\frac{\partial z}{\partial y}\frac{\partial y}{\partial t}$\\

\begin{minipage}{.8\linewidth}
The Chain Rule (Case 2) contains three types of variables: x and y and are independent
variables, s and t are called intermediate variables, z is the dependent variable.\\ Notice that this has one term for each intermediate variable and each of these terms resembles the one-dimensional Chain Rule above.\\
To remember the Chain Rule, refer to the tree diagram (right). Branches from the dependent variable z to the intermediate variables x and y indicate that z is a function of x and y. Then there are branches from x and y to the independent variables s and t. On each branch is the corresponding partial derivative.\\
To find $\frac{\partial z}{\partial s}$ find the product of the partial derivatives along each path from z to s and then add these products,\\

To find $\frac{\partial z}{\partial s}$ find the product of the partial derivatives along each path from z to s and then add these products,\\

i.e $\frac{\partial z}{\partial s}=\frac{\partial z}{\partial x}\frac{\partial x}{s}+\frac{\partial z}{\partial y}\frac{\partial y}{\partial s}$.\\
Similarly, find $\frac{\partial z}{\partial t}$ by using the paths from z to t,\\

i.e $\frac{\partial z}{\partial t}=\frac{\partial z}{\partial x}\frac{\partial x}{\partial t}+\frac{\partial z}{\partial y}\frac{\partial y}{\partial t}$\\
\end{minipage}
\begin{minipage}{.2\linewidth}
\begin{tikzpicture}[tn/.style={circle,inner sep=1.5pt,draw,fill}% tree node
]

\node (10) {z}
child { node (11) {x}
child { node (111) {s}}
child { node (112) {t}}}
child { node (12) {y}
child { node (121) {s}}
child { node (122) {t}}}
;
%    \node[left =of 121] {};
%    \node      at (10   -| 11) {$\frac{\partial z}{\partial x}$};
%    \node      at (10   -| 12) {$\frac{\partial z}{\partial y}$};
%    \node      at (11   -| 111) {$\frac{\partial x}{\partial s}$};
%    \node      at (11   -| 112) {$\frac{\partial x}{\partial t}$};
%    \node      at (12   -| 121) {$\frac{\partial y}{\partial s}$};
%    \node      at (12   -| 122) {$\frac{\partial y}{\partial t}$};
\end{tikzpicture}
\end{minipage}%
\end{document}

• label to nodes you can write for example as \node[label=left:<label>] {...} for more concrete helpo, please merge your code fragments into one, complete document, which we can be copied and tested in our computers. – Zarko May 21 '17 at 21:41
• I don't see how you can have modified the code you say, as it uses Forest and you don't. Did you reverse the links and maybe mean that you modified the other code? – cfr May 21 '17 at 22:18
• Off-topic: you should not be using \\  to end paragraphs like that. Use this only within special contexts e.g. tabular, array etc. – cfr May 21 '17 at 22:20
• Most of your content seems irrelevant to the question. Am I missing something or is only the picture itself needed? What do you mean about putting the length on each branch? – cfr May 21 '17 at 22:42

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}


• Thanks very much @cfr, the code you supplied does exactly what I want to do. And my sincere apologies for mixing my code with text. I modified code from here texblog.org/2015/09/16/drawing-trees-with-tikzpgf-and-lualatex, which is tikz code. The two links I gave in my original post is where I was trying to modify labeling. I am not able to attach my mwe to this comment as a tex file, otherwise if it is copied from \documentclass{} to \end{document}, all that is the mwe. Once more my sincere apologies to all. Thanks cfr for your time and effort and the spirit of knowledge sharing. – Zilore Mumba May 22 '17 at 5:00
• sorry to ask for more of your time. I find forest very complicated and usually work with tikz. The code you gave me works fine as a standalone, when inserted in my main document I get the error: ! Illegal parameter number in definition of \pgfmath@expression. And suggests the error is at \end{forest. I put only the following in my preamble: \usepackage{forest}. I wonder if you could assist further. – Zilore Mumba May 22 '17 at 8:04
• @ZiloreMumba If I plug my code into the example from your question, putting the forest in place of the tikzpicture and adding the forest package to the preamble, it works fine for me. Can you ask a new question with an MWE, linking to this one for context? Checked: pdfLaTeX, LuaLaTeX, XeLaTeX. – cfr May 22 '17 at 9:31