How to horizontally top-align multiple \tikzpicture's without using \hspace?
If you have many TikZ pictures you want align at the top you can use the align at top
style defined as
\tikzset{align at top/.style={baseline=(current bounding box.north)}}
The \Tree
macros of tikz-qtree
check whether they are already in a PGF picture so you could simply write
\Tree [.A B C [.D E ] ]
\Tree [.F [.G H ] I ]
\Tree [.J [.K [.L M M ] N ] O ]
\Tree [.F [.G H ] I ]
and they would automatically align at the top.
But they are not spread along the horizontal space.
The alignment can be fixed by including \hfill
s between the \Tree
s/the TikZ pictures.
An additional \hfill
is needed before the first and the last element.
To save typing I have introduced a environment spreadTrees
that adds a \hfill
before every PGF/TikZ picture and appends a last \hfill\null
at the end of the environment. The center
environment is included, though actually only needed for the vertical space the environment includes.
You can do the same without the center
environment (for floats). This is what the spreadTrees*
environment does. There is a slight different horizontal placement, though.
\hspace*{\fill}
instead of \hfill\null
Here’s the definition of the environments according to the comment by Ignasi and the answer by egreg:
\newenvironment{spreadTrees}{%
\expandafter\def\expandafter\pgfpicture\expandafter{\expandafter\hfill\pgfpicture}%
\center}{\hspace*{\fill}\par\endcenter}
\newenvironment{spreadTrees*}{%
%\noindent % (if not only used inside floats)
\expandafter\def\expandafter\pgfpicture\expandafter{\expandafter\hfill\pgfpicture}%
}{\hspace*{\fill}}
Code
\documentclass{article}
\usepackage[showframe]{geometry}
\usepackage{tikz}
\usepackage{tikz-qtree}
\newenvironment{spreadTrees}{%
\expandafter\def\expandafter\pgfpicture\expandafter{\expandafter\hfill\pgfpicture}%
\center}{\hfill\null\endcenter}
\newenvironment{spreadTrees*}{%
%\noindent % (if not only used inside floats)
\expandafter\def\expandafter\pgfpicture\expandafter{\expandafter\hfill\pgfpicture}%
}{\hfill\null}
\begin{document}%
\begin{spreadTrees}
\Tree [.A B C [.D E ] ]
\Tree [.F [.G H ] I ]
\Tree [.J [.K [.L M M ] N ] O ]
\Tree [.F [.G H ] I ]
\end{spreadTrees}
\hrule
\begin{figure}[h]
\begin{spreadTrees*}
\Tree [.A B C [.D E ] ]
\Tree [.F [.G H ] I ]
\Tree [.J [.K [.L M M ] N ] O ]
\Tree [.F [.G H ] I ]
\end{spreadTrees*}
\caption{(Figure above)}
\end{figure}
\end{document}
Output
Perhaps the following will help:
\documentclass{article}
\usepackage{amsmath}
\usepackage[showframe]{geometry}
\usepackage{tikz}
\usepackage{tikz-qtree}
\begin{document}
\begin{center}
hello\\[-\baselineskip]
\raisebox{-\height}{\begin{tikzpicture}
\Tree [.A B C [.D E ] ]
\end{tikzpicture}}
\raisebox{-\height}{\begin{tikzpicture}
\Tree [.F [.G H ] I ]
\end{tikzpicture}}
\raisebox{-\height}{\begin{tikzpicture}
\Tree [.J [.K [.L M M ] N ] O ]
\end{tikzpicture}}
\raisebox{-\height}{\begin{tikzpicture}
\Tree [.F [.G H ] I ]
\end{tikzpicture}}\\
world
\end{center}
\end{document}