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.

Is there a way to highlight the syntax of a diff file (i.e. by using the listings package)?

share|improve this question

2 Answers 2

up vote 20 down vote accepted

You can just do this with the listings package, and you won't even need an external pre-processor. listings doesn't come with a diff style but you can define one like this:

\lstdefinelanguage{diff}{
  morecomment=[f][\color{blue}]{@@},     % group identifier
  morecomment=[f][\color{red}]-,         % deleted lines 
  morecomment=[f][\color{green}]+,       % added lines
  morecomment=[f][\color{magenta}]{---}, % Diff header lines (must appear after +,-)
  morecomment=[f][\color{magenta}]{+++},
}

Use it in the usual ways. E.g., \lstinputlisting[language=diff]{myfile.diff}.

Update: Diff viewers often highlight the background of lines, instead of coloring the text. This can be added with the help of Martin Scharrer's lstlinebgrd package, available on CTAN in his lstaddons bundle (or get the latest version from the package homepage)

share|improve this answer
    
Indeed, diff syntax is pretty simple! Thanks for having replied even with an accepted answer… –  GaretJax Apr 1 '12 at 12:12
    
Welcome. If you figure out how to set the background color for any of these, please demonstrate. I'm not sure listings can set the background of anything smaller than a whole listing. –  alexis Apr 2 '12 at 12:00
    
(Update: I found out about lstlinebgrd, which can set the background color). –  alexis Dec 19 '12 at 15:55

minted?

share|improve this answer
1  
Indeed, pygmentize -L lexers shows that diff is supported. –  egreg Mar 31 '12 at 10:17
    
@egreg: I know, I checked it on the project's web page first;). –  mbork Mar 31 '12 at 10:18
    
Great! I don't have the time to change all my other listings to minted though (I use lstlistings). Is there a way to use lstlistings settings for minted? –  GaretJax Mar 31 '12 at 10:19
    
(And I couldn't resist having a go at the lowest-answer-length-to-upvotes-ratio contest:P.) –  mbork Mar 31 '12 at 10:19
2  
@mbork The comment was intended as a spur to improve the answer. ;-) –  egreg Mar 31 '12 at 10:19

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.