Exclude caption from listing margin in IEEEtran v1.8a

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.

• did you check the caption package? Jun 24, 2015 at 14: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}


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