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.

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

enter image description here

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 ...)

share|improve this question
    
You could use minted, potentially with a custom style for Pygments? –  You Mar 30 '13 at 10:24
add comment

4 Answers

up vote 12 down vote accepted

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}

example output

share|improve this answer
    
Thanks for this. I like the idea of taking the plus and minus as 'comment indicators'. –  uli_1973 Apr 4 '13 at 22:15
1  
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 at 2:18
add comment

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.

enter image description here

\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}
share|improve this answer
2  
+1 for your skills. –  Marco Daniel Mar 30 '13 at 18:05
3  
@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
add comment

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

enter image description here

Full disclosure: I am author of the vim module.

share|improve this answer
add comment

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.

enter image description here

share|improve this answer
    
How is verbments different from minted? –  Aditya Mar 30 '13 at 18:42
2  
@Aditya See minted vs texments vs verbments –  egreg Mar 30 '13 at 18:44
add comment

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.