2

I'm trying to replicate the following layout that I have in a Word version of the paper:

This is what I want to get in LaTeX.

There are two code sections side by side with the code balanced between the two. The code won't fit into a single column, so I used this way to get it to stay on the same page. The caption would be under.

I tried doing this, but unfortunately, that kind of works, but not quite. It produces the two columns, but there are issues:

LaTeX version of what I got.

So first thing is the columns do not align with the text columns (red lines). I think it probably has to do with my minipage somehow.

Secondly, it overlaps with text that follows (that biography box should be later on!). Makes me feel like it does not recognize that the minipage is there and renders the text anyways.

Here is a code snippet that does this (I use same syntax in my file):

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

\newcommand{\us}{\char`_}
\usepackage{listings}
\lstdefinestyle{base}{
    language=C,
    basicstyle=\footnotesize\ttfamily,
    multicols=2,
    breaklines=true
}
\usepackage{multicol}
\usepackage{xcolor}
\usepackage{blindtext}
\begin{document}


\noindent
\begin{minipage}[5cm]{\textwidth}
\begin{lstlisting}[language=C++,caption=Migration,style=base,captionpos=b]
int a;
int a;
int a;
int a;
int a;
int a;
int a;
int a;
int a;
int a;
int a;
int a;
int a;
int a;
int a;
int a;
int a;
int a;
int a;
int a;
int a;
int a;
int a;
int a;
int a;



\end{lstlisting}
\end{minipage}%


\blindtext
\blindtext
\blindtext
\blindtext
\blindtext
\blindtext

\end{document}

I hope someone can hint which way I should proceed here.

  • 1
    Can you get us some compilable code? This is a mess up there ;-) – Johannes_B Mar 2 '15 at 22:52
  • 1
    Put \noindent right before the minipage. – Johannes_B Mar 2 '15 at 22:52
  • @Johannes_B, thanks! The \noindent fixed the indent... I added a snippet that compiles and does the same thing (a MWE?). – Mewa Mar 2 '15 at 23:01
  • 1
    @Mewa get rid of the minipage and use the starred figure* to enclose your listing. It will be moved to the top of the following page but you won't have the problems that you mention. – Gonzalo Medina Mar 2 '15 at 23:44
  • 1
    @Mewa did you use the starred version as in \begin{figure*} \begin{lstlisting}[language=C++,caption=Migration,style=base,captionpos=b] int a; int a; int a; int a; int a; int a; int a; int a; int a; int a; int a; int a; int a; int a; int a; int a; int a; int a; int a; int a; int a; int a; int a; int a; int a; \end{lstlisting} \end{figure*}? Notice that it is figure* with an asterisk. – Gonzalo Medina Mar 2 '15 at 23:48
4

Get rid of the minipage and enclose the listing inside a figure*; it will be moved to the top of the following page but you won't have the problems that you are experiencing now:

\documentclass[10pt,journal,compsoc]{IEEEtran}
\usepackage{listings}
\lstdefinestyle{base}{
    language=C,
    basicstyle=\footnotesize\ttfamily,
    multicols=2,
    breaklines=true
}
\usepackage{multicol}
\usepackage{xcolor}
\usepackage{blindtext}

\newcommand{\us}{\char`_}

\begin{document}

\begin{figure*}
\begin{lstlisting}[language=C++,caption=Migration,style=base,captionpos=b]
int a;
int a;
int a;
int a;
int a;
int a;
int a;
int a;
int a;
int a;
int a;
int a;
int a;
int a;
int a;
int a;
int a;
int a;
int a;
int a;
int a;
int a;
int a;
int a;
int a;
\end{lstlisting}
\end{figure*}
\blindtext
\blindtext
\blindtext
\blindtext
\blindtext
\blindtext
\blindtext
\blindtext
\blindtext
\blindtext
\blindtext
\blindtext
\blindtext
\blindtext
\blindtext
\blindtext
\blindtext
\blindtext

\end{document}

enter image description here

  • \chardef\us`\_ instead of \newcommand\us..? (In case this suggestion is correct, I don't know what's de benefit of this, just that it seems to be the appropiate tool.) – Manuel Mar 3 '15 at 0:08
  • @Manuel that's part of the original code. I don't know its purpose and it was not my suggestion. – Gonzalo Medina Mar 3 '15 at 0:10
  • True, sorry, I thought it was yours :) – Manuel Mar 3 '15 at 0:16
  • @Manuel, that was the result of some quick Googlin' in an attempt to spend less time typing in \char`_ for all of the underscores. I'll check your method out tomorrow morning :) – Mewa Mar 3 '15 at 5:36
1

Perhaps something like this. This uses figure* to produce a double-column float. To centre the caption, caption is used so that the figure* caption can be assigned to the lstlisting captions rather than regular figures.

multicolumn listing with justified caption

listing in list of listings

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

\newcommand{\us}{\char`_}
\usepackage{listings}
\lstdefinestyle{base}{
  language=C,
  basicstyle=\footnotesize\ttfamily,
  multicols=2,
  breaklines=true,
}
\usepackage{multicol}
\usepackage{caption}
\captionsetup{justification=justified, singlelinecheck=false}
\usepackage{xcolor}
\usepackage{kantlipsum}
\begin{document}
  \lstlistoflistings
  \begin{figure*}
    \begin{lstlisting}[language=C++,style=base]
      int a;
      int a;
      int a;
      int a;
      int a;
      int a;
      int a;
      int a;
      int a;
      int a;
      int a;
      int a;
      int a;
      int a;
      int a;
      int a;
      int a;
      int a;
      int a;
      int a;
      int a;
      int a;
      int a;
      int a;
      int a;
    \end{lstlisting}
    \captionsetup{type=lstlisting}
    \caption{Migration}
  \end{figure*}

  \kant[1-20]

\end{document}

EDIT

Edited in light of Gonzalo Medina's comment concerning the format of captions for this class. justification=justified justifies captions and singlelinecheck=false prevents those containing only one line from being centred anyway.

  • The caption package and IEEEtran are not fully compatible. Why using \captionsetup{type=lstlisting} \caption{Migration} and not the caption key for lstlisting (as in my answer and the OP's original code) which doesn't require the use of the caption package? – Gonzalo Medina Mar 3 '15 at 0:41
  • @GonzaloMedina I thought that the OP wanted the caption under the two-column listing of code? – cfr Mar 3 '15 at 0:43
  • Ah, I see. This might indeed be the case and my answer doesn't cover it. I'll delete my comments shortly. – Gonzalo Medina Mar 3 '15 at 0:52
  • @GonzaloMedina Whether that's what the OP meant or not, I'm not now sure. (That's just how I read the question.) In any case, is there a better way to do this? I've not really any experience with listings. However, I did look to see if there was a way to do this with the caption key which the OP had used and couldn't find anything which looked promising in the manual. [Although I posted my answer 30 minutes after yours, I didn't actually see yours. I had to read the docs for listings to get as far as I did.] – cfr Mar 3 '15 at 0:56
  • Not really. One could try to redefine some internals using something like \end{lstlisting} \makeatletter \newcommand\ext@lstlisting{lol} \renewcommand\@captype{lstlisting} \caption{Migration} \makeatother \end{figure*} but this is just a provisional version (the counter will be missing in the caption). In any case, notice that the caption package changes the caption alignment (IEEEtran won't center captions even if single-lined while caption enforces this). – Gonzalo Medina Mar 3 '15 at 1:12

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.