Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?)
share|improve this question

1 Answer 1

up vote 4 down vote accepted
  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

share|improve this answer
    
Fantastic, @GonzaloMedina - that works great! I didn't realize it would have been this complicated, though :) Many thanks again - cheers! –  sdaau Mar 1 at 2:40

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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