我想将不同高度的数字在它们所在的块内垂直居中。
我使用“tikz-qtree”作为树,并且我希望“5”树和右箭头垂直对齐。
\documentclass[a4paper]{article}
\usepackage{mathtools}
\usepackage{tikz-qtree}
\begin{document}
\tikzset{every tree node/.style={minimum width=2em,draw,circle},
blank/.style={draw=none},
edge from parent/.style=
{draw, edge from parent path={(\tikzparentnode) -- (\tikzchildnode)}},
level distance=1.5cm}
\begin{center}
\begin{tikzpicture}
\Tree
[.9
[.7 ]
\edge[blank]; \node[blank]{};
]
\end{tikzpicture},
\begin{tikzpicture}
\Tree
[.5 ]
\end{tikzpicture}
$\Longrightarrow$
\end{center}
\end{document}
答案1
一种方法是将每个图表放置在\vcenter
:
\documentclass{article}
\usepackage{tikz,tikz-qtree}
\tikzset{blank/.style={white},every tree node/.style={draw,circle}}
\begin{document}
\begin{center}
\(
\vcenter{\hbox{\begin{tikzpicture}
\Tree
[.9
[.7 ]
\edge[blank]; \node[blank]{};
]
\end{tikzpicture}}},
\vcenter{\hbox{\begin{tikzpicture}
\Tree
[.5 ]
\end{tikzpicture}}}
\Longrightarrow \)
\end{center}
\end{document}
另一种方法是指定baseline
每个选项tikzpicture
,请参阅将 Tikz 图片与周围的文本对齐例如。
要将上述解决方案打包成宏,您可以使用
\usepackage{environ}
\NewEnviron{tikzcenter}{\ensuremath\vcenter{\hbox{\begin{tikzpicture}\BODY\end{tikzpicture}}}}
用作
\documentclass{article}
\usepackage{tikz,tikz-qtree}
\tikzset{blank/.style={white},every tree node/.style={draw,circle}}
\usepackage{environ}
\NewEnviron{tikzcenter}{\ensuremath\vcenter{\hbox{\begin{tikzpicture}\BODY\end{tikzpicture}}}}
\begin{document}
\begin{center}
\(
\begin{tikzcenter}
\Tree
[.9
[.7 ]
\edge[blank]; \node[blank]{};
]
\end{tikzcenter},
\begin{tikzcenter}
\Tree
[.5 ]
\end{tikzcenter}
\Longrightarrow \)
\end{center}
\end{document}
答案2
这是一种tikz
方法。
\documentclass{article}
\usepackage{tikz-qtree}
\begin{document}
\begin{center}
\begin{tikzpicture}[baseline=(current bounding box.center)] %%% this added
\Tree
[.9
[.7 ]
\edge[draw=none]; \node[draw=none]{};
]
\end{tikzpicture},
\begin{tikzpicture}[baseline] %%% this added
\Tree
[.5 ]
\end{tikzpicture}
$\Longrightarrow$
\end{center}
\end{document}
和adjustbox
。
\documentclass{article}
\usepackage{tikz-qtree}
\usepackage{adjustbox}
\usepackage{etoolbox}
\BeforeBeginEnvironment {tikzpicture}{\begin{adjustbox}{valign=m}}
\AfterEndEnvironment {tikzpicture}{\end{adjustbox}}
\begin{document}
\begin{center}
\begin{tikzpicture}
\Tree
[.9
[.7 ]
\edge[draw=none]; \node[draw=none]{};
]
\end{tikzpicture}
,
\begin{tikzpicture}
\Tree
[.5 ]
\end{tikzpicture}
$\Longrightarrow$
\end{center}
\end{document}