我有以下设置列表包,包括一个左边距以使行号保持在列内。
\documentclass{article}
\usepackage{color}
\usepackage[final]{listings}
\lstset{
language=Python,
numbers=left,
xleftmargin=2em,
frame=single,
captionpos=b,
basicstyle=\ttfamily\scriptsize,
breaklines=true,
tabsize=3,
numberbychapter=false,
keywordstyle=\bfseries\color{blue}
}
\begin{document}
\begin{lstlisting}[caption={This could be a very long description. In real life, my document is in two-column so I have even less space for the listing caption.}]
def foo():
bar()
\end{lstlisting}
\end{document}
这对于文章类。但是,我必须使用 IEEE Transactions 类 v1.8(例如这里:http://cruise.eecs.uottawa.ca/models2015/resources/IEEEtran.cls)但是当我用IEEEtran在上面的例子中,我得到了一个超出列宽的列表标题。
答案1
不同之处在于\@makecaption
类的末尾IEEEtran
没有显式\par
。TeX 在段落末尾评估段落参数。\par
后面是后 \@makecaption
选项设置xleftmargin
生效。
以下示例\par
在末尾添加了一个显式\@makecaption
:
\documentclass{IEEEtran}
\usepackage{etoolbox}
\makeatletter
\apptocmd\@makecaption{\par}{}{%
\errmessage{\noexpand\@makecaption could not be patched}%
}
\makeatother
\usepackage{color}
\usepackage[final]{listings}
\lstset{
language=Python,
numbers=left,
xleftmargin=2em,
frame=single,
captionpos=b,
basicstyle=\ttfamily\scriptsize,
breaklines=true,
tabsize=3,
numberbychapter=false,
keywordstyle=\bfseries\color{blue}
}
\begin{document}
\begin{lstlisting}[caption={This could be a very long description. In real
life, my document is in two-column so I have even less space for the listing
caption.}]
def foo():
bar()
\end{lstlisting}
\end{document}