Latex学习笔记 (Ex2) 使用LaTeX画规则的树形图

时间:2024-03-29 11:44:59

树型

使用LaTeX\LaTeX也可以进行树型图的绘制,如以下效果。Latex学习笔记 (Ex2) 使用LaTeX画规则的树形图
源代码

\documentclass[11pt]{minimal}
\usepackage{tikz}
\usepackage{verbatim}
\usepackage[active,tightpage]{preview}
\PreviewEnvironment{tikzpicture}
\usetikzlibrary{trees}
\begin{document}
	\begin{tikzpicture}
	[thick,scale=1, every node/.style={scale=2}]
	\node {root}
	child {node {1}
		child {node {2}
			child {node {3}}
		}
		child [missing] {}
		child {node {3}
			child {node {2}}
		}
	}	
	child [missing] {}	
	child [missing] {}
	child [missing] {}	
	child [missing] {}	
	child { node {2}
		child {node {1}
			child {node {3}}
		}
		child [missing] {}
		child {node {3}
			child {node {1}}
		}
	}	
	child [missing] {}	
	child [missing] {}
	child [missing] {}	
	child [missing] {}	
	child { node {3}
		child {node {1}
			child {node {2}}
		}
		child [missing] {}
		child {node {2}
			child {node {1}}
		}
	};
	\end{tikzpicture}
\end{document}
 

扩展

为了实现更多的功能,搜索以后发现一个不错的资源[1],可以很方便的画各种树形图,为了做一个全排列的计算,找到了[2]. 根据少量调整,得到以下目标图片。
Latex学习笔记 (Ex2) 使用LaTeX画规则的树形图

以下为 LaTeX\LaTeX 源代码。

% Author: Frantisek Burian
\documentclass{minimal}
\usepackage{tikz}
%%%<
\usepackage{verbatim}
\usepackage[active,tightpage]{preview}
\PreviewEnvironment{tikzpicture}
\setlength\PreviewBorder{5pt}%
%%%>
\begin{comment}
:Title: Filesystem tree
:Tags: Trees; Styles
:Author: Frantisek Burian
:Slug: filesystem-tree
\end{comment}
\usetikzlibrary{trees}
\begin{document}
	\tikzstyle{every node}=[draw=black,thick,anchor=west]
	\tikzstyle{selected}=[draw=red,fill=red!30]
	\tikzstyle{optional}=[dashed,fill=gray!50]
	\begin{tikzpicture}[
	grow via three points={one child at (0.5,-0.7) and two children at (0.5,-0.7) and (0.5,-1.4)},
	edge from parent path={(\tikzparentnode.south) |- (\tikzchildnode.west)}]
	\node {[1,2,3]-[]}
	child { node [selected] {i=0 move to: $\rightarrow$ [2,3]-[1]}
		child { node {[3]-[1,2]}
			child { node {[]-[1,2,3]}}
		}
		child [missing] {}	
		child { node {[2]-[1,3]}
			child {node {[] - [1,3,2]}}
		}
		child [missing] {}	
	}		
	child [missing] {}	
	child [missing] {}	
	child [missing] {}		
	child [missing] {}	
	child { node [selected] {i=0  move back: $\rightarrow$ [1,2,3]-[]}}	
	child { node {i=1 $\rightarrow$ [1,3]-[2]}}				
	child { node {i=2 $\rightarrow$ [1,2]-[3]}};
	\end{tikzpicture}
\end{document}