Graphics, Figures & Tablestwo listings side by side to each other

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
krislodz
Posts: 42
Joined: Sun Nov 08, 2009 1:13 pm

two listings side by side to each other

Post by krislodz »

Hello,

I would like to do something like a code snippet on the left hand side and corresponding data on the right hand side. I use minipage currently but it looks not so nice, minipages are not centered.

Did anyone have some suggestions how can I improve the outcome ?

Here is latex file:

Code: Select all

\documentclass[a4paper,11pt]{article}
\usepackage{hyperref}
\hypersetup{linktocpage}
\usepackage{listings}
\usepackage{amsmath}
\usepackage{array}
\usepackage{tikz}
\usepackage{rotating}
\usepackage{graphicx}
\usepackage{booktabs} % for much better looking tables
\usetikzlibrary{arrows}

\begin{document}
%\begin{table}[ht]
\begin{minipage}[b]{0.5\linewidth}
\centering
\lstset{language=C,label=SliceExaple}
%\lstinputlisting{example_source.c}
\begin{lstlisting}[frame=single, numbers=left, mathescape, label=scopingExample, linewidth = 4cm]
x:=1;
if(x>0)
{
 x:=5;
 while(x>0)
 {
  x:=x-1;
 }
 x:=10;
}
else
{
 write x;
}
x:=15;
\end{lstlisting}
\caption*{Example WHILE code snippet with nested scopes.}
\end{minipage}
\hspace{0.5cm}
\begin{minipage}[b]{0.5\linewidth}
\centering
\begin{lstlisting}[frame=single, mathescape, label=scopesEnteredContents, linewidth = 4cm]
[0]
[0]

[1;0]
[1;0]

[2;1;0]

[1;0]



[3;0]

[0]
\end{lstlisting}
\caption{Contents of scopesEntered for each block.}   
\end{minipage}
%\caption{Example of scope assigning using scopesEntered list.}
%\end{table}
\end{document}

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

two listings side by side to each other

Post by gmedina »

Hi,

the code you posted won't compile due to the use of \caption outside floating environments. Perhaps something like the following is what you are looking for:

Code: Select all

\documentclass[a4paper,11pt]{article}
\usepackage{listings}

\begin{document}

\begin{minipage}[b]{0.45\linewidth}
\centering
\lstset{language=C,label=SliceExaple}
\begin{lstlisting}[frame=single, numbers=left, mathescape,%
  caption={Example WHILE code snippet with nested scopes.}, label=scopingExample]
x:=1;
if(x>0)
{
x:=5;
while(x>0)
{
  x:=x-1;
}
x:=10;
}
else
{
write x;
}
x:=15;
\end{lstlisting}
\end{minipage}
\hspace{0.5cm}
\begin{minipage}[b]{0.45\linewidth}
\centering
\begin{lstlisting}[frame=single, mathescape,%
  title={Contents of scopesEntered for each block.}, label=scopesEnteredContents]
[0]
[0]

[1;0]
[1;0]

[2;1;0]

[1;0]



[3;0]

[0]
\end{lstlisting}
\end{minipage}

\end{document}
1,1,2,3,5,8,13,21,34,55,89,144,233,...
User avatar
Juanjo
Posts: 657
Joined: Sat Jan 27, 2007 12:46 am

Re: two listings side by side to each other

Post by Juanjo »

You can use the xleftmargin and xrightmargin keys in the optional argument of lstlisting to move listings to the left or the right.
The CTAN lion is an artwork by Duane Bibby. Courtesy of www.ctan.org.
krislodz
Posts: 42
Joined: Sun Nov 08, 2009 1:13 pm

Re: two listings side by side to each other

Post by krislodz »

Thanks gmedina, this is what I was aiming for. Juanjo, I was looking also for justification of text within listing but cannot find it anywhere. Can you help with this also ?
User avatar
Juanjo
Posts: 657
Joined: Sat Jan 27, 2007 12:46 am

two listings side by side to each other

Post by Juanjo »

krislodz wrote: I was looking also for justification of text within listing but cannot find it anywhere. Can you help with this also ?
I'm not sure about what you understand by justification in this context. Anyway, since your original problem was that the boxes were not centered, I suggested to use two specific keys. To see their effect, in the code of gmedina, replace

Code: Select all

\begin{lstlisting}[frame=single, numbers=left, mathescape,%
  caption={Example WHILE code snippet with nested scopes.}, label=scopingExample]
by

Code: Select all

\begin{lstlisting}[frame=single, numbers=left, mathescape,%
  caption={Example WHILE code snippet with nested scopes.}, label=scopingExample,%
  xleftmargin=1cm,xrightmargin=1cm]
and do analogously with the other lstlisting environment. I think it looks better.
The CTAN lion is an artwork by Duane Bibby. Courtesy of www.ctan.org.
krislodz
Posts: 42
Joined: Sun Nov 08, 2009 1:13 pm

Re: two listings side by side to each other

Post by krislodz »

Nice, thanks a lot for help.
Post Reply