4

I am trying to display to codes side-by-side, using this:

\begin{center} \begin{tabular}{c|c}  \begin{lstlisting}
while (x > 0) do
  x++; \end{lstlisting} & \begin{lstlisting}
x = 0; \end{lstlisting}  \end{tabular}\end{center}

Here is what is displayed after compilation: latex code lstlisting

The problem is, the vertical line of the tabular goes too far. I have tried to play with the lstlisting parameters belowskip and aboveskip, and tried to use minipages too, but nothing seems to work properly.

My second problem is that using the code above, the second code ("x = 0;") is not aligned with the while. EDIT : the second issue is solved using minipages, thanks to cfr

Do you know how to solve this ?

3
  • 1
    Use minipages or \parboxes with [t] alignment for the second problem.
    – cfr
    Jul 30, 2015 at 1:51
  • You might be interested in this answer: tex.stackexchange.com/questions/210284/… Jul 30, 2015 at 6:32
  • Thanks Henri, I have a working solution using minipages and vrule. But I hoped I could use a tabular and no vrule to do this (this would be cleaner for me)
    – waffle
    Jul 30, 2015 at 10:16

1 Answer 1

2

enter image description here

We got the above image with the assistance of array package, you can change the width of columns as you like

% \usepackage{array}  


\begin{center} 
\begin{tabular}{p{3.9cm}|p{3.9cm}} 
 \begin{lstlisting}
while (x > 0) do
  x++; \end{lstlisting} &
   \begin{lstlisting}
x = 0; \end{lstlisting}  
\end{tabular}
\end{center} 
2
  • Thank you for your answer but this is not what I was searching for. I am not trying to change the width of columns, but the height of the vertical line.
    – waffle
    Jul 30, 2015 at 11:28
  • Try to reduce \arraystretch , example \renewcommand{\arraystretch}{0.2}.
    – Salim Bou
    Jul 30, 2015 at 11:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .