3

This question is building off the solution found here

I was attempting to place a figure within the enumerate environment and for the enumerate number to show up at the top, but I get the error Not in outer par mode

Can you help me fix this error so that my file can compile correctly and I get the Figure with caption to show up correctly?

CODE:

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

\begin{document}
%---------------------------------------------------------------%
\begin{enumerate}
\item
\raisebox{\heightof{1} - \height}{%
\begin{figure}[!htbp]
\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}%
\caption{Histogram}
\end{figure}}

\item

\end{enumerate}
\end{document}
1
  • it isn't the enumerate it is the raisebox that doesn't allow floats, it would make no sense to allow it as the only reason for the figure environment is to specify that the figure can move ("float") to a different position to help with page breaking, but a \raisebox needs some fixed content to raise. Sep 13, 2016 at 23:02

2 Answers 2

2

MAybe you want this (I added an \fbox around the picture to visualise the control we have on the vertical placement of the picture):

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

\begin{document}
%---------------------------------------------------------------%
\begin{enumerate}
\item\leavevmode
\raisebox{\baselineskip}{%
\begin{minipage}[t]{\linewidth}
\begin{figure}[H]%
\centering\setlength\fboxsep{0pt}
\fbox{\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}}%
\caption{Histogram}
\end{figure}%
\end{minipage}
}

\item

\end{enumerate}

\end{document} 

enter image description here

3

I suggest to do it this way:

\usepackage{caption}
...
\begin{document}
...
\begin{enumerate}
\item \mbox{}
  \begin{center}
    \begin{tikzpicture}
    ... code of picture ...
    \end{tikzpicture}\\
    \captionof{caption}{Histogram}%
    \label{fig:histogram}
  \end{center}
\item
\end{enumerate}
...
As you can see in Fig.~\ref{fig:histogram}, \dots

The figure environement tells LaTeX that it is allowed to place the contents of the environment somewhere at the top or bottom of the page, or even on another page. 'Capturing' it in a box (\raisebox) contradicts this purpose and leads to an error.

enter image description here

4
  • 1
    You might want to suggest that the OP can use \captionof{} since wanting a caption is the most common reason people try to use floats when they don't want things to float.
    – cfr
    Sep 13, 2016 at 23:06
  • @cfr Good idea, done.
    – gernot
    Sep 14, 2016 at 0:02
  • @gernot, thank you for your response. I cannot compile your code as I get the error, ! Package caption Error: No float type 'Histogram' defined. Can you help me fix this error?
    – Joe
    Sep 14, 2016 at 3:47
  • 1
    @joe Sorry, somehow the \captionof command lost its first argument. Corrected now. Thanks for pointing it out!
    – gernot
    Sep 14, 2016 at 6:57

You must log in to answer this question.

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