![列表内的自定义环境存在问题](https://linux22.com/image/426341/%E5%88%97%E8%A1%A8%E5%86%85%E7%9A%84%E8%87%AA%E5%AE%9A%E4%B9%89%E7%8E%AF%E5%A2%83%E5%AD%98%E5%9C%A8%E9%97%AE%E9%A2%98.png)
我正在尝试在列表中使用自定义的类似定理的环境(使用包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}