
我想在描述項中包含內聯列表,所以我使用了這個 非常好的答案。不幸的是,這個駭客錯誤地破壞了我的定理環境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
都是可用的。