Is there a way to highlight the syntax of a diff file (i.e. by using the listings
package)?
3 Answers
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)
-
Indeed, diff syntax is pretty simple! Thanks for having replied even with an accepted answer…– GaretJaxApr 1, 2012 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.– alexisApr 2, 2012 at 12:00 -
(Update: I found out about
lstlinebgrd
, which can set the background color).– alexisDec 19, 2012 at 15:55
\begin{minted}{diff}
<diff data goes here>
\end{minted}
would work, or
\inputminted{diff}{path/to/file.diff}
if you want to just read the diff from a file. Minted also provides a listing command you can use to wrap it in a floating box like a figure or table.
-
2
-
-
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?– GaretJaxMar 31, 2012 at 10:19
-
(And I couldn't resist having a go at the lowest-answer-length-to-upvotes-ratio contest:P.)– mborkMar 31, 2012 at 10:19
-
3
I extended the answer from alexis to cover more formats: normal format (diff
), unified format (diff -u
), context format (diff -c
) and diffs from Git (git diff
):
\lstdefinelanguage{diff}{
sensitive=true,
% diff command line
morecomment=[f][\color{gray}][0]{diff},
% commit identifiers for git diff
morecomment=[f][\color{gray}][0]{index},
% hunk location/line numbers for unified format
morecomment=[f][\color{blue}][0]{@@},
% hunk location/line numbers for context format
morecomment=[f][\color{magenta}][0]{***},
% changed line for context format
morecomment=[f][\color{violet}][0]{!},
% deleted lines for unified format
morecomment=[f][\color{red!60!black}][0]-,
% added lines for unified format
morecomment=[f][\color{green!60!black}][0]+,
% file name and time stamp old file
morecomment=[f][\color{magenta}][0]{---},
% file name and time stamp new file
morecomment=[f][\color{magenta}][0]{+++},
% Binary files ... differ
morecomment=[f][\color{gray}][0]{Binary},
% Only in ...: file.txt
morecomment=[f][\color{gray}][0]{Only},
% old mode ...
morecomment=[f][\color{gray}][0]{old},
% new mode ...
morecomment=[f][\color{gray}][0]{new},
% rename from/to ...
morecomment=[f][\color{gray}][0]{rename},
% similarity index ...%
morecomment=[f][\color{gray}][0]{similarity},
% deleted file mode ...%
morecomment=[f][\color{gray}][0]{deleted},
% hunk separator for context format
morecomment=[f][\color{magenta}][0]{***************},
% deleted lines for normal format
morecomment=[f][\color{red!60!black}][0]<,
% added lines for normal format
morecomment=[f][\color{green!60!black}][0]>,
% line number specifier for normal format
morecomment=[f][\color{blue}][0]{0},
% line number specifier for normal format
morecomment=[f][\color{blue}][0]{1},
% line number specifier for normal format
morecomment=[f][\color{blue}][0]{2},
% line number specifier for normal format
morecomment=[f][\color{blue}][0]{3},
% line number specifier for normal format
morecomment=[f][\color{blue}][0]{4},
% line number specifier for normal format
morecomment=[f][\color{blue}][0]{5},
% line number specifier for normal format
morecomment=[f][\color{blue}][0]{6},
% line number specifier for normal format
morecomment=[f][\color{blue}][0]{7},
% line number specifier for normal format
morecomment=[f][\color{blue}][0]{8},
% line number specifier for normal format
morecomment=[f][\color{blue}][0]{9},
}[comments]