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?

1
  • 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
    Commented Nov 6, 2010 at 22:12

4 Answers 4

13

Here is one way:

\item \leavevmode\vadjust{\vspace{-\baselineskip}}\newline
\begin{tikzpicture}
2
  • 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 tex.stackexchange.com/questions/5413/… (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 :) ). Commented Nov 16, 2010 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
    Commented Dec 28, 2012 at 21:42
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.

0
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}
1
  • 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
    Commented Nov 6, 2010 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.

1
  • Nice, but why does this work?
    – Junglemath
    Commented Feb 20, 2023 at 18:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .