[Tex/LaTex] Final linebreak in lstlisting, and linebreak in captions (with IEEEtran)

captionsieeetranlistings

Using:

… I got to the following MWE:

% \documentclass[12pt,a4paper]{article}
\documentclass[12pt,a4paper,onecolumn]{IEEEtran}

\usepackage{listings}

\begin{document}

\noindent\begin{minipage}{.45\textwidth}
\begin{lstlisting}[basicstyle=\ttfamily,caption={[short]This is first line \\\hspace{\textwidth} This is second line},frame=tlrb]

function Something() {
  doOneThing() ;
  doAnotherThing() ;
  doThirdThing() ;
  if (something(wrong)) {
    repeat();
  }
}

\end{lstlisting}
\end{minipage}\hfill
\begin{minipage}{.45\textwidth}
\begin{lstlisting}[basicstyle=\scriptsize\ttfamily,caption=code 2,frame=tlrb]{Name}

function SomethingElse() {
  doAnotherThing() ;
  doOneThing() ;
  doThirdThing() ;
  if (something(wrong)) {
    repeat();
  }
}

\end{lstlisting}
\end{minipage}

\end{document}

… which gets me this:

test.pdf

  • How do I get the multiline caption for Listing 1 to align properly above the listing box (note, this is a problem only with IEEEtran, not with regular article class)?
  • How do I get a blank line (linebreak) added at the end of the listing (just like the single line whitespace at start of listing is preserved?)

Best Answer

  1. Using the etoolbox package we locally redefine \@makecaption inside lstlisting to correct the vertical position and spacing for the caption.

  2. Use showlines=true to print empty lines at the end of listings.

The code:

\documentclass[12pt,a4paper,onecolumn]{IEEEtran}
\usepackage{listings}
\usepackage{etoolbox}
\lstset{showlines=true}

\makeatletter
\AtBeginEnvironment{lstlisting}{%
\long\def\@makecaption#1#2{%
% test if is a for a figure or table
\ifx\@captype\@IEEEtablestring%
% if a table, do table caption
\footnotesize\bgroup\par\centering\@IEEEtabletopskipstrut{\normalfont\footnotesize #1}\\{\normalfont\footnotesize\scshape #2}\par\addvspace{0.5\baselineskip}\egroup%
\@IEEEtablecaptionsepspace
% if not a table, format it as a figure
\else
\@IEEEfigurecaptionsepspace
% 3/2001 use footnotesize, not small; use two nonbreaking spaces, not one
\setbox\@tempboxa\hbox{\normalfont\footnotesize {#1.}\nobreakspace\nobreakspace #2}%
\ifdim \wd\@tempboxa >\hsize%
% if caption is longer than a line, let it wrap around
\setbox\@tempboxa\hbox{\normalfont\footnotesize {#1.}\nobreakspace\nobreakspace}%
\parbox[b]{\hsize}{\normalfont\footnotesize\noindent\unhbox\@tempboxa#2}\medskip%
% if caption is shorter than a line, center if conference, left justify otherwise
\else%
\ifCLASSOPTIONconference \hbox to\hsize{\normalfont\footnotesize\hfil\box\@tempboxa\hfil}%
\else \hbox to\hsize{\normalfont\footnotesize\box\@tempboxa\hfil}\medskip%
\fi\fi\fi}}
\makeatother
\begin{document}

\noindent\begin{minipage}{.45\textwidth}
\begin{lstlisting}[basicstyle=\ttfamily,caption={[short]This is first line and some other text to span several lines},frame=tlrb]

function Something() {
  doOneThing() ;
  doAnotherThing() ;
  doThirdThing() ;
  if (something(wrong)) {
    repeat();
  }
}

\end{lstlisting}
\end{minipage}\hfill
\begin{minipage}{.45\textwidth}
\begin{lstlisting}[basicstyle=\scriptsize\ttfamily,caption=code 2,frame=tlrb]{Name}

function SomethingElse() {
  doAnotherThing() ;
  doOneThing() ;
  doThirdThing() ;
  if (something(wrong)) {
    repeat();
  }
}


\end{lstlisting}
\end{minipage}

\end{document}

enter image description here

Related Question