如何在 tikz-tree 周围创建花括号?

如何在 tikz-tree 周围创建花括号?

如果我使用 lyx 并且我有一个 tikz-tree,我该如何创建像这样的花括号并在它们上面写字:

在此处输入图片描述

答案1

首次尝试使用tikzdecorations.pathreplacing来绘制括号

在此处输入图片描述

代码

\documentclass[margin=5mm,dvipsnames]{standalone}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{positioning,decorations.pathreplacing}
\begin{document}

\begin{tikzpicture}[node distance=2cm,minimum height=1cm]

\node (A) {$n$};
\node[below left=of A] (B){$\dfrac{n}{3}$}; 
\node[below right=of A] (C){$\dfrac{2n}{3}$}; 
\begin{scope}[node distance=1cm and 1mm]
\node[below left=of B] (D){$T\left(\vphantom{\left(\dfrac{4n}{9}\right)}\dfrac{n}{9}\right)$}; 
\node[below right=of B] (E){$T\left(\dfrac{2n}{9}\right)$}; 
\node[below left=of C] (F){$T\left(\dfrac{2n}{9}\right)$}; 
\node[below right=of C] (I){$T\left(\dfrac{4n}{9}\right)$}; 
\end{scope}

\foreach \from/\to in {A/B,A/C,B/D,B/E,C/F,C/I}
{\draw(\from)--(\to);}

\draw[decorate,decoration={brace,amplitude=3mm},OliveGreen](A.45)--node[right,anchor=south west] {$\log_{3/2}n$}(I.60);
\draw[decorate,decoration={brace,mirror,amplitude=3mm},blue](A.135)--node[right,anchor=south east] {$\log_{3}n$}(D.120);


\end{tikzpicture}

\end{document}

更新

如果你想要更复杂的树,你可以利用森林

\documentclass[margin=5mm,dvipsnames]{standalone}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{positioning,decorations.pathreplacing}
\begin{document}

\begin{tikzpicture}[node distance=2cm and 2cm,minimum height=1cm]

\node (A) {$n$};
\node[below left=of A] (B){$\dfrac{n}{3}$}; 
\node[below right=of A] (C){$\dfrac{2n}{3}$}; 
\node[right =1cm of B](G){$\dfrac{n}{3}$}(C);
\node[left =1cm of C](K){$\dfrac{n}{3}$}(C);
\begin{scope}[node distance=1cm and 1mm]
\node[below left=of B] (D){$T\left(\vphantom{\left(\dfrac{4n}{9}\right)}\dfrac{n}{9}\right)$}; 
\node[below right=of B] (E){$T\left(\dfrac{2n}{9}\right)$}; 
\node[below left=of C] (F){$T\left(\dfrac{2n}{9}\right)$}; 
\node[below right=of C] (I){$T\left(\dfrac{4n}{9}\right)$}; 
\end{scope}

\foreach \from/\to in {A/B,A/C,A/G,A/K,B/D,B/E,C/F,C/I}
{\draw(\from)--(\to);}

\draw[decorate,decoration={brace,amplitude=3mm},OliveGreen](A.45)--node[right,anchor=south west] {$\log_{3/2}n$}(I.60);
\draw[decorate,decoration={brace,mirror,amplitude=3mm},blue](A.135)--node[right,anchor=south east] {$\log_{3}n$}(D.120);

\end{tikzpicture}

\end{document}

在此处输入图片描述

相关内容