19

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

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

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

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

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}
  • 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
  • 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
14

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.

12

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

2

I took a combination of this answer and this answer to get a listings environment closer to the git diff visualizations, with coloured line backgrounds rather than coloured text.

output

\documentclass{article}

\usepackage{xcolor}

\usepackage{listings}

\newcommand{\lstbg}[3][0pt]{{\fboxsep#1\colorbox{#2}{\strut #3}}}
\lstdefinelanguage{diff}{
  basicstyle=\ttfamily\small,
  morecomment=[f][\lstbg{red!20}]-,
  morecomment=[f][\lstbg{green!20}]+,
  morecomment=[f][\textit]{@@},
  %morecomment=[f][\textit]{---},
  %morecomment=[f][\textit]{+++},
}

\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}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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