用 LaTeX 编写一棵简单的树

用 LaTeX 编写一棵简单的树

我想知道如何在 LaTeX 中编写以下图表:

在此处输入图片描述

我只是 LaTeX 的初学者,所以我尝试遵循这个例子,但我无法让它工作。使用

\begin{document}

\begin{tikzpicture}[>=stealth',shorten >=1pt,node distance=2cm,on grid,initial/.style={}]
\node[state] (T0) {$0$};
\node[state] (T1) [above =of T0] {$1$};
\node[state] (T2) [right =of T1] {$2$};
\node[state] (T3) [below =of T0] {$4$};
\node[state] (T4) [left =of T3] {$5$};
\node[state] (T5) [right =of T3] {$3$};

\tikzset{every node/.style={fill=white}}
\path (T0) edge (T1);
\path (T0) edge (T2);
\path (T0) edge (T3);
\path (T0) edge (T4);
\path (T0) edge (T5);
\end{tikzpicture}

\end{document}

我明白了

在此处输入图片描述

我没有完成,因为做得不好。我该如何弥补?

答案1

这是实现此目的的一种方法。

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows, positioning}

\begin{document}

\begin{tikzpicture}[every node/.style={shape=circle,fill=black,circle,inner sep=1pt}]
    \node[label=above:{$0$}] (T0) {}
        child [grow=126] {node[label=above right:{$1$}] (T1) {}
            child {node[label=above:{$b_1$}] (Tb1) {}}
            child {node[label=left:{$a_1$}] (Ta1) {}} }
        child [grow= 54] {node[label=above left:{$2$}] (T2) {}
            child {node[label=right:{$b_2$}] (Tb2) {}}
            child {node[label=above:{$a_2$}] (Ta2) {}} }
        child [grow=344] {node[label=above:{$3$}] (T3) {}
            child {node[label=right:{$a_3$}] (Ta3) {}}
            child {node[label=right:{$b_3$}] (Tb3) {}} }
        child [grow=270] {node[label=right:{$4$}] (T4) {}
            child {node[label=left:{$a_4$}] (Ta4) {}}
            child {node[label=right:{$b_4$}] (Tb4) {}} }
        child [grow=198] {node[label=above:{$5$}] (T5) {}
            child {node[label=left:{$a_5$}] (Ta5) {}}
            child {node[label=left:{$b_5$}] (Tb5) {}} };
\end{tikzpicture}

\end{document}

答案2

可以使用选项方便地绘制此类树grow cyclic。此示例与 pgf 手册 v3.1.5 第 845 页的示例非常相似

在此处输入图片描述

\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{trees}
\begin{document}
\begin{tikzpicture}[grow cyclic,nodes={circle,fill,inner sep=1.5pt},
level 1/.style={level distance=2.5cm,sibling angle=-360/5},
level 2/.style={font=\footnotesize, level distance=2cm,sibling angle=40},
rotate=-360/20,semithick]

\node {} % root
child foreach \X in {1,...,5}
{node[label=540/5-\X*360/5:\X]{} 
      child { node[label=180+360/20-\X*360/5-20:$b_\X$] {} }
      child { node[label=180+360/20-\X*360/5+20:$a_\X$] {} }
  };
\end{tikzpicture}
\end{document}

在此处输入图片描述

通过在标签锚点计算中使用实际的同级角度,可以使其更加通用。它存储在\pgfkeysvalueof{/tikz/sibling angle}级别中,并且因级别而异。(不,我对此并不感到自豪,\xdef但这样用户只需调整一次角度。)

\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{trees}
\begin{document}
\begin{tikzpicture}[grow cyclic,nodes={circle,fill,inner sep=1.5pt},
level 1/.style={level distance=2.5cm,sibling angle=-360/5},
level 2/.style={font=\footnotesize, level distance=2cm,sibling angle=60},
rotate=-360/20,semithick]

\node[label=above:$0$] {} % root
child foreach \X in {1,...,5}
{node[label={-1.5*\pgfkeysvalueof{/tikz/sibling angle}+\pgfkeysvalueof{/tikz/sibling angle}*\X}:\X]{\xdef\LevelOneSiblingAngle{\pgfkeysvalueof{/tikz/sibling angle}}} 
      child { node[label=180-\LevelOneSiblingAngle/5+\X*\LevelOneSiblingAngle-\pgfkeysvalueof{/tikz/sibling angle}/2:$b_\X$] {} }
      child { node[label=180-\LevelOneSiblingAngle/5+\X*\LevelOneSiblingAngle+\pgfkeysvalueof{/tikz/sibling angle}/2:$a_\X$] {} }
  };
\end{tikzpicture}
\end{document}

在此处输入图片描述

还可以破解growth function记录当前旋转角度,使角度的放置更加方便。破解增长函数的想法并不新鲜,我们可以看到非常好的例子,例如这里但也许这个 hack 是。当前边的斜率现在在函数中可用currangle。我决定不包括由设置的整体旋转rotate,因为我认为这样更干净。可以想象,这个 hack 对也很有用mindmaps

\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{trees}
\makeatletter
\def\tikz@grow@circle@record{%
  \pgfmathsetmacro{\currentrotation}{(\pgfkeysvalueof{/tikz/sibling angle})*(-.5-.5*\tikznumberofchildren+\tikznumberofcurrentchild)}%
  \pgftransformrotate{\currentrotation}%
  \expandafter\xdef\csname tikz@grow@circle@rot@\romannumeral\tikztreelevel\endcsname{\currentrotation}%
  \pgftransformxshift{\the\tikzleveldistance}%
}%
\pgfmathdeclarefunction{currangle}{0}{\begingroup
\pgfmathparse{(\pgfkeysvalueof{/tikz/sibling angle})*(-.5-.5*\tikznumberofchildren+\tikznumberofcurrentchild)}%
\ifcase\tikztreelevel
\or
\or
\pgfmathparse{\pgfmathresult+\tikz@grow@circle@rot@i}%
\or
\pgfmathparse{\pgfmathresult+\tikz@grow@circle@rot@i+\tikz@grow@circle@rot@ii}%
\fi
\typeout{\the\tikznumberofcurrentchild:\pgfmathresult}%
\pgfmathsmuggle\pgfmathresult
\endgroup}
\tikzset{
  recorded grow cyclic/.style={
    edge from parent/.append style={reset cm},
    growth function=\tikz@grow@circle@record,
  }
}%
\tikzoption{recorded clockwise from}{\let\tikz@grow=\tikz@grow@circle@cw@from@record\def\tikz@grow@circle@from@start{#1}}%
\makeatother
\begin{document}
\begin{tikzpicture}[recorded grow cyclic,nodes={circle,fill,inner sep=1.5pt},
level 1/.style={level distance=2.5cm,sibling angle=-360/5},
level 2/.style={font=\footnotesize, level distance=2cm,sibling angle=60},
semithick,rotate=-360/20]

\node[label=above:$0$] {} % root
child foreach \X in {1,...,5}
{node[label={currangle-90-360/20}:\X]{} 
      child { node[label={currangle-360/20}:$b_\X$] {} }
      child { node[label={currangle-360/20}:$a_\X$] {} }
  };
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容