# Is there a ready solution to typeset a diff file?

In a text about Git I want to typeset diffs similar to this visualization: Commit details on GitHub.

Is there any ready-made solution to that, especially the coloring of the diff lines? (In other commits there may be even an additional highlighting of the actual changed part of a line with a different background color.

Of course I could 'roll my own' but don't want to reinvent the wheel.

(Searching for LaTeX and diff only seems to mathematical symbols or how to diff LaTeX documents ...)

-
You could use minted, potentially with a custom style for Pygments? – You Mar 30 '13 at 10:24

## 4 Answers

You can also use listings. You must define an own language with own colour settings. The lines with special characters are defined as comment lines here:

\documentclass{article}

\usepackage[svgnames]{xcolor}
\definecolor{diffstart}{named}{Grey}
\definecolor{diffincl}{named}{Green}
\definecolor{diffrem}{named}{OrangeRed}

\usepackage{listings}
\lstdefinelanguage{diff}{
basicstyle=\ttfamily\small,
morecomment=[f][\color{diffstart}]{@@},
morecomment=[f][\color{diffincl}]{+\ },
morecomment=[f][\color{diffrem}]{-\ },
}

\begin{document}

\begin{lstlisting}[language=diff]
@@ -85,8 +85,8 @@
\newcommand{\doctitleifsub}[2]{%
\thispagestyle{empty}
\begin{center}
-    \sffamily This document is part of the documentation of \openlilylib%
-    \footnote{\url{https://github.com/openlilylib/openLilyLib}},\\
+    \sffamily This document is part of \textbf{\openlilylib}%
+    \footnote{\url{http://www.openlilylib.org}},\\
a collection of resources for the LilyPond notation software%
\footnote{\url{http://www.lilypond.org}}\\
and the \LaTeX{} typesetting system.
\end{lstlisting}

\end{document}


-
Thanks for this. I like the idea of taking the plus and minus as 'comment indicators'. – uli_1973 Apr 4 '13 at 22:15
Some apps create patch files that don't have a space after the +/- signs, if you copy & paste the above solution but it doesn't work, try removing the "\ " (slash-space) from the end of the second and third lines that start with "morecomment=" – daniel Feb 7 '14 at 2:18

Using the verbatim package you can easily apply formatting to each line, so this just extracts the first character and uses it as part of a colour name to use for the highlighting.

\documentclass{article}
\usepackage{verbatim,color}
\makeatletter

\long\def\diffcolor#1#2\@nil{color\string#1diff}

\def\verbatim@processline{%
\nointerlineskip\noindent\rlap{%
\colorbox{\expandafter\diffcolor\next..\@nil}{%
\the\verbatim@line}}\par}
\makeatother

\definecolor{color diff}{rgb}{1,1,1}
\definecolor{color-diff}{rgb}{1,.5,.5}
\definecolor{color+diff}{rgb}{.5,1,.5}

\begin{document}

\verbatiminput{diffex.diff}

\end{document}

-
+1 for your skills. – Marco Daniel Mar 30 '13 at 18:05
@MarcoDaniel shouldn't that +1 have been in green:-) – David Carlisle Mar 30 '13 at 18:11
Changed my accepted answer because the listings approach doesn't affect all environments but can be applied only when applicable. – uli_1973 Apr 4 '13 at 11:57
@uli_1973 I'll survive the loss of 15 :-) You could of course just locally define \verbatim@processline for the environments that need it, but there is no harm (and some sense) in using an accepted package interface if it supports this. – David Carlisle Apr 4 '13 at 12:09
Is it possible to adapt the solution to minted package? (I really like minted style, but I got no clue how to highlight part of the code like the diff sections) – Oxdeadbeef Jan 21 '15 at 1:52

The vim module for ConTeXt uses the VIM editor to do the syntax highlighting of files. As such it supports all syntax highlighting for all filetypes supported by VIM (which is a lot). For example, to highlight diff files, you may use:

\usemodule[vim]

\definevimtyping[DIFFtyping][syntax=diff]

\starttext
\startDIFFtyping
@@ -85,8 +85,8 @@
\newcommand{\doctitleifsub}[2]{%
\thispagestyle{empty}
\begin{center}
-    \sffamily This document is part of the documentation of \openlilylib%
-    \footnote{\url{https://github.com/openlilylib/openLilyLib}},\\
+    \sffamily This document is part of \textbf{\openlilylib}%
+    \footnote{\url{http://www.openlilylib.org}},\\
a collection of resources for the LilyPond notation software%
\footnote{\url{http://www.lilypond.org}}\\
and the \LaTeX{} typesetting system.
\stopDIFFtyping
\stoptext


which gives

Full disclosure: I am author of the vim module.

-

You can use verbments, provided you have Pygmentize on your machine:

\documentclass{article}
\usepackage{verbments}
\begin{document}
\begin{pyglist}[language=diff]
@@ -85,8 +85,8 @@
\newcommand{\doctitleifsub}[2]{%
\thispagestyle{empty}
\begin{center}
-    \sffamily This document is part of the documentation of \openlilylib%
-    \footnote{\url{https://github.com/openlilylib/openLilyLib}},\\
+    \sffamily This document is part of \textbf{\openlilylib}%
+    \footnote{\url{http://www.openlilylib.org}},\\
a collection of resources for the LilyPond notation software%
\footnote{\url{http://www.lilypond.org}}\\
and the \LaTeX{} typesetting system.
\end{pyglist}
\end{document}


Running LaTeX with -shell-escape enabled is required.

-
How is verbments different from minted? – Aditya Mar 30 '13 at 18:42
@Aditya See minted vs texments vs verbments – egreg Mar 30 '13 at 18:44