3

I would like some help to get the enumerate item number appear at the top of the tikzpicture like this:

enter image description here

I put the tikzpicture in a minipage environment, but was not successful.

Code:

\documentclass[11pt]{book}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}

\begin{document}
%---------------------------------------------------------------%
\begin{enumerate}
\item
\begin{minipage}{\linewidth}
            \centering
\begin{tikzpicture}
  \begin{axis}[
    ymin=0, ymax=10,
    xmin=-0.5, xmax=9,
    xtick={0, ..., 7},
    ytick={0, ..., 9},
    axis x line=bottom,
    axis y line=left,
    area style,
  ]
    \addplot+[ybar interval] plot coordinates {
      (-0.50, 0) (0.5, 9) (1.5, 7) (2.5, 0) (3.5, 2) (4.5, 6) (5.5, 0)
    };
    \path
      \foreach[count=\i from 0] \v in {0, 9, 7, 0, 2, 6, 0} {
        (\i, \v) node[below] {\v}
      }
      (axis description cs:1, 0) node[above left, align=center] {grey\\level}
    ;
    \draw[ultra thick, blue]
      \foreach \x in {0, 3, 6, 7} {
        (\x -.5, 0) -- node[above=2pt, black] {0} (\x + .5, 0)
      }
    ;
  \end{axis}
\end{tikzpicture}
\end{minipage}

\item 

\end{enumerate}
\end{document}

Thanks.

3 Answers 3

3

Solution with \raisebox:

\documentclass[11pt]{book}
\usepackage{calc}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}

\begin{document}
%---------------------------------------------------------------%
\begin{enumerate}
\item
\raisebox{\heightof{1} - \height}{%
  \begin{minipage}{\linewidth}
    \centering
    \begin{tikzpicture}
      \begin{axis}[
       ymin=0, ymax=10,
        xmin=-0.5, xmax=9,
        xtick={0, ..., 7},
        ytick={0, ..., 9},
        axis x line=bottom,
        axis y line=left,
        area style,
      ]
        \addplot+[ybar interval] plot coordinates {
          (-0.50, 0) (0.5, 9) (1.5, 7) (2.5, 0) (3.5, 2) (4.5, 6) (5.5, 0)
        };
        \path
          \foreach[count=\i from 0] \v in {0, 9, 7, 0, 2, 6, 0} {
            (\i, \v) node[below] {\v}
          }
          (axis description cs:1, 0)
          node[above left, align=center] {grey\\level}
        ;
        \draw[ultra thick, blue]
          \foreach \x in {0, 3, 6, 7} {
            (\x -.5, 0) -- node[above=2pt, black] {0} (\x + .5, 0)
          }
        ;
      \end{axis}
    \end{tikzpicture}%
  \end{minipage}%
}

\item

\end{enumerate}
\end{document}

Result

6

If you are not against additional packages, adjustbox can do the work easy:

\documentclass[11pt]{book}
\usepackage{pgfplots}

\usepackage{adjustbox}

\pgfplotsset{compat=newest}

\begin{document}
%---------------------------------------------------------------%
\begin{enumerate}
\item\adjustbox{valign=t}{
\begin{minipage}{\linewidth}
            \centering
\begin{tikzpicture}
  \begin{axis}[
    ymin=0, ymax=10,
    xmin=-0.5, xmax=9,
    xtick={0, ..., 7},
    ytick={0, ..., 9},
    axis x line=bottom,
    axis y line=left,
    area style,
  ]
    \addplot+[ybar interval] plot coordinates {
      (-0.50, 0) (0.5, 9) (1.5, 7) (2.5, 0) (3.5, 2) (4.5, 6) (5.5, 0)
    };
    \path
      \foreach[count=\i from 0] \v in {0, 9, 7, 0, 2, 6, 0} {
        (\i, \v) node[below] {\v}
      }
      (axis description cs:1, 0) node[above left, align=center] {grey\\level}
    ;
    \draw[ultra thick, blue]
      \foreach \x in {0, 3, 6, 7} {
        (\x -.5, 0) -- node[above=2pt, black] {0} (\x + .5, 0)
      }
    ;
  \end{axis}
\end{tikzpicture}
\end{minipage}
}

\item 

\end{enumerate}
\end{document}

enter image description here

3
  • 2
    I think there should be a % after in \adjustbox{valign=t}{% and \end{minipage}%. Sep 13, 2016 at 19:39
  • 1
    @PeterGrill OP's picture suggests that even an additional vertical space may be needed. Sep 13, 2016 at 19:45
  • 1
    Picture probably wat not meant to be precie. But, I think the % will suppress spurious horizontal space -- don't think it affects vertical space at all. Sep 13, 2016 at 19:46
4

Use the baseline key:

...
\begin{enumerate}
\item
    \begin{tikzpicture}[baseline = (current bounding box.north)]
      \begin{axis}[
...

enter image description here

2
  • Good alternate suggestion. However, the OP also invokes \centering. Sep 13, 2016 at 19:48
  • Yes, but I wasn't sure if that was important or just a side effect of his attempt to use minipage.
    – Derek
    Sep 13, 2016 at 19:53

You must log in to answer this question.

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