在枚举环境中包含图形

在枚举环境中包含图形

这个问题是建立在解决方案在这里找到

我试图在枚举环境中放置一个图形,并让枚举数字显示在顶部,但我收到了错误Not in outer par mode

您能帮我修复这个错误,以便我的文件可以正确编译,并且带有标题的图形可以正确显示吗?

代码:

\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

也许你想要这个(我\fbox在图片周围添加了一个,以直观地显示我们对图片垂直放置的控制):

\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} 

在此处输入图片描述

答案2

我建议这样做:

\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

环境figure告诉 LaTeX 可以将环境内容放置在页面顶部或底部的某个位置,甚至是另一页上。将其“捕获”在框 ( \raisebox) 中违背了此目的并会导致错误。

在此处输入图片描述

相关内容