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.

I'd like to compare two versions of program with vertical line between them. How can I do this with LaTeX?

enter image description here

share|improve this question

2 Answers 2

up vote 6 down vote accepted

One way would be to use two minipages and the listings package:

enter image description here

While you are at it, you can also add a bit of syntax highlighting, and import an external file which has the source code you want to show:

enter image description here

Notes:


Code: Basic

\documentclass{article}
\usepackage{listings}

\begin{document}
\begin{minipage}[t]{0.45\linewidth}
\begin{lstlisting}[language=c++]
// before refactoring
class A {
  public int f;
  void m (int i) {
     f = i + f;
  }
}
\end{lstlisting}
\end{minipage}%
\hfill\vrule\hfill
\begin{minipage}[t]{0.45\linewidth}
\begin{lstlisting}[language=c++]
// after refactoring
class A {
  private int f;
  void m(int i) {
     setF(i * getF()();
  }

  public int getF() {
    return this.F;
  }

  public void setF(int f) {
    this.f = f;
  }
}
\end{lstlisting}
\end{minipage}%
\end{document} 

Code: Fancy

\documentclass{article}
\usepackage{xcolor}
\usepackage{listings}

\usepackage{filecontents}
\begin{filecontents*}{before.c}
// before refactoring
class A {
  public int f;
  void m (int i) {
     f = i + f;
  }
}
\end{filecontents*}
\begin{filecontents*}{after.c}
// after refactoring
class A {
  private int f;
  void m(int i) {
     setF(i * getF()();
  }

  public int getF() {
    return this.F;
  }

  public void setF(int f) {
    this.f = f;
  }
}
\end{filecontents*}

\lstdefinestyle{MyStyle}{ %
    language=C++,
    backgroundcolor=\color{gray!10},
    basicstyle=\small,
    basicstyle=\small\ttfamily,
    keywordstyle=\color{red}
}

\begin{document}
\begin{minipage}[t]{0.45\linewidth}
    \lstinputlisting[style=MyStyle]{before.c}
\end{minipage}%
\hfill\vrule\hfill
\begin{minipage}[t]{0.45\linewidth}
    \lstinputlisting[style=MyStyle]{after.c}
\end{minipage}
\end{document} 
share|improve this answer

use a simple tabular environment:

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{xcolor,listings,beramono}
\lstset{basicstyle=\ttfamily\small,
        keywordstyle=\color{blue}}
\begin{document}

\begin{tabular}{@{}p{\dimexpr0.4\linewidth-\tabcolsep}% can be up to 0.5\linewidth 
                 | p{\dimexpr0.5\linewidth-\tabcolsep}@{}}
\begin{lstlisting}[language=c++]
// before refactoring
class A {
  public int f;
  void m (int i) {
     f = i + f;
  }
}
\end{lstlisting}
&
\begin{lstlisting}[language=c++]
// after refactoring
class A {
  private int f;
  void m(int i) {
     setF(i * getF()();
  }

  public int getF() {
    return this.F;
  }

  public void setF(int f) {
    this.f = f;
  }
}
\end{lstlisting}

\end{tabular}

\end{document} 

enter image description here

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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