Zum Inhalt springen

This article demonstrates two ways to use latexdiff on Overleaf.

What is latexdiff?

latexdiff is a Perl script designed to compare the content of two LaTeX files. It generates LaTeX code that visually displays any significant differences between those files.

To compare new.tex with old.tex and output the results to a file called diff.tex, you would use latexdiff like this:

latexdiff OPTIONS old.tex new.tex > diff.tex

OPTIONS are command-line parameters you can use to configure latexdiff's behavior—refer to the latexdiff documentation for a list of available options.

How to use latexdiff on Overleaf

We provide two solutions, both published and discussed on tex.stackexchage. The first method was suggested by David Carisle, the second solution was proposed by Tom Hejda, Overleaf's Support Manager.

David Carlisles solution

Source files to demonstrate this solution are listed below, together with links to open them as an Overleaf project. See How this first solution works for as a short explanation of this code.

 Open David Carlisle's solution in Overleaf.

main.tex

\documentclass{article}
\title{David Carlisle's solution}
\author{me}
\date{May 2024}
\begin{document}
\maketitle
\section{Introduction}
zzz
\end{document}

main2.tex

\documentclass{article}
\title{latexdiff}
\author{me\and you}
\date{May 2023}

\begin{document}

\maketitle

\section{Introduction}
zzz zzz

\section{Section}
zzz
\end{document}

latexmkrc

$pdflatex = "latexdiff main.tex main2.tex > main-d.tex; pdflatex %O  main-d"

 Open David Carlisle's solution in Overleaf.

This example produces the following output:

Graphic showing the output produced by latexdiff

How this first solution works

This solution compares the files main.tex and main2.tex and writes the results to a file called main-d.tex. Note that because main-d.tex file is a generated file, i.e., created by our code, it won’t be visible (listed) within the project’s files.

It works by making use of a file called latexmkrc which contains Perl code to customize Overleaf's compilation process. Here, the command to compile using pdfLaTeX is redefined to first run latexdiff then execute pdfLaTeX to compile main-d.tex, the file generated by latexdiff.

As noted by the solution's author, to stop running latexdiff and typeset the actual content of main.tex or main2.tex, just add a #, the Perl comment marker, to the latexmkrc command line, making it looks like this:

# $pdflatex = "latexdiff main.tex main2.tex > main-d.tex; pdflatex %O  main-d"

Now you can compile main.tex or main2.tex to typeset their actual content, not the differences between them.

For more information, refer to the Overleaf article How to use latexmkrc with Overleaf.

Tom Hejdas solution

Source files to demonstrate this solution are listed below, together with links to open them as an Overleaf project. See How this second solution works for as a short explanation of this code.

 Open Tom Hejda's solution in Overleaf.

diff.tex

\RequirePackage{shellesc}
\ShellEscape{latexdiff main.tex main2.tex > main-d.tex}
\input{main-d}
\documentclass{dummy}
\title{Demonstrating latexdiff}

main.tex

\documentclass{article}
\title{Tom Hejda's solution}
\author{me}
\date{May 2024}
\begin{document}
\maketitle
\section{Introduction}
zzz
\end{document}

main2.tex

\documentclass{article}
\title{latexdiff}
\author{me\and you}
\date{May 2023}

\begin{document}

\maketitle

\section{Introduction}
zzz zzz

\section{Section}
zzz
\end{document}

 Open Tom Hejda's solution in Overleaf.

This example produces the following output:

Graphic showing the output produced by latexdiff

How this second solution works

This solution compares the files main.tex and main2.tex and writes the results to a file called main-d.tex. Note that because main-d.tex file is a generated file, i.e., created by our code, it won’t be visible (listed) within the project’s files.

A file called diff.tex uses the \ShellEscape command, provided by the shellesc package, to execute latexdiff. Results of the comparison, contained in main-d.tex, are \input into diff.tex for typesetting.

diff.tex contains line \documentclass{dummy} to trick Overleaf's compiler into believing diff.tex can be compiled. That works because Overleaf's compile process checks the currently opened file for the presence of \documentclass. If it doesn't find it, Overleaf compiles the file listed in the Menu as the Main file.

If you select Recompile with diff.tex open in the editor, it will typeset the results of the comparison. You can select and compile main.tex or main2.tex because they both contain a \documentclass command.

For more information on latexdiff check out the official documentation.

Overleaf guides

LaTeX Basics

Mathematics

Figures and tables

References and Citations

Languages

Document structure

Formatting

Fonts

Presentations

Commands

Field specific

Class files

Advanced TeX/LaTeX