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.

This question already has an answer here:

I wonder is there any possibility to show two different listings in one table side by side? Here is my code that only show one listening but how can I embed it in a table and another one?

EDIT: Even I have tried the mentioned links on below, I get a few errors while compiling the file. I also post my edited LaTeX code.

    \documentclass[english,a4paper,12pt]{report}
\usepackage{mathpazo}
\usepackage{graphicx}
\renewcommand{\sfdefault}{lmss}
\usepackage{courier}
\usepackage[T1]{fontenc}
\usepackage[latin9]{inputenc}
\usepackage{listings}

\begin{document}
\begin{tabular}{|l|l|}

\begin{minipage}{.48\textwidth}
\begin{lstlisting}[
basicstyle={\scriptsize\ttfamily},
identifierstyle={\color{black}},
tabsize=2,
language={[AspectJ]Java},
numbersep=8pt,
numbers=left,
xleftmargin=0.5cm,frame=tlbr,framesep=2pt,framerule=0pt,
morekeywords ={class,run}
]
class PrintHelloThread ext Thread {
    void run(Dummy x) {
        out.println("Hello world")
    }
}

class X ext ProxifiedObject {
    PrintHelloThread thread

    void main(X x) {
        x.thread := new PrintHelloThread;
        x.thread.start(0);
        x.thread.join(0);
    }
}
\end{lstlisting}
\end{minipage}

&

\begin{minipage}{.48\textwidth}
\begin{lstlisting}[
basicstyle={\scriptsize\ttfamily},
identifierstyle={\color{black}},
tabsize=2,
language={[AspectJ]Java},
numbersep=8pt,
numbers=left,
xleftmargin=0.5cm,frame=tlbr,framesep=2pt,framerule=0pt,
morekeywords ={class,run}
]
class PrintHelloThread ext Thread {
    void run(Dummy x) {
        out.println("Hello world")
    }
}

class X ext ProxifiedObject {
    PrintHelloThread thread

    void main(X x) {
        x.thread := new PrintHelloThread;
        x.thread.start(0);
        x.thread.join(0);
    }
}
\end{lstlisting}
\end{minipage}
\\

\end{tabular}
\end{document}

What I would like to show is : enter image description here

share|improve this question

marked as duplicate by Masroor, Gonzalo Medina, Peter Jansson, Jubobs, Paul Stanley Mar 31 at 21:06

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1  
Just put them in two minipages or two tabular p-column entries, no special package or coding is needed. –  David Carlisle Mar 31 at 16:44
    

2 Answers 2

up vote 2 down vote accepted

As David said to do. In this case, I placed the result in a tabular. I also made the listings in \tiny, so as to make two side-by-side listings fit in the margin width.

In my case, I also placed them in boxes beforehand, so that my tabular code would look nice:

\begin{tabular}{|c|c|}
\hline
\stackinset{l}{-5pt}{t}{13\llength}{$\bullet$}{\listingA} &
\stackinset{l}{-5pt}{t}{ 7\llength}{$\bullet$}{\listingB} \\
\hline
\end{tabular}

EDITED to add little black circle to each listing. In this case, I superimposed them on the listing box with a \stackinset, defining the length of \llength to correspond to one line spacing in the tiny listing. Thus, a command like

\stackinset{l}{-5pt}{t}{13\llength}{$\bullet$}{\listingA}

indicated to place the bullet 13 lines down on the first listing.

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[latin9]{inputenc}
\usepackage{listings}
\usepackage{xcolor}
\usepackage{stackengine}
\newlength\llength
\llength=1.38ex\relax
\begin{document}

\clearpage

\setbox0=\hbox{%
\begin{minipage}{1.9in}
\begin{lstlisting}[
basicstyle={\tiny\ttfamily},
identifierstyle={\color{black}},
tabsize=2,
language={[AspectJ]Java},
numbersep=8pt,
numbers=left,
xleftmargin=0.5cm,frame=tlbr,framesep=2pt,framerule=0pt,
morekeywords ={class,run}
]
class PrintHelloThread ext Thread {
    void run(Dummy x) {
        out.println("Hello world")
    }
}

class X ext ProxifiedObject {
    PrintHelloThread thread

    void main(X x) {
        x.thread := new PrintHelloThread;
    }
}
\end{lstlisting}
\end{minipage}
}
\savestack{\listingA}{\box0}

\setbox0=\hbox{%
\begin{minipage}{1.9in}
\begin{lstlisting}[
basicstyle={\tiny\ttfamily},
identifierstyle={\color{black}},
tabsize=2,
language={[AspectJ]Java},
numbersep=8pt,
numbers=left,
xleftmargin=0.5cm,frame=tlbr,framesep=2pt,framerule=0pt,
morekeywords ={class,run}
]
class PrintHelloThread ext Thread {
    void run(Dummy x) {
        out.println("Hello world")
    }
}

class X ext ProxifiedObject {
    PrintHelloThread thread

    void main(X x) {
        x.thread := new PrintHelloThread;
    }
}
\end{lstlisting}
\end{minipage}
}
\savestack{\listingB}{\box0}

