19

I've drawn a tree using pgf/tikz and am trying to place it in an enumerated list. A snippet of my complete code is as follows:

\subsection*{Problem 2}
\begin{enumerate}[1)]
\item Max heap
\item
\begin{tikzpicture}[level/.style={sibling distance=50mm/#1}]
\node [circle,draw] (z){98}
  child {node [circle,draw] (a) {80}
    child {node [circle,draw] (b) {23}
      child {node [circle,draw] (c) {18}}
      child {node [circle,draw] (d) {21}}
    }
    child {node [circle,draw] (g) {48}}
  }
  child {node [circle,draw] (j) {79}
    child {node [circle,draw] (k) {76}}
    child {node [circle,draw] (l) {29}}
};
\end{tikzpicture}
\end{enumerate}

My problem is the list number is aligned with the bottom of the tikzpicture. How would I go about aligning it with the top of the tikzpicture?

lockstep
  • 250,273
  • 2
    A complete (compilable) minimal example would be better. For example until now it's not clear which of the packages enumerate and enumitem has been used. – Stefan Kottwitz Nov 06 '10 at 22:12

4 Answers4

15

Just add baseline:

\begin{tikzpicture}[level/.style={sibling distance=50mm/#1},baseline]

without any arguments, baseline takes the value 0pt, which is great because your tree grows downwards. So y=0 is at the top.

Matthew Leingang
  • 44,937
  • 14
  • 131
  • 195
13

Here is one way:

\item \leavevmode\vadjust{\vspace{-\baselineskip}}\newline
\begin{tikzpicture}
  • 2
    +1 for this answer since, if anything else, it's a perfectly useful and acceptable solution for all parts of the question posed at http://tex.stackexchange.com/questions/5413/positioning-of-numbers-in-gb4e-sty-examples (for which I also answered, only in a rather different way, and for which I really wish I'd seen your answer here first -- it would have saved me very much time :) ). – Geoffrey Jones Nov 16 '10 at 15:37
  • 3
    The problem with this is that the number can wind up on a separate page from the tree. Matthew's solution solves that. – emily Dec 28 '12 at 21:42
10

You have to determine explicitly the base line of the tikzpicture environment with respect to the current line. See sample below.

\documentclass{article}
\usepackage{tikz}

\begin{document}
  \begin{enumerate}
    \item Max heap
    \item
      \begin{tikzpicture}[%
        baseline=(current bounding box.north),
        level/.style={sibling distance=50mm/#1}
      ]
        \node [circle,draw] (z){98}
        child {node [circle,draw] (a) {80}
          child {node [circle,draw] (b) {23}
            child {node [circle,draw] (c) {18}}
            child {node [circle,draw] (d) {21}}
          }
          child {node [circle,draw] (g) {48}}
        }
        child {node [circle,draw] (j) {79}
          child {node [circle,draw] (k) {76}}
            child {node [circle,draw] (l) {29}}
        };
      \end{tikzpicture}
  \end{enumerate}
\end{document}
  • 2
    Even better is baseline={($(current bounding box.north)-(0,1.6ex)$)} as it aligns the top of the picture with the top of the line. (Don’t forget to add \usetikzlibrary{calc}.) – Caramdir Nov 06 '10 at 22:15
7

A simple way to bring the number up (and this is what I always do) is to simply add ~\\ after \item.

Jimi Oke
  • 2,393