# Multicolumn listing for comparison in LaTeX

I have some C code that I want to insert in a LaTeX document using a listing. I also want to display its translation to assembly code. I don't know how to display those two kind of codes in a multicolumn view, so to give the reader the possibility to make a quick comparison between C code and assembly code. I don't need to have the lines of C aligned with their correspondent assembly instructions since I think this is not going to be easy to do. What I need is just to show the two codes side by side. Can you help me?

Thanks

-

The code can be displayed using the listings package. The side by side effect I would do using two minipages as shown below. In general you have to be careful because the listings are verbatim material and don't work inside the arguments of other macros. (Using saveboxes helps here)

\documentclass{article}

\usepackage[T1]{fontenc}
\usepackage{lmodern}

\usepackage{listings}
\usepackage{lipsum}

\lstset{basicstyle=\ttfamily\scriptsize}
\begin{document}

\lipsum[1]

\par\noindent
\begin{minipage}[t]{.45\textwidth}
\begin{lstlisting}[language=C,frame=lrtb]
int function test(int a) {
return a;
}
\end{lstlisting}%
\end{minipage}%
\hfill
\begin{minipage}[t]{.45\textwidth}
\begin{lstlisting}[frame=lrtb]
TEST:
goto SOMEWHERE
\end{lstlisting}%
\end{minipage}%

\lipsum[2]

\end{document}

-

You may try parcolumns package.

See also other question: Of parallel texts, lists and display equations.

### Example

\documentclass[12pt]{article}
\usepackage[a4paper,margin=1cm]{geometry}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage[scaled=0.82]{beramono}
\usepackage{pifont,calc,xcolor}
\usepackage{parcolumns}
\usepackage{listings}
\definecolor{verylightgray}{gray}{0.85}
\newcommand*\lstnumberstyle{\ttfamily\scriptsize}
\newlength\lstnumbersep
\setlength\lstnumbersep{10pt}
\newlength\lstnumberwidth
\lstset{
,basicstyle=\ttfamily%
,breaklines=true%
,tabsize=4%
,showstringspaces=false%
,numbers=left%
,numbersep=\lstnumbersep%
,numberstyle=\lstnumberstyle%
,framesep=0pt%
,xleftmargin=\lstnumberwidth%
,backgroundcolor=\color{verylightgray}%
,postbreak=\ding{229}\space%
}
%% far from complete list of gasm-specific keywords and instructions in AT&T syntax
\lstdefinelanguage[x86gasm]{Assembler}[x86masm]{Assembler}{%
,morekeywords={rax,rbx,rcx,rdx,rip,rdi,rsi,rsp,subq,decl,movq,movl,xorl,imull,popq}%
,morekeywords=[2]{.file,.section,.string,.text,.globl,.cfi_startproc,.cfi_def_cfa_offset,.cfi_endproc,.size,.ident}%
}
\lstnewenvironment{ccol}[1][]{\lstset{language=c,#1}}{}
\lstnewenvironment{acol}[1][]{\lstset{language=[x86gasm]Assembler,#1}}{}
\pagestyle{empty}
\parindent=0pt
\begin{document}
\texttt{gcc} on \texttt{text.c} file (left side) [\texttt{gcc -S -Os text.c}] gives following assembler code (right side):
\begin{parcolumns}{2}
\colchunk[1]{\begin{ccol}
#include <stdio.h>

int main(int argc, char *argv[])
{
int t;

if (argc < 2) {
fprintf(stderr, "Not enough arguments!\n");
return 1;
}

t = atoi(argv[1]);
printf("%d^2 = %d\n", t, t * t);

return 0;
}
\end{ccol}}
\colchunk[2]{\begin{acol}
.file   "test.c"
.section    .rodata.str1.1,"aMS",@progbits,1
.LC0:
.string "Not enough arguments!\n"
.LC1:
.string "%d^2 = %d\n"
.text
.globl main
.type   main, @function
main:
.LFB0:
.cfi_startproc
subq    $8, %rsp .cfi_def_cfa_offset 16 decl %edi jg .L2 movq stderr(%rip), %rsi movl$.LC0, %edi
call    fputs
movl    $1, %eax jmp .L3 .L2: movq 8(%rsi), %rdi xorl %eax, %eax call atoi movl %eax, %edx movl %eax, %esi movl$.LC1, %edi
imull   %eax, %edx
xorl    %eax, %eax
call    printf
xorl    %eax, %eax
.L3:
popq    %rdx
ret
.cfi_endproc
.LFE0:
.size   main, .-main
.ident  "GCC: (Debian 4.4.5-8) 4.4.5"
.section    .note.GNU-stack,"",@progbits
\end{acol}}
\end{parcolumns}
\end{document}

-
The code you provided is very complete, thanks. – Raffo Jul 14 '11 at 10:42