\documentclass{article}
\usepackage{amssymb}
\usepackage{tikz}
\begin{document}
\begin{itemize}
\item \textbf{HEAPIFY(A, 7)}\\
\begin{tikzpicture}
\node{4}
child { node {21} }
child { node {14} };
\end{tikzpicture}
$\rightsquigarrow$
\begin{tikzpicture}
\node{21}
child { node {4} }
child { node {14} };
\end{tikzpicture}
\end{itemize}
\end{document}
上述代码产生以下输出:
我想根据图表的总高度(而不是 14 和 4 的高度)将箭头垂直居中。该怎么做?
答案1
有几种选择,也许最简单的是
- 适当地设置s
baseline
的tikzpicture
,或者 - 用于
remember picture
添加overlay
包含箭头的路径。
前两个示例将图的基线设置为底部节点,另两个示例将其基线设置为中心。
\documentclass{article}
\usepackage{amssymb}
\usepackage{tikz}
\begin{document}
\begin{itemize}
\item \textbf{HEAPIFY(A, 7)}\\
\begin{tikzpicture}[baseline={(14.base)}]
\node{4}
child { node {21} }
child { node (14){14} };
\end{tikzpicture}
$\rightsquigarrow$
\begin{tikzpicture}[baseline={(14.base)}]
\node{21}
child { node {4} }
child { node (14){14} };
\end{tikzpicture}
\item \textbf{HEAPIFY(A, 7)}\\
\begin{tikzpicture}[remember picture]
\node{4}
child { node {21} }
child { node (14){14} };
\end{tikzpicture}
$\hphantom{\rightsquigarrow}$
\begin{tikzpicture}[remember picture]
\node{21}
child { node (4) {4} }
child { node {14} };
\path[overlay] (14) -- node{$\rightsquigarrow$}(4);
\end{tikzpicture}
\item \textbf{HEAPIFY(A, 7)}\\
\begin{tikzpicture}[baseline={(aux.base)}]
\node (4) {4}
child { node {21} }
child { node (14){14} };
\path (4) -- node(aux){\vphantom{x}}(14) ;
\end{tikzpicture}
$\rightsquigarrow$
\begin{tikzpicture}[baseline={(aux.base)}]
\node(21){21}
child { node {4} }
child { node (14){14} };
\path (21) -- node(aux){\vphantom{x}} (14) ;
\end{tikzpicture}
\item \textbf{HEAPIFY(A, 7)}\\
\begin{tikzpicture}[remember picture]
\node(4){4}
child { node {21} }
child { node (14){14} };
\end{tikzpicture}
$\hphantom{\rightsquigarrow}$
\begin{tikzpicture}[remember picture]
\node(21){21}
child { node (4') {4} }
child { node {14} };
\path[overlay] (4) -- node(aux1){\vphantom{x}}(14)
(21) -- node(aux2){\vphantom{x}} (4')
(aux1) -- node{$\rightsquigarrow$}(aux2);
\end{tikzpicture}
\end{itemize}
\end{document}