朋友们,我尝试使用 tikzpicture\node\child 命令设置下图,但无法绘制。你能帮助我吗?
谢谢。
\documentclass[tikz,margin=2mm]{standalone}
\usetikzlibrary{trees,arrows}
\begin{document}
\tikzstyle{level 1}=[level distance=30mm, sibling distance=30mm]
\tikzstyle{level 2}=[level distance=30mm, sibling distance=15mm]
\tikzstyle{level 3}=[level distance=20mm]
\begin{tikzpicture}[grow=right,-,=angle 60]
%\begin{scope}[yshift=0]
\node {$f_{0,0}$}
child {node {$f_{2,2}$}
child {node {$f_{3,2}$}
child[-] {node{$x_{3}$}}
}
child {node{$f_{3,1}$}
child[-] {node{$x_{2}$}}
}
}
child {node {$f_{2,1}$}
child {node{$f_{3,2}$}
child[-] {node{$x_{1}$}}
}
child {node{$f_{3,1}$}
child[-] {node{$x_{0}$}}
}
};
%\end{scope}
\begin{scope}[yshift=-6cm]
\node {$f_{1,2}$}
child {node {$f_{2,2}$}
child {node {$f_{3,2}$}
child[-] {node{$x_{7}$}}
}
child {node {$f_{3,1}$}
child[-] {node{$x_{6}$}}
}
}
child {node {$f_{2,1}$}
child {node {$f_{3,2}$}
child[-] {node{$x_{5}$}}
}
child {node {$f_{3,1}$}
child[-] {node{$x_{4}$}}
}
};
\end{scope}
\end{tikzpicture}
\end{document}
答案1
正如 Alan Munn 所说,这实际上不是一棵树,矩阵可能更合适. 此类矩阵可用 很方便地生成tikz-cd
。要包围矩阵元素中的某些子表达式,可以使用\subnode
附带的tikzmark
。
\documentclass{article}
\usepackage[margin=1in]{geometry}
\usepackage{mathtools}
\usepackage{tikz-cd}
\usetikzlibrary{shapes.geometric,tikzmark,fit}
\begin{document}
\newcommand{\ARR}{\arrow[ul]\arrow[dl]}
\begin{tikzcd}[column sep=1ex,row sep=0pt,
every arrow/.append style={dash,shorten >=-2pt,shorten <=-2pt},
/tikz/remember picture]
x_1 & \subnode{red}{y_1} & & & & & \\
& & \subnode{orange}{y_{21}}=\dfrac{y_{2}-y_{1}}{x_2-x_1} \arrow[ul]\arrow[dl] & & & & \\
x_2 & y_2 & & \subnode{yellow}{y_{321}}=\dfrac{y_{32}-y_{21}}{x_3-x_1} \arrow[ul]\arrow[dl] & & \\
& & y_{32}=\dfrac{y_{3}-y_{2}}{x_3-x_2} \arrow[ul]\arrow[dl]& &
\subnode{green}{y_{4321}}=\dfrac{y_{432}-y_{321}}{x_4-x_1} \arrow[ul]\arrow[dl] & \\
x_3 & y_3 & & y_{432}=\dfrac{y_{43}-y_{32}}{x_4-x_2} \arrow[ul]\arrow[dl] & & \subnode{blue}{y_{54321}}=\dfrac{y_{5432}-y_{4321}}{x_5-x_1}\arrow[ul]\arrow[dl] \\
& & y_{43}=\dfrac{y_{4}-y_{3}}{x_4-x_3} \arrow[ul]\arrow[dl]& &
y_{5432}=\dfrac{y_{543}-y_{432}}{x_5-x_2} \arrow[ul]\arrow[dl] & \\
x_4 & y_4 & & y_{543}=\dfrac{y_{54}-y_{43}}{x_5-x_3} \arrow[ul]\arrow[dl] & & & \\
& &y_{54}=\dfrac{y_{5}-y_{4}}{x_5-x_4} \arrow[ul]\arrow[dl]& && & \\
x_5 & y_5 & & & & & \\
\end{tikzcd}
\begin{tikzpicture}[remember picture,overlay]
\foreach \X in {red,green,blue,orange,yellow}
{\node[draw,ellipse,inner ysep=-2pt,inner xsep=-4pt,fit=(\X),color=\X]{};}
\end{tikzpicture}
\end{document}
答案2
以下 MWE(是我之前的一个类似回答)可能是一个起点:
\documentclass{article}
\usepackage{tikz}
\usepackage{amsmath}
\begin{document}
\begin{tikzpicture}[grow=left,
sibling distance=50pt,
level distance=2cm,
edge from parent path={(\tikzparentnode.west) -- (\tikzchildnode.east)}]
\node {$y_{21} = \dfrac{y_2 - y_1}{x_2 - x_1}$}
child {node {$y_1$}}
child {node {$y_2$}}
;
\end{tikzpicture}
\end{document}
这是一个彩色的版本:
\documentclass{article}
\usepackage{tikz}
\usepackage{amsmath}
\usetikzlibrary{shapes}
\newcommand{\highlighted}[2]{\tikz[remember picture,baseline=-2pt]\node[rounded rectangle,draw=#1,text=black,anchor=center]{$#2$};}
\begin{document}
\begin{tikzpicture}[grow=left,
sibling distance=50pt,
level distance=2cm,
edge from parent path={(\tikzparentnode.west) -- (\tikzchildnode.east)}]
\node {$\highlighted{orange}{y_{21}}{} = \dfrac{y_2 - y_1}{x_2 - x_1}$}
child {node{\highlighted{red}{y_1}}}
child {node {$y_2$}}
;
\end{tikzpicture}
\end{document}
答案3
\documentclass[tikz,margin=2mm]{standalone}
\usetikzlibrary{trees,arrows}
\begin{document}
\tikzstyle{level 1}=[level distance=30mm, sibling distance=30mm]
\tikzstyle{level 2}=[level distance=30mm, sibling distance=15mm]
\tikzstyle{level 3}=[level distance=20mm]
\begin{tikzpicture}[grow=right,-,=angle 60]
%\begin{scope}[yshift=0]
\node {$f_{0,0}$}
child {node {$f_{2,2}$}
child {node {$f_{3,2}$}
child[-] {node{$x_{3}$}}
}
child {node{$f_{3,1}$}
child[-] {node{$x_{2}$}}
}
}
child {node {$f_{2,1}$}
child {node{$f_{3,2}$}
child[-] {node{$x_{1}$}}
}
child {node{$f_{3,1}$}
child[-] {node{$x_{0}$}}
}
};
%\end{scope}
\begin{scope}[yshift=-6cm]
\node {$f_{1,2}$}
child {node {$f_{2,2}$}
child {node {$f_{3,2}$}
child[-] {node{$x_{7}$}}
}
child {node {$f_{3,1}$}
child[-] {node{$x_{6}$}}
}
}
child {node {$f_{2,1}$}
child {node {$f_{3,2}$}
child[-] {node{$x_{5}$}}
}
child {node {$f_{3,1}$}
child[-] {node{$x_{4}$}}
}
};
\end{scope}
\end{tikzpicture}
\end{document