6

I have the following setup for the listings package, including a left margin to have the line numbers stay within the column.

\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}

This works fine for the article class. However, I have to work with the IEEE Transactions class v1.8 (for example here: http://cruise.eecs.uottawa.ca/models2015/resources/IEEEtran.cls) but as soon as I replace the document class article with IEEEtran in the above example, I get a listing caption that exceeds the column width.

enter image description here

1
  • did you check the caption package?
    – musicman
    Jun 24, 2015 at 14:50

1 Answer 1

4
+50

The difference is that \@makecaption of class IEEEtran does not have an explicit \par at the end. TeX evaluates the paragraph parameters at the end of the paragraph. The \par comes later after \@makecaption with the setting of option xleftmargin in force.

The following example adds an explicit \par at the end of \@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}

Result

1
  • Works perfect, thanks. Will award the bounty as soon as this is possible
    – Georg
    Jun 25, 2015 at 7:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.