我想在表格旁边放置一个 TikZ 树。文档类型是 apa。因此树有四个分支,因此我想在树的左侧放置一个 4 x 6 的表格。分支的高度应与行的高度匹配。
这是我的代码。
\documentclass[doc,apacite]{apa}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
%%%%Tikz%%%%%%
\usepackage{tikz}
\usetikzlibrary{trees}
%%%%%%%%%%%%%%
%%%%Table%%%%%
\usepackage{ctable} % needed for \cmidrule{}
\usepackage{multirow} % needed for \multirow{}
%%%%%%%%%%%%%
\begin{document}
\begin{minipage}[t]{150pt}
% Overall layout of the tree
\tikzstyle{level 1}=[level distance=2.0cm, sibling distance=3.5cm]
\tikzstyle{level 2}=[level distance=3.0cm, sibling distance=3.0cm]
\tikzstyle{level 3}=[level distance=3.0cm, sibling distance=3.0cm]
% Styles for bags and leafs
\tikzstyle{bag} = [text width=4em, text centered]
\tikzstyle{end} = [circle, minimum width=3pt,fill, inner sep=0pt]
\begin{tikzpicture}[grow=right, sloped]
\node{}
child {
node{U2}
child {
node[label=right:
]{D2} {}
child {node[end, label=right:
{R1}] {}
edge from parent
node[above] {$1-R$}
node[below] {}}
child {node[end, label=right:
{R2}] {}
edge from parent
node[above] {$R$}
node[below] {}}
edge from parent
node[above] {$1-D$}
node[below] {}
}
child {
node[end, label=right:
{D1}] {}
edge from parent
node[above] {$D$}
node[below] {}
}
edge from parent
node[above] {}
node[below] {$1-u$}
}
child {
node{U1}
child{
node[end, label=right:
{$T$}] {}
edge from parent
node[above] {}
node[below] {}
}
edge from parent
node[above] {$u$}
node[below] {}
};
\end{tikzpicture}
\end{minipage}
\hfill
\begin{minipage}[t]{150pt}
%\begin{table}[h]
\begin{tabular}{|l|l|l|l|l|l|l|}
& & & & & & \\ \hline
& & & & & & \\ \hline
& & & & & & \\ \hline
& & & & & &
\end{tabular}
%\end{table}
\end{minipage}
\end{document}
我希望有人能帮助我。我已经花了好几个小时了。
答案1
也许你正在寻找类似的东西:
然后,您可以命名树的父节点,并使用matrix
和fit
库在树旁边定位一个矩阵。
代码:
\documentclass[margin=10pt]{standalone}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
%%%%Tikz%%%%%%
\usepackage{tikz}
\usetikzlibrary{
trees,
matrix,
fit
}
%%%%%%%%%%%%%%
\begin{document}
\newcommand\sibdist{2cm}
\begin{tikzpicture}[
grow=right, sloped,
bag/.style={text width=4em, text centered},
end/.style={circle, minimum width=3pt,fill, inner sep=0pt,font=\tiny},
sibling distance=\sibdist,
level 1/.style={level distance=2.0cm},
level 2/.style={level distance=3.0cm},
every node/.style={font=\strut},
]
\node(n){}% named parent node
child {
node{U2}
child {
node[label=right:
]{D2} {}
child {node[end, label=right:
{R1}] {}
edge from parent
node[above] {$1-R$}
node[below] {}}
child {node[end, label=right:
{R2}] {}
edge from parent
node[above] {$R$}
node[below] {}}
edge from parent
node[above] {$1-D$}
node[below] {}
}
child {
node[end, label=right:
{D1}] {}
edge from parent
node[above] {$D$}
node[below] {}
}
edge from parent
node[above] {}
node[below] {$1-u$}
}
child {
node{U1}
child{
node[end, label=right:
{$T$}] {}
edge from parent
node[above] {}
node[below] {}
}
edge from parent
node[above] {$u$}
node[below] {}
};
\matrix[% table as tikz matrix
matrix of nodes,
anchor=m-4-1.west,
xshift=1cm,
nodes={inner xsep=\tabcolsep,minimum height=0.5*\sibdist},
](m)at(n-1-1-1){%
c&b&a&c&bd&a\\
a&c&b&a&a&c\\
x&g&f&c&d&b\\[.5*\sibdist]
ta&g&a&x&p&d\\
};
% lines around the columns:
\foreach \i in {1,...,6}\node[draw,fit=(m-1-\i) (m-2-\i) (m-3-\i) (m-4-\i),inner sep=-.5\pgflinewidth](c\i){};
% lines between the rows:
\foreach \i in {1,...,3}\draw([yshift=-.25*\sibdist]m-\i-1.center-|c1.west)--([yshift=-.25*\sibdist]m-\i-1.center-|c6.east);
\end{tikzpicture}
\end{document}
或者可能更好
代码:
\newcommand\sibdist{1.5cm}
\begin{tikzpicture}[
grow=right, sloped,
bag/.style={text width=4em, text centered},
end/.style={circle, minimum width=3pt,fill, inner sep=0pt,font=\tiny},
sibling distance=\sibdist,
level 1/.style={level distance=2.0cm},
level 2/.style={level distance=3.0cm},
every node/.style={font=\strut},
]
\node(n){}
child {
node{U2}
child {
node[label=right:
]{D2} {}
child {node[end, label=right:
{R1}] {}
edge from parent
node[above] {$1-R$}
node[below] {}}
child {node[end, label=right:
{R2}] {}
edge from parent
node[above] {$R$}
node[below] {}}
child[missing]
edge from parent
node[above] {$1-D$}
node[below] {}
}
child {
node[end, label=right:
{D1}] {}
edge from parent
node[above] {$D$}
node[below] {}
}
child[missing]
edge from parent
node[above] {}
node[below] {$1-u$}
}
child {
node{U1}
child{
node[end, label=right:
{$T$}](e) {}
edge from parent
node[above] {}
node[below] {}
}
edge from parent
node[above] {$u$}
node[below] {}
};
\matrix[
matrix of nodes,
anchor=m-4-1.west,
xshift=1cm,
nodes={inner xsep=\tabcolsep,minimum height=\sibdist}
](m)at(n-1-1-1){%
c&b&a&c&bd&a\\
a&c&b&a&a&c\\
x&g&f&c&d&b\\%[1cm]
ta&g&a&x&p&d\\
};
\foreach \i in {1,...,6}\node[draw,fit=(m-1-\i) (m-2-\i) (m-3-\i) (m-4-\i),inner sep=-.5\pgflinewidth](c\i){};
\foreach \i in {1,...,3}\draw([yshift=-.5*\sibdist]m-\i-1.center-|c1.west)--([yshift=-.5*\sibdist]m-\i-1.center-|c6.east);
\end{tikzpicture}
如果第一和第二个分支应该与第三和第四个分支对齐,则可以在第 2 级插入空子分支:
\newcommand\sibdist{1.5cm}
\begin{tikzpicture}[
grow=right, sloped,
bag/.style={text width=4em, text centered},
end/.style={circle, minimum width=3pt,fill, inner sep=0pt,font=\tiny},
sibling distance=\sibdist,
level 1/.style={level distance=2.0cm},
level 2/.style={level distance=3.0cm},
every node/.style={font=\strut},
]
\node(n){}
child {
node{U2}
child {
node[label=right:
]{D2} {}
child {node[end, label=right:
{R1}] {}
edge from parent
node[above] {$1-R$}
node[below] {}}
child {node[end, label=right:
{R2}] {}
edge from parent
node[above] {$R$}
node[below] {}}
child[missing]
edge from parent
node[above] {$1-D$}
node[below] {}
}
child {%empty child on level 2
child{
node[end, label=right:
{D1}] {}
edge from parent
node[above] {$D$}
node[below] {}
}
}
child[missing]
edge from parent
node[above] {}
node[below] {$1-u$}
}
child {
node{U1}
child{% empty child on level 2
child{
node[end, label=right:
{$T$}](e) {}
edge from parent
node[above] {}
node[below] {}
}
}
edge from parent
node[above] {$u$}
node[below] {}
};
\matrix[
matrix of nodes,
anchor=m-4-1.west,
xshift=1cm,
nodes={inner xsep=\tabcolsep,minimum height=\sibdist}
](m)at(n-1-1-1){%
c&b&a&c&bd&a\\
a&c&b&a&a&c\\
x&g&f&c&d&b\\%[1cm]
ta&g&a&x&p&d\\
};
\foreach \i in {1,...,6}\node[draw,fit=(m-1-\i) (m-2-\i) (m-3-\i) (m-4-\i),inner sep=-.5\pgflinewidth](c\i){};
\foreach \i in {1,...,3}\draw([yshift=-.5*\sibdist]m-\i-1.center-|c1.west)--([yshift=-.5*\sibdist]m-\i-1.center-|c6.east);
\end{tikzpicture}