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:
- 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 regulararticle
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
Using the etoolbox package we locally redefine
\@makecaption
insidelstlisting
to correct the vertical position and spacing for the caption.Use
showlines=true
to print empty lines at the end of listings.The code: