3

I'm trying to use \captionof{lstlisting}{Pseudo Code} to obtain a listing with its normal caption "Listing 1. Pseudo Code", in an IEEEtran document.

I want caption of instead of the caption= because I want to add comments next to the code, and the caption to span above both code and comments.

Here's a MCVR:

\documentclass[10pt,journal,compsoc]{IEEEtran}

\usepackage{listings}
\usepackage{capt-of}

\makeatletter
% the 3 caption packages caption.sty, ltcaption.sty, newfloat.sty contain:
\providecommand*\ext@lstlisting{lol}%
\makeatother

\begin{document}

\begin{figure}
    \centering%
    \captionof{lstlisting}{pseudo code with comments}
    \begin{minipage}[t]{0.40\textwidth}
        \begin{lstlisting}[label=code]
code here
        \end{lstlisting}
    \end{minipage}%
    \hspace{1em}%
    \begin{minipage}[t]{0.45\textwidth}
        Some comments here
    \end{minipage}
\end{figure}

Reference Listing~\ref{code} in text.

\end{document}

This gets me nearly what I want, except the number does not appear: I get "Listing. Pseudo Code", however the references work fine.

I get an undefined control sequence from ext@lstlisting which I had to define manually for the file to compile.

The whole ordeal works fine if I load caption instead of capt-of however IEEEtrans guidelines warn:

Axel Sommerfeldt's caption.sty [will] override IEEEtran.cls' handling of captions and this will result in non-IEEE style figure/table captions.

And indeed, using this package changes the Table and Figure captions' style, so I need to not use this package (which makes this question different from this similar one that uses caption).

How can I work around this and get the listing numbers with \captionof{lstlisting}{}?

1
  • 1
    Have you defined a float of type lstlisting? If so, what should be its label? Should it be "Listing"? (By default, the label of figure floats is "Figure", and that of table floats is "Table".) – Mico May 4 '17 at 16:40
3

The command \captionof from capt-of is not as sophisticated as the \captionof command from caption package.

It uses \caption internally, which needs the \@captype to be defined being lstlisting but also \fnum@lstlisting, which is not properly defined then.

A redefinition of \fnum@lstlisting as (say) Listing~\thelstlisting cures the problem.

\documentclass[10pt,journal,compsoc]{IEEEtran}


\usepackage{capt-of}
\usepackage{listings}

\makeatletter

% the 3 caption packages caption.sty, ltcaption.sty, newfloat.sty contain:
 \AtBeginDocument{%
  \providecommand*\ext@lstlisting{lol}%
  \renewcommand{\fnum@lstlisting}{\lstlistingname\nobreakspace\thelstlisting}
 }
\makeatother

\begin{document}
\begin{figure}
  \centering%
   \captionof{lstlisting}{pseudo code with comments}
   \begin{minipage}[t]{0.40\textwidth}
    \begin{lstlisting}[label=code]
code here
\end{lstlisting}
\end{minipage}%
\hspace{1em}%
\begin{minipage}[t]{0.45\textwidth}
  Some comments here
\end{minipage}
\end{figure}

Reference Listing~\ref{code} in text.

\end{document}

enter image description here

0

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.