# Why aren't qtrees aligned horizontally if they are at the end of a theorem with qed symbol enabled?

\documentclass{article}

\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{thmtools}
\usepackage{qtree}

\declaretheorem[name=Example,qed=\ensuremath{\triangleleft}]{example}
\declaretheorem[name=Example2]{example2}

\begin{document}

\begin{example}
Bla:

\Tree [.cons [.s [.s [.s 0 ] ] ] [.cons [.s 0 ] nil ] ]
\end{example}

\begin{example2}
Bla:

\Tree [.cons [.s [.s [.s 0 ] ] ] [.cons [.s 0 ] nil ] ]
\end{example2}

\end{document}

The second one is centered, the first isn't. Why and what to do?

In the end I'd like to align several trees horizontally like that:

\begin{example2}
Bla:

\Tree [.nil ]
\Tree [.cons 0 nil ]
\Tree [.cons [.s [.s [.s 0 ] ] ] [.cons [.s 0 ] nil ] ]
\end{example2}
-

From the documentation of qtree:

Qtree attempts to align the topmost label of the tree with the baseline of the text, similarly to the eﬀect of the [t] option for \parbox alignment. To center trees vertically on the baseline, enclose the entire tree in a tabular environment.

You have two strategies:

\documentclass{article}

\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{thmtools}
\usepackage{qtree}

\declaretheorem[name=Example,qed=\ensuremath{\triangleleft}]{example}

\begin{document}

\begin{example}
Bla:

\raisebox{\depth}{%
\Tree [.cons [.s [.s [.s 0 ] ] ] [.cons [.s 0 ] nil ] ]%
}
\end{example}

\end{document}

The second strategy consists in using tikz-qtree that shares the same syntax with qtree, but is much more powerful:

\documentclass{article}

\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{thmtools}
\usepackage{tikz-qtree}

\declaretheorem[name=Example,qed=\ensuremath{\triangleleft}]{example}

\begin{document}

\begin{example}
Bla:

\begin{tikzpicture}
\Tree [.cons [.s [.s [.s 0 ] ] ] [.cons [.s 0 ] nil ] ]
\end{tikzpicture}
\end{example}

\end{document}

The placement is not really perfect in both cases, so some small adjustment may be necessary.

For centering, use an equation* environment and the \qedhere feature:

\documentclass{article}

\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{thmtools}
\usepackage{tikz-qtree}

\declaretheorem[name=Example,qed=\ensuremath{\triangleleft}]{example}

\begin{document}

\begin{example}
Bla:

\begin{equation*}
\begin{tikzpicture}
\Tree [.cons [.s [.s [.s 0 ] ] ] [.cons [.s 0 ] nil ] ]
\end{tikzpicture}}\qedhere
\end{equation*}
\end{example}

\end{document}

-
I see no difference for both strategies. The tree is still aligned at the left and not centered like when not using a qed symbol. –  neo Feb 27 '13 at 21:30
@neo I've added how to center the tree –  egreg Feb 27 '13 at 21:38
Do you also know why the tree isn't centered? –  neo Feb 28 '13 at 19:29
Regarding the centering, unfortunately using an equation breaks when there are many trees which would otherwise just break to a new line. For the moment, I just work around it by using my original code and adding a white line at the end. The qed symbol then is a bit below the lowest tree leaf... –  neo Mar 1 '13 at 12:14
@neo I wouldn't rely on automatic line breaking in this case. –  egreg Mar 1 '13 at 12:21