描述定义和 n 定理

描述定义和 n 定理

我想在描述项目中有内联列表,所以我用了这个 非常好的答案。不幸的是,这个黑客行为以错误破坏了我的定理环境something's wrong--perhaps a missing \item

以下是显示问题的 MWE:

\documentclass{article}
\makeatletter
%%inline listings in description items
\let\orig@item\item

\def\item{%
    \@ifnextchar{[}%
        {\lstinline@item}%
        {\orig@item}%
}

\begingroup
\catcode`\]=\active
\gdef\lstinline@item[{%
    \setbox0\hbox\bgroup
        \catcode`\]=\active
        \let]\lstinline@item@end
}
\endgroup

\def\lstinline@item@end{%
    \egroup
    \orig@item[\usebox0]%
}
\makeatother

\usepackage{listings}
\lstloadlanguages{C++}
    \lstset{
        basicstyle=\ttfamily,
    }
\usepackage{ntheorem}
\newtheorem{theorem}{Theorem}

\begin{document}
\section{Descriptions}
\begin{description}
\item[\lstinline{some verb text}] here is text
\item[\lstinline{some more}] more text
\end{description}
\section{Theorems}
\begin{theorem}[A Named theorem]
\begin{equation}
    e^x = \sum_{k=1}^\infty \frac{x^k}{k!}
\end{equation}         
\end{theorem}
\end{document}

有没有办法可以同时使用两者而不互相干扰?

答案1

我不会使用重新定义的方法\item,而是对想要使用的项目使用特殊命令\lstinline

\documentclass{article}

\usepackage{ntheorem}
\usepackage{listings}

\makeatletter
%%inline listings in description items; it's a modified version of \lstinline
\newcommand\lstinlineitem[1][]{%
  \setbox0=\hbox\bgroup % \lstinline has \leavevmode\bgroup
  \aftergroup\finish@off@lstinlineitem % do something after building the box
  \def\lst@boxpos{b}%
  \lsthk@PreSet\lstset{flexiblecolumns,#1}%
  \lsthk@TextStyle
  \@ifnextchar\bgroup{\afterassignment\lst@InlineG \let\@let@token}%
  \lstinline@}
\def\finish@off@lstinlineitem{\item[\usebox0]} % output the \item
\makeatother

\lstloadlanguages{C++}
    \lstset{
        basicstyle=\ttfamily,
    }

\newtheorem{theorem}{Theorem}

\begin{document}

\section{Descriptions}

\begin{description}
\lstinlineitem{some verb text} here is text

\lstinlineitem!some verb %text! here is text

\lstinlineitem[basicstyle=\sffamily]+some more+ more text
\end{description}
\section{Theorems}
\begin{theorem}[A Named theorem]
\begin{equation}
    e^x = \sum_{k=1}^\infty \frac{x^k}{k!}
\end{equation}         
\end{theorem}
\end{document}

如您所见,的完整语法\lstinline均可用。

在此处输入图片描述

相关内容