264

I am writing a scientific paper with my collaborator. We have submitted a version to a journal and now we got a review from them, so we need to fix a few things. In Microsoft Word there is the option to "track changes", which displays the deleted text as crossed out and the new text bolded (if I remember the details correctly). Is there a way to achieve a similar result in LaTeX?

I was thinking to create a new file for the revised version and then write a script which would basically compare the two files and make the differences either bold or crossed out before sending it to latex. Is there a tool that already does it?

Grzenio
  • 9,465
  • 17
    Well, there are always solutions. The key question is: do you need a solution which operates on PDF level? Or would you be satisfied with a diff-based solution (i.e. comparison of the .tex source files)? You could simply create a mercurial or git repo and commit your files - and get the history on source level for free. A solution which highlights changes in the PDF is much more difficult. What are your requirements? – Christian Feuersänger Aug 01 '12 at 12:26
  • 2
    Mendeley (http://www.mendeley.com/features/read-and-annotate/) allows communal pdf markup. You'd still have to write the changes to the LaTeX source, though. – Ethan Bolker Aug 01 '12 at 12:30
  • 2
    The changebar latex package has \begin{changebar}...\end{changebar} as well as a cross environment version \cbstart...\cbend. Also the framed latex package has \begin{leftbar}...\end{leftbar}. – user1189687 Aug 01 '12 at 13:16
  • 2
  • 3
    latexdiff is one of a solution for your requirement... – MadyYuvi Oct 20 '18 at 07:36
  • Not perfect, but easiest: % deleted: foo \n % added: \n baz % end of changes – Fran Oct 14 '19 at 10:04

13 Answers13

190

You can process your TeX files with the Perl program latexdiff that is included with most LaTeX distributions.

From the manual:

Briefly, latexdiff is a utility program to aid in the management of revisions of latex documents. It compares two valid latex files, here called old.tex and new.tex, finds significant differences between them (i.e., ignoring the number of white spaces and position of line breaks), and adds special commands to highlight the differences.

StrongBad
  • 20,495
  • That looks really useful. Now I just need to script it into my document build process, which has the added complexity that most of my project's documentation is actually written in Markdown and translated to LaTeX by pandoc. So the script would have to retrieve a past revision, translate it to LaTeX, translate the present revision to LaTeX, then run latexdiff. While it will be fussy to do, it is probably easier than using a diff of the Markdown source text to drive markup of the generated LaTeX. – RBerteig Aug 02 '12 at 00:33
  • 6
    I found using latexdiff in combination with svn a killer solution. See also my earlier realted question (and answer) on this topic: http://tex.stackexchange.com/q/61405/2090 – Rabarberski Aug 04 '12 at 17:36
  • 7
    latexdiff is very complete and comes with many options: for instance, use the "--flatten" option if your document is divided into separate files (using \input or \include commands) – meduz Aug 08 '12 at 12:36
180

I have used the changes package in the past and I find it very useful. It has a key=value system so most of the things are customizable. You can define different authors and the changes are tracked depending on the id; here is a simple example (mostly from the manual).

\documentclass{article}

\usepackage{changes}
\usepackage{lipsum}% <- For dummy text
\definechangesauthor[name={Per cusse}, color=orange]{per}
\setremarkmarkup{(#2)}


\begin{document}
\lipsum[1-7]

This is \added[id=per,remark={we need this}]{new} text.
This is \added[id=per,remark={has to be in it}]{new} text.
This is \deleted[id=per,remark=obsolete]{unnecessary}text.
This is \replaced[id=per]{nice}{bad} text.

This is \added[remark={we need this}]{new} text.
This is \added[remark={has to be in it}]{new} text.
This is \deleted[remark=obsolete]{unnecessary}text.
This is \replaced{nice}{bad} text.


\listofchanges
\end{document}

enter image description here

And the nice thing is that if you supply the final option to the package declaration that is \usepackage[final]{changes} it clears the traces of changes made by the authors and respecting the last changes. See the replaced and deleted text for an example.

enter image description here

alfC
  • 14,350
percusse
  • 157,807
  • 4
    This is unrelated to downvotes. I just want to understand how the changes package works. The Changes package gives you a way to manually annotate an existing document with changed, deleted, etc. Each individual changed part of the document needs to be manually identified and then manually annotated (by further changing the document)? – Trevor Boyd Smith Aug 01 '12 at 18:55
  • 6
    @TrevorBoydSmith This is more suited to more than one authors, not necessarily heavy coders, such as me and my supervisor. For example, writing an article is a painful and kind of a unilateral process. My supervisor always wants to see what I did changed without reading the whole thing again etc. And he doesn't want to compile the whole thing etc. each time. So manually I plug in some changes and we discuss afterwards. I would really recommend the die-hard daily coders stay away from such stop and smell the flowers type of solution. But it's kind of indispensable for us. – percusse Aug 01 '12 at 20:10
  • 5
    @percusse, I can definitely see it happening where there is a student writing a paper with an adviser. The student has lots of time and the advisor has very little time. The student can spend lots of time marking his document to show the changes... and then maximize the productivity with the adviser. (but definitely it looks like it would take a lot of work) – Trevor Boyd Smith Aug 02 '12 at 12:38
  • 1
    @TrevorBoydSmith Not really actually! You embed these commands into macros so per comment, you type three letters and 2 brace pairs. I won't consider it as a lot. Besides the final version just cleans it which is perfect for me. – percusse Aug 02 '12 at 22:02
  • There seems to be an extra whitespace in line 3 of your example, i.e. the deleted text... If this is the case, it would undermine the usefulness of that option. – Jonathan Aug 25 '12 at 16:18
  • @Jonathan Yes, that's my mistake because, I use these in custom macros and I handle that extra space inside that macro so I don't really bother with this issue. But you are right for the deleted text you shouldn't introduce a blank space. – percusse Aug 25 '12 at 17:35
  • Thanks, percusse. I cannot comment. But when I COPY your lines, I still get the error message: Runaway argument. I really most of the time it says I missed a } or something. But it is really not the reason here. – Yizhen Hai Apr 16 '14 at 09:28
  • 1
    Changes package is not so good if your text contains macros and the like. – Nicholas Hamilton Jun 26 '16 at 21:52
  • @percusse, in my case list of changes are not generated? – LearnToGrow Nov 02 '16 at 16:41
  • But do you need to add all that annotations by hand or there is an automatic system that detects them? – skan Dec 01 '16 at 15:55
  • @skan this is for editing hence manual. Otherwise you need diff but that won't tell you why the changes are made. – percusse Dec 01 '16 at 15:57
  • Does this only work with the article document class? I've tried adding this to my thesis (including copy pasting the relevant commands) but it doesn't work: I get "! Package changes Error: Undefined changes author: gs." – Greg Slodkowicz May 14 '17 at 11:20
  • @GregSlodkowicz Can't say much with that description? Care to open a new question? – percusse May 14 '17 at 12:10
  • I don't think I can say anything more than that I copy-and-pasted your example and got the error message I quoted. Anyway I upgraded to MacTex 2016 (from 2015) and the error went away. – Greg Slodkowicz May 15 '17 at 12:51
  • @percusse How can we change the color of the font, as In my case the edited text appears in blue color only, but I want the text color to be in red font. – IgotiT Nov 22 '17 at 15:18
  • @IgotiT See the example and also the manual. I've changed it to orange – percusse Nov 22 '17 at 15:26
  • @percusse But the orange color is for particular id, and unwanted superscript "per" appears over the text. I need to change the default blue color only. – IgotiT Nov 22 '17 at 16:03
  • 4
    @percusse, this is awesome! But what does \setremarkmarkup{(#2)} do? I get on error on that line (Undefined control sequence), but works nicely if I just remove that line. – Egalth Nov 15 '18 at 13:39
  • @Egalth In the current version of the package, what used to be remark is called comment. – Arkya Feb 19 '22 at 16:05
58

Latex documents are plain text. Therefore you can handle tracking changes for Latex documents using the same robust methods that millions of software developers have been using for decades!

Short description of how software developers track changes in their source code:

  • keep the source in version control (git, svn, or whatever)
  • use differencing software to see what changes are made in between each revision (diff, vimdiff, Beyond Compare)

Here is a screenshot of what a diff program looks like (in this case this is a stock photo from Beyond Compare's website):

Beyond Compare Screenshot

(I actually don't use Beyond Compare. I prefer plain diff with vim which colorizes the diff.)

So whenever a change is made the change is saved in version control. And your version control keeps track of all changes. Allowing you to do things like: compare the two most recent versions, or compare the most recent with 5 versions back... or compare arbitrary version to arbitrary version. You have all the versions stored in version control!

  • As has been pointed out in the comments and answers above, using latexdiff is probably better than any of the other standard diffing packages -- because it is customized and tailored to LaTeX. – gt6989b Aug 01 '12 at 14:29
  • 4
    @gt6989b, Yep I already saw the answer that talks about latexdiff (and I upvoted it). I decided to answer because latexdiff only allows you to diff between two versions... but does not help you keep track of the different versions. My answer focuses on both using diff AND keeping track of different versions. – Trevor Boyd Smith Aug 01 '12 at 14:33
  • I did not argue with your answer (although someone suggested a source-control system in the comments), just wanted to point out that combining svn or git with latexdiff (instead of regular diff) would do better. – gt6989b Aug 01 '12 at 14:36
  • @gt6989b I am not sure I agree. To me latexdiff really shines when I am working with PDFs of compiled documents. I don't particularly like to look at the source code of the latexdiff processed files, I think that a plain old diff works better if you are not going to work with a compiled document. That said, the OP provided a workflow and asked if a tool existed. Version control and plain text diff, doesn't really fit into the proposed workflow. – StrongBad Aug 01 '12 at 14:45
  • 5
    The problem with plain diff on latex files are the newlines. There are two ways people store tex files. Either having a single paragraph as a single line, or using a fixed line width. With the first changing a single character in a paragraph leads to marking the whole paragraph as changed in the diff which is useless. In the second mode some editors (e.g., vim paragraph autoformat) would potentially mark multiple (sometimes all) lines in the paragraph as changed as well. Diff-ing tex documents is not a trivial issue. And that was only about plain text, no tables or equations. – walkmanyi Aug 01 '12 at 15:02
  • 2
    @walkmanyi Most diff programs have an option for ignoring white space. For GNU diff, you can use diff -w -B. – Aditya Aug 01 '12 at 15:15
  • 4
    @Aditya: simply ignoring whitespace is not sufficient, because it still respects newlines: rewrapped text would still show up as multiple changed lines even if only on one line a word had been added. Better to use wdiff or a similar tool here. – Jaap Eldering Aug 01 '12 at 19:02
  • FWIW, most of the comments after gt6989b's comment SEEM to me more of a discussion about 'What is the best diff tool to use when comparing two different versions of the same latex documents?'. I have created a chat room for this topic. Would it be possible if we can continue in the chatroom? (I would definitely like to hear about the various pros and cons of using different diff tools on latex source!) – Trevor Boyd Smith Aug 01 '12 at 19:41
  • 2
    This solution has the advantage that changes to latex comments and non-text things like tikz code show up nicely in the diff. It also allows you to diff between arbitrary revisions (e.g., what have I changed in the last month?) – Neil G Aug 01 '12 at 22:11
  • Three tools i use for this purpose are bzr qdiff, emacs ediff, and redmine's version comparison tool – David LeBauer Aug 03 '12 at 02:21
  • it would be great to have that functionality directly in your main latex tool, for example TeXStudio, without needing to use other external tools. – skan Dec 01 '16 at 15:56
12

For completeness sake, Adobe Acrobat Professional offers an excellent tool to compare PDFs. That tool has been heavily revised for v9 and now works extremely well for all sorts of text, including tables, font changes etc.

Some advantages over latexdiff:

  • Can track changes in the bibliography
  • Changed text is "overlayed", not crossed out or underlined, allowing for a cleaner document.
  • All changes are stored as comments that can be exported.

Downside:

Jörg
  • 7,653
6

You can simply create two commands for marking added and deleted text and display them as you wish. LyX features a change tracking mechanism, which is implemented in the source like this:

\PassOptionsToPackage{normalem}{ulem}
\usepackage{ulem}

\providecolor{added}{rgb}{0,0,1}
\providecolor{deleted}{rgb}{1,0,0}
%% Change tracking with ulem
\newcommand{\added}[1]{{\color{added}{}#1}}
\newcommand{\deleted}[1]{{\color{deleted}\sout{#1}}}

Whatever you mark with \added{...} will be displayed in blue and \deleted{...} in red and strikenthrough. If such a functionality would be just enough for you, in the final compilation of your document simply redefine \added to plain text passing and \deleted to be omitted. I.e., the following snippet should do:

\newcommand{\added}[1]{#1}
\newcommand{\deleted}[1]{}

Note, these are commands, so you cannot use that for too long text snippets. If your changes are more like blocks of text, take a look at the versions package at CTAN.

walkmanyi
  • 710
5

If you need to mark the changes in the \LaTeX source, the package http://trackchanges.sourceforge.net/TrackChanges is very helpful. It comes with a couple of python programs for globally accepting or rejecting changes as well.

cbowern
  • 149
  • 1
  • 4
3

I created revdiff to build diff versions of my papers before sending them to reviewers. They have told me several times that the result was really helpful. Hope it can be useful for you.

Find it here

jjmerelo
  • 210
2

For the sake of completeness, the accepted answer to an even older, similar question on Stack Overflow mentions the package todonotes, which offers "MS Word-style" commenting, and more, for those reviewers who are comfortable enough to edit the actual LaTeX code. Haven't used it myself, but it looks pretty solid. The documentation is updated as of July 2018, and provides nice examples.

Egalth
  • 129
2

Most answers focus on one part of the question which is how to compare two versions of a document. The other part being how to keep track of changes in the document.

As it has been underlined by other answers, .tex files are actually plain text files containing the latex markup language. It can be handled using classical software development tools.

So for comparison of versions you can use diff tools.

And for keeping track of multiple versions you can use a Revision control software. I would recommend git or mercurial. Those software typically embed diff tools to check differences between various revisions of the document. The main advantage over a simple copy paste of the .tex file is that you can keep track of the whole history of the document: what changes you made 2 weeks ago ?

It is also great for multiple author writing as revision control software can keep track of who made what change as well as merge changes of automagically !

Twonky
  • 750
1

Maybe this is not a direct answer to your question, but I still suggest you try my package debate. It helps you organize a discussion between reviewers inside your document:

\documentclass{article}
\usepackage{debate}
\begin{document}
I think apples are tastier
  \debate[jeff]{I don't think it's true!}
  \debate[anna]{It is indeed true!}
than pears!
\end{document}

When you are ready to make them all invisible, just use \usepackage[hide]{debate}

yegor256
  • 12,021
1

If you use pdflatex, there is an Acrobat (Professional) function to compare two PDFs. For a high-level overview on the major differences, it works surprisingly well IMHO.

1

You can also use the trackchanges package (link), which is very straightforward to use. The only thing to do is adding the trackchanges.sty file in the root of the LaTeX project and start adding your changes:

...
\usepackage[margins]{trackchanges}
...

\addeditor{sampleEditor}

My text \add[sampleEditor]{new text}. My text \remove[sampleEditor]{removed text}.

0

I think unfortunately there is no way to work with an advisor that knows latex but that likes MS Word track-changes option so much that you need to pass him directly the pure latex .tex file and he works with word on it...

linello
  • 403
  • 2
  • 12