\begin{tabular}{|c|c|}
\hline
\stackinset{l}{-5pt}{t}{13\llength}{$\bullet$}{\listingA} &
\stackinset{l}{-5pt}{t}{ 7\llength}{$\bullet$}{\listingB} \\
\hline
\end{tabular}

\end{document}

enter image description here

share|improve this answer
    
Thank you for your help! One tiny question is that is it possible to draw a circle next to a code like this ? –  Hakan Mar 31 at 17:33
    
@Hakan Can you be more specific? What size circle, filled or unfilled? Color? Location? –  Steven B. Segletes Mar 31 at 17:34
    
Sure I can, a filled black circle whose size is very small. Did you notice my circle drawn in the related link? –  Hakan Mar 31 at 17:38
    
@Hakan I revised the code. Is that what you seek? If not quite, you can play with the 2nd and 4th arguments of \stackinset to move the dot around. –  Steven B. Segletes Mar 31 at 17:49
1  
@Hakan You need to download the stackengine package, from which the \stackinset macro comes. It is at ctan.org/tex-archive/macros/latex/contrib/stackengine. You can place the .sty file in your working directory, until you figure out how to install the package. –  Steven B. Segletes Mar 31 at 17:59

Your example generated errors because color was not loaded, but other than that it basically worked. You don't need the tabular just two `minipages are sufficient. Some of your lines are too ong to fit side by side, but that's a separate issue.

enter image description here

\documentclass[english,a4paper,12pt]{report}
\usepackage{mathpazo}
\usepackage{graphicx}
\renewcommand{\sfdefault}{lmss}
\usepackage{courier}
\usepackage[T1]{fontenc}
\usepackage[latin9]{inputenc}
\usepackage{listings}
\usepackage{color}%<<<<<<<<<<<<<<<

\begin{document}
%\begin{tabular}{|l|l|}

\begin{minipage}{.48\textwidth}
\begin{lstlisting}[
basicstyle={\scriptsize\ttfamily},
identifierstyle={\color{black}},
tabsize=2,
language={[AspectJ]Java},
numbersep=8pt,
numbers=left,
xleftmargin=0.5cm,frame=tlbr,framesep=2pt,framerule=0pt,
morekeywords ={class,run}
]
class PrintHelloThread ext Thread {
    void run(Dummy x) {
        out.println("Hello world")
    }
}

class X ext ProxifiedObject {
    PrintHelloThread thread

    void main(X x) {
        x.thread := new PrintHelloThread;
        x.thread.start(0);
        x.thread.join(0);
    }
}
\end{lstlisting}
\end{minipage}%%
\hfill
%&
%
\begin{minipage}{.48\textwidth}
\begin{lstlisting}[
basicstyle={\scriptsize\ttfamily},
identifierstyle={\color{black}},
tabsize=2,
language={[AspectJ]Java},
numbersep=8pt,
numbers=left,
xleftmargin=0.5cm,frame=tlbr,framesep=2pt,framerule=0pt,
morekeywords ={class,run}
]
class PrintHelloThread ext Thread {
    void run(Dummy x) {
        out.println("Hello world")
    }
}

class X ext ProxifiedObject {
    PrintHelloThread thread

    void main(X x) {
        x.thread := new PrintHelloThread;
        x.thread.start(0);
        x.thread.join(0);
    }
}
\end{lstlisting}
\end{minipage}
%\\

%\end{tabular}
\end{document}
share|improve this answer
    
Thank you! whereas I need a tabular property to distinguish two different codes in one big table. –  Hakan Mar 31 at 19:23
1  
Yes if you are using a table anyway you can put them in a tabular (but then you don't need the minipages you can put the listings envirinment straight in the tabular cells if you use p{...} instead of l in the column specifiers. –  David Carlisle Mar 31 at 19:39
    
But I also want to use bullets in my tabular, as we talked about it in the first post's comments with Steven. How can be possible to use multiple bullets in a listing then? –  Hakan Mar 31 at 20:10
    
@Hakan but that is an entirely separate issue which would be best asked as a separate question, Adding bullets to the listing is the same whether the listings are full page or 2 side by side. The question here was about putting two listings side by side and it's misleading to future readers if you then ask a different question in the comments which force the answers to load extra packages and more complicated code. The original question doesn't require any more code than putting two images or two paragraphs side by side. –  David Carlisle Mar 31 at 20:15
1  
I just changed ll in your linked example to \begin{tabular}{|p{.4\textwidth}|p{.4\textwidth}|} and it runs without error. If you have a new problem please ask a new question. not in comments –  David Carlisle Mar 31 at 21:27

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