4

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

Hakan
  • 546
  • 1
    Just put them in two minipages or two tabular p-column entries, no special package or coding is needed. – David Carlisle Mar 31 '14 at 16:44
  • related: http://tex.stackexchange.com/questions/19098/displaying-two-xml-listings-side-by-side and http://tex.stackexchange.com/questions/118437/including-html-code-side-by-side-in-latex – jub0bs Mar 31 '14 at 16:48

2 Answers2

4

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

  • 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 '14 at 17:33
  • @Hakan Can you be more specific? What size circle, filled or unfilled? Color? Location? – Steven B. Segletes Mar 31 '14 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 '14 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 '14 at 17:49
  • Sorry for bothering you again, I am getting an error on line 7 :" \newlength\llength\newlength\lwidth " this says that "File `stackengine.sty' not found." How can I get rid of this? – Hakan Mar 31 '14 at 17:56
  • 1
    @Hakan You need to download the stackengine package, from which the \stackinset macro comes. It is at http://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 '14 at 17:59
2

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}
David Carlisle
  • 757,742
  • Thank you! whereas I need a tabular property to distinguish two different codes in one big table. – Hakan Mar 31 '14 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 '14 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 '14 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 '14 at 20:15
  • Allright you are totally right , I will arrange another question for that. Thanks again! – Hakan Mar 31 '14 at 20:16
  • Could you please explain why I am getting the error saying 'Missing number, treated as zero \begin{lstlisting}' in this link – Hakan Mar 31 '14 at 20:44
  • @Hakan a p column is essentially a minipage it needs a width argument. p{.45\textwidth} or p{3cm}` or something. – David Carlisle Mar 31 '14 at 21:01
  • I am trying to eliminate minipages in my code, I have put my codes into cells and deleted all the minipages. I still get this error. – Hakan Mar 31 '14 at 21:05
  • @Hakan the link still shows \begin{tabular}{|p|p|} which as I say above is wrong – David Carlisle Mar 31 '14 at 21:08
  • according to the edited link I now have this error :'Missing \endgroup inserted morekeywords={class,run}]' – Hakan Mar 31 '14 at 21:11
  • 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 '14 at 21:27