Search code examples
latexlistings

LaTeX - two columns within the same listing?


I'm trying to achieve the following but with no problems in spacing.

The image is what I'm trying to achieve but without the spacing problems :

enter image description here

At the moment it's just a normal listing with tabbing.

I want to avoid tabbing by introducing two columns. Is that possible?

Current code:

\begin{lstlisting}[caption=Elements of time in the background knowledge, label=btime]

year(Y):-                       hour(H):-
   Y in 2000..2011.                 H in 0..23.

month(M):-                  minute(M):-
   M in 1..12.                      M in 0..59.

day_of_month(D):-               seconds(S):-
    D in 1..31.                     minute(S).

date([D, M, Y]):-                   time([H,M]):-
    year(Y),                            hour(H),
    month(M),                       minute(M).
    day_of_month(D).                            

\end{lstlisting}

Solution

  • Something like this (result looks like THIS)?

    \begin{tabular}{p{7cm}p{7cm}}
     year(Y):-                           &  hour(H):-      \\
      \hspace{10mm}Y in 2000..2011.      &   \hspace{10mm} H in 0..23.    \\
    & \\
     month(M):-                          &  minute(M):-    \\
      \hspace{10mm} M in 1..12.          &   \hspace{10mm} M in 0..59.    \\
    & \\
     day\_of\_month(D):-                 &  seconds(S):-   \\
      \hspace{10mm} D in 1..31.          &   \hspace{10mm} minute(S).     \\
    & \\
     date([D, M, Y]):-                   &   \hspace{10mm} time([H,M]):-  \\
          \hspace{10mm} year(Y),         &   \hspace{15mm} hour(H),       \\
      \hspace{10mm} month(M),            &   \hspace{10mm} minute(M).     \\
      \hspace{10mm} day\_of\_month(D).   &                 \\
    \end{tabular}
    
    • Tweak the values p{Xcm} to get the column distance you want
    • Tweak the values \hspace{Xmm} to get the indentation you want
      • An alternative would be to use \quad or \qquad, though that might not be enough for you