列表内的自定义环境存在问题

列表内的自定义环境存在问题

我正在尝试在列表中使用自定义的类似定理的环境(使用包enumitem),但收到错误消息

“LaTeX 错误:出现问题——可能缺少 \item。”

在列表之外,自定义环境可以正常工作。我该如何纠正这个问题?enumitem包和自定义环境之间是否存在不兼容问题?

以下是 MWE:

\documentclass{article}
\usepackage{amsthm} % theorem-like environments
\usepackage{framed} % vertical bar to the left of text of custom environment
\usepackage{enumitem} % enumerations
\usepackage{lipsum} % dummy text

% custom environment, with vertical bar to the left of text
\newtheorem{theoremzz}{Theorem}
\newenvironment{theorem}[1][]{\begin{theoremzz}[#1]\begin{leftbar}}{\end{leftbar}\end{theoremzz}}

% default environment
\newtheorem{proposition}[theoremzz]{Proposition}

\begin{document}

\begin{enumerate}

\item I want my custom \texttt{theorem} environment inside this item of the list.

\begin{theorem}
\lipsum[1][1-4]
\end{theorem}

As you can see, it did not work correctly.

\item Now an attempt with a default \texttt{proposition} environment:

\begin{proposition}
\lipsum[2][1-4]
\end{proposition}

It works!

\end{enumerate}

\noindent Outside the list, the custom environment works:

\begin{theorem}
\lipsum[3][1-4]
\end{theorem}

\end{document}

在此处输入图片描述

答案1

考虑将嵌套放置theorem在具有适当宽度的 内minipage\linewidth)。此装箱可避免错误。添加一些额外的间距以匹配周围的布局。

在此处输入图片描述

\documentclass{article}

\usepackage{amsthm,framed}
\usepackage{lipsum}

% Custom environment, with vertical bar to the left of text
\newtheorem{theoremzz}{Theorem}
\newenvironment{theorem}[1][]{\begin{theoremzz}[#1]\begin{leftbar}}{\end{leftbar}\end{theoremzz}}

% default environment
\newtheorem{proposition}[theoremzz]{Proposition}

\begin{document}

\begin{enumerate}

  \item I want my custom \texttt{theorem} environment inside this item of the list.

    \vspace{\topsep}

    \begin{minipage}{\linewidth}
      \begin{theorem}
        \lipsum[1][1-4]
      \end{theorem}
    \end{minipage}

    \vspace{\topsep}

    As you can see, it worked as expected.

  \item Now an attempt with a default \texttt{proposition} environment:

    \begin{proposition}
      \lipsum[2][1-4]
    \end{proposition}

    It works!

\end{enumerate}

\noindent Outside the list, the custom environment works:

\begin{theorem}
  \lipsum[3][1-4]
\end{theorem}

\end{document}

相关内容