tikz-qtree 中的边缘过长

tikz-qtree 中的边缘过长

我有这棵树:

\documentclass{standalone}
\usepackage{tikz}
\usepackage{tikz-qtree}

\begin{document}
\begin{tikzpicture}
\tikzstyle{level 1}=[sibling distance=50pt]
\tikzstyle{level 2}=[sibling distance=30pt]
\tikzstyle{level 3}=[sibling distance=25pt]
\tikzstyle{level 4}=[sibling distance=15pt]
\begin{scope}[xshift=3cm]
\Tree [.\node(a1){$\ \Delta^1$};
\edge node[auto=right,scale=0.7]{$q_1$};
[.{ $q_1\cdot\Delta^2$}
    \edge node[auto=right,scale=0.7]{$q_5$};
    {}
    \edge node[auto=left,scale=0.7]{$q_6$};
    [.{$q_6\cdot\Delta^7$} % TODO add Cartesian product here
        \edge node[auto=right,scale=0.7]{$q_{13}$};
        {}
        \edge node[auto=left,scale=0.7]{$q_{14}$}; 
        {}
        \edge node[auto=left,scale=0.7]{$q_{15}$}; 
        {}
        ]
    ]
%
\edge node[auto=left,scale=0.7]{$q_2$};
[. {$q_2\cdot\Delta^3$}
\edge node[auto=right,scale=0.7]{$q_7$};
        {} 
\edge node[auto=left,scale=0.7]{$q_8$};
        {} 
]
]
\end{scope}
\begin{scope}[xshift=9cm]
\Tree [.\node(a2){$\ \Delta^2$};
\edge node[auto=right,scale=0.7]{$q_3$};
[. {$q_3\cdot\Delta^5$}
\edge node[auto=right,scale=0.7]{$q_9$};
        {} 
\edge node[auto=left,scale=0.7]{$q_{10}$};
        {} 
]
\edge node[auto=left,scale=0.7]{$q_{4}$};
[. {$q_4\cdot\Delta^6$}
\edge node[auto=right,scale=0.7]{$q_{11}$};
        {} 
\edge node[auto=left,scale=0.7]{$q_{12}$};
        {} 
]
]
\end{scope}
\path (a1) -- node {$\times$} (a2);
\end{tikzpicture}
\end{document}

但是,从这张图片中可以看出,与同一级别的其他边缘相比,边缘$q_1\cdot\Delta^2$太长了。有什么办法可以解决这个问题吗?谢谢!

在此处输入图片描述

答案1

如果您不介意切换到该forest包,Gonzalo 的答案是一个不错的方法。但是,如果您想保留已经编写的代码并继续使用tikz-qtree,则可以\vphantom{$q_6\cdot\Delta^7$}在该边之后的节点中添加。这将在该节点中添加与其姊妹节点中的垂直空间高度相同的不可见垂直空间。

结果

在此处输入图片描述

代码

\documentclass{standalone}
\usepackage{tikz}
\usepackage{tikz-qtree}

\begin{document}
\begin{tikzpicture}
\tikzstyle{level 1}=[sibling distance=50pt]
\tikzstyle{level 2}=[sibling distance=30pt]
\tikzstyle{level 3}=[sibling distance=25pt]
\tikzstyle{level 4}=[sibling distance=15pt]
\begin{scope}[xshift=3cm]
\Tree [.\node(a1){$\ \Delta^1$};
\edge node[auto=right,scale=0.7]{$q_1$};
[.{ $q_1\cdot\Delta^2$}
    \edge node[auto=right,scale=0.7]{$q_5$};
    {\vphantom{$q_6\cdot\Delta^7$}}
    \edge node[auto=left,scale=0.7]{$q_6$};
    [.{$q_6\cdot\Delta^7$} % TODO add Cartesian product here
        \edge node[auto=right,scale=0.7]{$q_{13}$};
        {}
        \edge node[auto=left,scale=0.7]{$q_{14}$}; 
        {}
        \edge node[auto=left,scale=0.7]{$q_{15}$}; 
        {}
        ]
    ]
%
\edge node[auto=left,scale=0.7]{$q_2$};
[. {$q_2\cdot\Delta^3$}
\edge node[auto=right,scale=0.7]{$q_7$};
        {} 
\edge node[auto=left,scale=0.7]{$q_8$};
        {} 
]
]
\end{scope}
\begin{scope}[xshift=9cm]
\Tree [.\node(a2){$\ \Delta^2$};
\edge node[auto=right,scale=0.7]{$q_3$};
[. {$q_3\cdot\Delta^5$}
\edge node[auto=right,scale=0.7]{$q_9$};
        {} 
\edge node[auto=left,scale=0.7]{$q_{10}$};
        {} 
]
\edge node[auto=left,scale=0.7]{$q_{4}$};
[. {$q_4\cdot\Delta^6$}
\edge node[auto=right,scale=0.7]{$q_{11}$};
        {} 
\edge node[auto=left,scale=0.7]{$q_{12}$};
        {} 
]
]
\end{scope}
\path (a1) -- node {$\times$} (a2);
\end{tikzpicture}
\end{document}

答案2

Adam Liter 使用 给出了一个很好的答案tikz-qtree,但在这里我想展示一种使用强大forest包;特别注意更短、更干净的代码;问题中的直接问题在这里以不同的方式解决,使用节点的文本高度键,因此它们都具有相同的高度,而不管它们的内容如何:

\documentclass{standalone}
\usepackage{forest}

\begin{document}

\forestset{
leftedge/.style={
  edge label={node[inner sep=1pt,midway,auto,swap,font=\scriptsize]{$#1$}}
  },
rightedge/.style={
  edge label={node[inner sep=1pt,midway,auto,font=\scriptsize]{$#1$}}
  }
}
\begin{forest}
for tree={
  parent anchor=south,
  child anchor=north,
  math content,
  text height=2ex,
  l sep=25pt,
  s sep=20pt,
  where level={3}{s sep=30pt}{}
}
[,phantom,
  [\ \Delta^1
    [q_{1}\cdot\Delta^{2},leftedge={q_{1}}
      [,leftedge={q_{5}}
      ]
      [q_{6}\cdot\Delta^{7},rightedge={q_{6}}
        [,leftedge={q_{13}}]
        [,rightedge={q_{14}}]
        [,rightedge={q_{15}}]
      ]
    ]
    [q_{2}\cdot\Delta^{3},rightedge={q_{2}}
      [,leftedge={q_{7}}
      ]
      [,rightedge={q_{8}}
      ]
    ]
  ]
  [\ \Delta^2
    [q_{3}\cdot\Delta^{5},leftedge={q_{3}}
      [,leftedge={q_{9}}
      ]
      [,rightedge={q_{10}}
      ]
    ]
    [q_{4}\cdot\Delta^{6},rightedge={q_{4}}
      [,leftedge={q_{11}}
      ]
      [,rightedge={q_{12}}
      ]
    ]
  ]
]
\end{forest}

\end{document}

在此处输入图片描述

相关内容