tikz矩阵和树的垂直对齐

tikz矩阵和树的垂直对齐

我想在同一个图形环境中将表格显示为 tikz 矩阵,旁边是 tikz 树。对于同一图形环境中有两棵树的情况,使用每个 tikzpicture 中的基线选项可以将两张图片对齐在顶部,但对于这种情况则不行!为什么会这样?

\documentclass{article}![enter image description here][1]

\usepackage{tikz}
\usetikzlibrary{matrix}
\usepackage{booktabs}

\begin{document}

\tikzset{align at top/.style={baseline=(current bounding box.north)}}
\begin{figure}
\centering
\begin{tikzpicture}[baseline]
\matrix (m) [matrix of nodes,
nodes={ minimum height=6mm, minimum width=2cm,align=center}
]{
\toprule
$CT$&freq\\ \midrule
$\{b,d,e\}$ &4\\ 
$\{c,e,f\}$ &3\\ 
$\{a,c\}$&4\\ 
$\{a,g\}$&4\\
$\{f\}$&4\\
$\{g\}$&4\\  
\bottomrule \\
};
\end{tikzpicture}
\hspace{4ex}
\begin{tikzpicture}[baseline,
        fp/.style={rectangle, draw, rounded corners=1mm,
        text centered, anchor=north},
        inact/.style={rectangle, draw=black!50, rounded corners=1mm,
        text centered, anchor=north, text=black!50},
        level 1/.style={sibling distance=1.9cm}, 
        level distance=0.8cm, growth parent anchor=south,
        transform shape]

        \node [fp] (n){$\emptyset$}
        child {[sibling distance=1.3cm]
            node [fp] (a) {$a:2$}
            child {
                node [fp] (ac) {$c:1$}
            }
            child {
                node [fp] (ab) {$b:1$}
            }
        }   
        child {[sibling distance=1.2cm]
            node[fp] (c) {$c:1$}
            child{
                node[fp] (ce) {$e:1$}
                child {
                    node[fp] (cef) {$f:1$}
                }
            }
        }
        child {
            node[fp] (e) {$e:1$}
            child {
                node[fp] (eb) {$b:1$}
                child {
                    node[fp] (ebd) {$d:1$}
                }
            }    
        };
    \end{tikzpicture}
    \caption{The encoding of a database (left table) and the induced tree representation of the  database (right).}
    \label{fig:tree}
\end{figure}

\end{document}

输出

答案1

如果您添加baseline=(current bounding box.north)到两个tikzpicture环境中它就会起作用。

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{matrix}
\usepackage{booktabs}

\begin{document}

\tikzset{align at top/.style={baseline=(current bounding box.north)}}
\begin{figure}
\centering
\begin{tikzpicture}[baseline = (current bounding box.north)]
\matrix (m) [matrix of nodes,
nodes={ minimum height=6mm, minimum width=2cm,align=center}
]{
\toprule
$CT$&freq\\ \midrule
$\{b,d,e\}$ &4\\
$\{c,e,f\}$ &3\\
$\{a,c\}$&4\\
$\{a,g\}$&4\\
$\{f\}$&4\\
$\{g\}$&4\\
\bottomrule \\
};
\end{tikzpicture}
\hspace{4ex}
\begin{tikzpicture}[baseline=(current bounding box.north),
        fp/.style={rectangle, draw, rounded corners=1mm,
        text centered, anchor=north},
        inact/.style={rectangle, draw=black!50, rounded corners=1mm,
        text centered, anchor=north, text=black!50},
        level 1/.style={sibling distance=1.9cm},
        level distance=0.8cm, growth parent anchor=south,
        transform shape]

        \node [fp] (n){$\emptyset$}
        child {[sibling distance=1.3cm]
            node [fp] (a) {$a:2$}
            child {
                node [fp] (ac) {$c:1$}
            }
            child {
                node [fp] (ab) {$b:1$}
            }
        }
        child {[sibling distance=1.2cm]
            node[fp] (c) {$c:1$}
            child{
                node[fp] (ce) {$e:1$}
                child {
                    node[fp] (cef) {$f:1$}
                }
            }
        }
        child {
            node[fp] (e) {$e:1$}
            child {
                node[fp] (eb) {$b:1$}
                child {
                    node[fp] (ebd) {$d:1$}
                }
            }
        };
    \end{tikzpicture}
    \caption{The encoding of a database (left table) and the induced tree representation of the  database (right).}
    \label{fig:tree}
\end{figure}

\end{document}

在此处输入图片描述

相关内容