答案1
不可能看到一棵树,而不给出使用强大forest
包裹:
\documentclass[svgnames]{standalone}
\usepackage{forest}
\def\GG{\textcolor{DarkGreen}{G}}
\def\YY{\textcolor{Gold}{Y}}
\begin{document}
\begin{forest}
for tree={
edge path={
\noexpand\path[\forestoption{edge}]
(!u.parent anchor) -- +(0,-15pt) -|
(.child anchor)\forestoption{edge label};
},
}
[,shape=coordinate,name=root
[\YY\GG]
[\YY\GG
[\YY\YY
[\YY\YY,name=level3] [\YY\YY] [\YY\YY] [\YY\YY]
]
[\YY\GG
[\YY\YY] [\YY\GG] [\YY\GG] [\GG\GG]
]
[\YY\GG
[\YY\YY] [\YY\GG] [\YY\GG] [\GG\GG]
]
[\GG\GG,name=level2
[\GG\GG] [\GG\GG] [\GG\GG] [\GG\GG]
]
]
[\YY\GG]
[\YY\GG,name=level1]
]
\draw (root) -- ++(-15pt,0) node[anchor=east] {\YY\YY};
\draw (root) -- ++(15pt,0) node[anchor=west] {\GG\GG};
\foreach \Nodo/\Nombre in
{root/{parent generation},level1/{f1 generation},level2/{f2 generation}}
\node[anchor=west] at ([xshift=15pt]level2|-\Nodo) {\Nombre};
\node[anchor=north west,inner sep=0pt] at (level3|-0,0)
{\begin{tabular}{@{}r@{\,=\,}l@{}}
\YY & yellow allele \\
\GG & green allele
\end{tabular}%
};
\end{forest}
\end{document}
解释性评论
在评论中,有人要求抑制所有不必要的信息(标签和颜色)并显示树形成的一些步骤。
我们从一棵有空根和四个子树开始,逐步进行一些改变
\documentclass{standalone}
\usepackage{forest}
\begin{document}
\begin{forest}% just an empty root with four children using the default settings
[
[YG]
[YG]
[YG]
[YG]
]
\end{forest}\qquad
\begin{forest}% we declare the root as a ''coordinate'', so it takes no space
[,shape=coordinate
[YG]
[YG]
[YG]
[YG]
]
\end{forest}\qquad
\begin{forest}% we change the shape for the edges to a ''fork'' shape and name the root for later use
for tree={
edge path={
\noexpand\path[\forestoption{edge}]
(!u.parent anchor) -- +(0,-15pt) -|
(.child anchor)\forestoption{edge label};
},
}
[,shape=coordinate,name=root
[YG]
[YG]
[YG]
[YG]
]
\end{forest}
\end{document}
现在我们在根节点的一侧添加更多子节点和两个节点
\documentclass{standalone}
\usepackage{forest}
\begin{document}
\begin{forest}
for tree={
edge path={
\noexpand\path[\forestoption{edge}]
(!u.parent anchor) -- +(0,-15pt) -|
(.child anchor)\forestoption{edge label};
},
}
[,shape=coordinate,name=root % root with empty content; the shape is coordinate so it takes no space; we just give it a name for later use
[YG]
[YG]
[YG]
[YG]
]
% we use the name for the root here
\draw (root) -- ++(-15pt,0) node[anchor=east] {YY};
\draw (root) -- ++(15pt,0) node[anchor=west] {GG};
\end{forest}\qquad
\begin{forest}
for tree={
edge path={
\noexpand\path[\forestoption{edge}]
(!u.parent anchor) -- +(0,-15pt) -|
(.child anchor)\forestoption{edge label};
},
}
[,shape=coordinate,name=root % root with empty content; the shape is coordinate so it takes no space; we just give it a name for later use
[YG]
[YG [YY] [YG] [YG] [GG] ]
[YG]
[YG]
]
% we use the name for the root here
\draw (root) -- ++(-15pt,0) node[anchor=east] {YY};
\draw (root) -- ++(15pt,0) node[anchor=west] {GG};
\end{forest}
\end{document}
这部分
edge path={
\noexpand\path[\forestoption{edge}]
(!u.parent anchor) -- +(0,-15pt) -|
(.child anchor)\forestoption{edge label};
},
只是规定了连接节点和其子节点的路径的形状(默认是直线,但这里我们需要一个“叉”形);this answer
到定制树木时了解森林选项我解释了类似案例中使用的语法。
答案2
\documentclass[tikz]{standalone}
\usepackage{helvet}
\usetikzlibrary{calc}
\newcounter{M}
\setcounter{M}{0}
\newcommand{\Mend}[5][]{%
\begin{scope}[#1]
\draw (-1.5,0) -- (1.5,0)
node[pos=0,below=5pt] (\theM1) {#2}
node[pos=.333,below=5pt] (\theM2) {#3}
node[pos=.5,coordinate] (\theM3) {}
node[pos=.666,below=5pt] (\theM4) {#4}
node[pos=1,below=5pt] (\theM5) {#5} ;
\foreach \i in {0,...,3} {\draw (-1.5+\i,0)--(-1.5+\i,-5pt); }
\end{scope}
\stepcounter{M}
}
\newcommand{\Y}{{\color{orange}\bf\textsf{Y}}}
\newcommand{\G}{{\color{green}\bf\textsf{G}}}
\begin{document}
\begin{tikzpicture}[y=-.75cm]
\draw (-.5,0) node[left] {\Y} -- (.5,0) node[right] {\G} ;
\node[right] at (3,0) {parent generation};
\Mend[shift={(0,1)}]{\Y\G}{\Y\G}{\Y\G}{\Y\G}
\draw (0,0)--(03) ;
\node[right] at (3,1.5) {$f_1$ generation};
\Mend[shift={($(02)+(0,1)$)}]{\Y\Y}{\Y\G}{\Y\G}{\G\G}
\draw (02)--(13) ;
\node[right] at (3,3) {$f_2$ generation};
\begin{scope}[shift={($(13)+(0,.5)$)}]
\Mend[shift={(-6,1)}]{\Y\Y}{\Y\Y}{\Y\Y}{\Y\Y}
\draw (11.south)--(23) ;
\Mend[shift={(-2,1)}]{\Y\G}{\Y\G}{\Y\G}{\Y\G}
\draw (12.south)--(33) ;
\Mend[shift={( 2,1)}]{\Y\G}{\Y\G}{\Y\G}{\Y\G}
\draw (14.south)--(43) ;
\Mend[shift={( 6,1)}]{\G\G}{\G\G}{\G\G}{\G\G}
\draw (15.south)--(53) ;
\end{scope}
\node[right] at (-7,1) {\Y\ = yellow allele};
\node[right] at (-7,2) {\G\ = green allele};
\end{tikzpicture}
\end{document}
答案3
使用tikz
樹
代码
\documentclass[border=20pt]{standalone}%[12pt]{article}
\usepackage{xcolor}
\usepackage{tikz}
\usetikzlibrary{shapes,calc,positioning,arrows,mindmap,matrix}
\newcommand{\Y}{\color{yellow!50!red}Y}
\newcommand{\G}{\color{green!50!blue}G}
\begin{document}
\tikzset{
community_label_style/.style= {font=\sffamily\Large\bfseries,minimum size=0.7cm,text height=1.5ex,text depth=.25ex,},
edge from parent fork down,
level 1/.style={sibling distance = 2cm, level distance =2cm},
level 2/.style={sibling distance=2cm},
level 3/.style={sibling distance=4.5cm},
level 4/.style={sibling distance=1cm},
}
\begin{tikzpicture}[ultra thick,]
\node{}
child[] {node[community_label_style] (p) {$\Y\Y\G\G$}
child[]{node[community_label_style] (f1) {$\Y\G$}} %1
child[]{node[community_label_style] ( ) {$\Y\G$} %1
child[]{node[community_label_style] (v1) {$\Y\Y$}
child[]{node[community_label_style] (a) {$\Y\Y$}}
child[]{node[community_label_style] (b) {$\Y\Y$}}
child[]{node[community_label_style] (c) {$\Y\Y$}}
child[]{node[community_label_style] (d) {$\Y\Y$}}
}
child[]{node[community_label_style] (v2) {$\Y\G$}
child[]{node[community_label_style] (a) {$\Y\Y$}}
child[]{node[community_label_style] (b) {$\Y\G$}}
child[]{node[community_label_style] (c) {$\Y\G$}}
child[]{node[community_label_style] (f2){$\G\G$}}
}
child[]{node[community_label_style] (v3) {$\Y\G$}
child[]{node[community_label_style] (a) {$\Y\Y$}}
child[]{node[community_label_style] (b) {$\Y\G$}}
child[]{node[community_label_style] (c) {$\Y\G$}}
child[]{node[community_label_style] (d) {$\G\G$}}
}
child[]{node[community_label_style] (f2) {$\G\G$}
child[]{node[community_label_style] (a) {$\Y\Y$}}
child[]{node[community_label_style] (f1){$\Y\G$}}
child[]{node[community_label_style] (c) {$\Y\G$}}
child[]{node[community_label_style] (d) {$\G\G$}}
}}
child[]{node[community_label_style] (v5) {$\Y\G$}} %1
child[]{node[community_label_style] (f1) {$\Y\G$}} %1
};
\node[right=1cm of p](){parent generation};
\node[left=2.5cm of p](y){{\Y}=yellow allele };
\node[below=0.5cm of y](){{\G}=green allele};
\node[right=1cm of f1](){f1 generation};
\node[right=1cm of f2](){f2 generation};
\end{tikzpicture}
\end{document}
答案4
这是无耻地抄袭的Gonzalo Medina 的回答。它调整了该代码以使用实验性justtrees
包,这使得在右侧对齐级别描述变得容易。它还展示了如何使用 将图例等添加为树的一部分forest
。afterthought
这节省了不必要的命名节点。
环境的另一个justtree
有用的方面是,您不需要像forest
环境本身那样指定幻影或空的根节点。
\documentclass[svgnames,border=5pt,tikz]{standalone}
\usepackage{justtrees}% version 0.03
% original forest tree is from Gonzalo Medina's answer at https://tex.stackexchange.com/a/175689/
\def\GG{\textcolor{DarkGreen}{G}}
\def\YY{\textcolor{Gold}{Y}}
\begin{document}
\begin{justtree}
{
for tree={
edge path={
\noexpand\path[\forestoption{edge}] (!u.parent anchor) -- +(0,-10pt) -| (.child anchor)\forestoption{edge label};
},
font=\sffamily,
},
just format/.style={font=\sffamily},
}
[\YY,
afterthought={
\node [anchor=north, align=left, font=\sffamily] at (.north -| here)
{\begin{tabular}{r@{ = }l}
\YY & yellow allele\\
\GG & green allele
\end{tabular}
};
}
]
[, right just=parent generation, afterthought={\draw (.south) -- (.south |- !p.east) (!p.east) -- (!n.west);}
[\YY\GG, right just=f1 generation]
[\YY\GG
[\YY\YY, right just=f2 generation, name=here
[\YY\YY]
[\YY\YY]
[\YY\YY]
[\YY\YY]
]
[\YY\GG
[\YY\YY]
[\YY\GG]
[\YY\GG]
[\GG\GG]
]
[\YY\GG
[\YY\YY]
[\YY\GG]
[\YY\GG]
[\GG\GG]
]
[\GG\GG
[\GG\GG]
[\GG\GG]
[\GG\GG]
[\GG\GG]
]
]
[\YY\GG]
[\YY\GG]
]
[\GG]
\end{justtree}
\end{document}