如何在 tikz 中的树形图上添加标签?

如何在 tikz 中的树形图上添加标签?

我请求帮助在 latex 中绘制树形图。我正在准备一份数学课程的讲义。在讲义中,我有一个树形图,我从这里进行了修改:我怎样才能重现这个简单的树形图?

我虽然熟悉流程图tikz,但对树形图却不熟悉。我无法在树形图中显示的内容包括:

  1. 偏导数作为变量之间的标签,例如

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

    z 和 x 之间,以及 z 和 y、x 和 s、x 和 t、y 和 s 以及 y 和 t 之间的类似偏导数。

  2. 每个分支的长度

我在这里找到了一些有关树形图标签的材料:如何使用 tikz 标记树形图上的节点。但我无法根据自己的需要修改代码。此外,变量之间的分离也不好,例如 s 和 t 在最后一级被覆盖。如能得到帮助,我将不胜感激。我希望文档可读性强,因为我在插入数学方程式文本时遇到了问题。以下是我的 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}

答案1

Forest 可以自动布局树,避免大多数冲突。(尽管分支上的标签可能会发生冲突,但树内节点内容和边等之间的大多数冲突都可以自动避免。)它还可以使用树节点的内容来创建标签。

例如,

\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}

森林解决方案

相关内容