Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

The use case I'm having is the following:

\documentclass{article}

\usepackage{amsmath}
\usepackage[showframe]{geometry}
\usepackage{qtree}

\begin{document}

\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{document}

which due to qtrees clever horizontal alignment produces

qtree

Now, as qtree is limited to 5 branches, I'm using tikz-qtree and therefore essentially a tikzpicture:

\documentclass{article}

\usepackage{amsmath}    
\usepackage[showframe]{geometry}    
\usepackage{tikz}
\usepackage{tikz-qtree}

\begin{document}

\begin{center}
\begin{tikzpicture}
    \Tree [.A B C [.D E ] ]
\end{tikzpicture}
\begin{tikzpicture}
    \Tree [.F [.G H ] I ]
\end{tikzpicture}
\begin{tikzpicture}
    \Tree [.J [.K [.L M M ] N ] O ]
\end{tikzpicture}
\begin{tikzpicture}
    \Tree [.F [.G H ] I ]
\end{tikzpicture}
\end{center}

\end{document}

which produces

tikz-qtree

How can I achieve the same spacing as in qtree? So that each space between two tikzpictures and to the borders is the same, without using any manual measurements. Also, alignment should be at the top, not bottom.

share|improve this question

2 Answers 2

up vote 3 down vote accepted

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 \hfills between the \Trees/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 \hfillbefore 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

enter image description here

share|improve this answer
    
I'm impressed! That's really useful. :) –  neo Mar 4 '13 at 15:37
    
Some time ago, egreg suggested me to use \hspace*{\fill} instead of \hfill\null or similars at end of line. –  Ignasi Mar 4 '13 at 19:58
    
@Ignasi The definition of \null is \mbox{}, so the last \hfill is not “at the end of a paragraph”. Anyhow, I have updated the answer with a \hspace* solution. –  Qrrbrbirlbel Mar 4 '13 at 20:40

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}

aligned trees

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.