> For the complete documentation index, see [llms.txt](https://overleaf-pro.ayaka.space/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://overleaf-pro.ayaka.space/latex/in-depth-articles/27-how-to-use-latexdiff-on-overleaf.md).

# How to use latexdiff on Overleaf

This article demonstrates two ways to use [latexdiff](https://ctan.org/pkg/latexdiff?lang=en) 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](http://mirrors.ctan.org/support/latexdiff/doc/latexdiff-man.pdf) 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](#david-carlisles-solution) was [suggested by David Carisle](https://tex.stackexchange.com/a/603311), the [second solution](#tom-hejdas-solution) was [proposed by Tom Hejda](https://tex.stackexchange.com/a/603351), Overleaf's Support Manager.

### David Carlisle’s 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](#how-this-first-solution-works) for as a short explanation of this code.

[Open David Carlisle's solution in Overleaf.](https://www.overleaf.com/docs?engine=pdflatex\&snip_name\[]=main.tex\&snip\[]=%5Cdocumentclass%7Barticle%7D%0A%5Ctitle%7BDavid+Carlisle%27s+solution%7D%0A%5Cauthor%7Bme%7D%0A%5Cdate%7BMay+2024%7D%0A%5Cbegin%7Bdocument%7D%0A%5Cmaketitle%0A%5Csection%7BIntroduction%7D%0Azzz%0A%5Cend%7Bdocument%7D\&snip_name\[]=main2.tex\&snip\[]=%5Cdocumentclass%7Barticle%7D%0A%5Ctitle%7Blatexdiff%7D%0A%5Cauthor%7Bme%5Cand+you%7D%0A%5Cdate%7BMay+2023%7D%0A%0A%5Cbegin%7Bdocument%7D%0A%0A%5Cmaketitle%0A%0A%5Csection%7BIntroduction%7D%0Azzz+zzz%0A%0A%5Csection%7BSection%7D%0Azzz%0A%5Cend%7Bdocument%7D\&snip_name\[]=latexmkrc\&snip\[]=%24pdflatex+%3D+%22latexdiff+main.tex+main2.tex+%3E+main-d.tex%3B+pdflatex+%25O++main-d%22\&main_document=main.tex)

**main.tex**

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

**main2.tex**

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

\begin{document}

\maketitle

\section{Introduction}
zzz zzz

\section{Section}
zzz
\end{document}
```

**latexmkrc**

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

[Open David Carlisle's solution in Overleaf.](https://www.overleaf.com/docs?engine=pdflatex\&snip_name\[]=main.tex\&snip\[]=%5Cdocumentclass%7Barticle%7D%0A%5Ctitle%7BDavid+Carlisle%27s+solution%7D%0A%5Cauthor%7Bme%7D%0A%5Cdate%7BMay+2024%7D%0A%5Cbegin%7Bdocument%7D%0A%5Cmaketitle%0A%5Csection%7BIntroduction%7D%0Azzz%0A%5Cend%7Bdocument%7D\&snip_name\[]=main2.tex\&snip\[]=%5Cdocumentclass%7Barticle%7D%0A%5Ctitle%7Blatexdiff%7D%0A%5Cauthor%7Bme%5Cand+you%7D%0A%5Cdate%7BMay+2023%7D%0A%0A%5Cbegin%7Bdocument%7D%0A%0A%5Cmaketitle%0A%0A%5Csection%7BIntroduction%7D%0Azzz+zzz%0A%0A%5Csection%7BSection%7D%0Azzz%0A%5Cend%7Bdocument%7D\&snip_name\[]=latexmkrc\&snip\[]=%24pdflatex+%3D+%22latexdiff+main.tex+main2.tex+%3E+main-d.tex%3B+pdflatex+%25O++main-d%22\&main_document=main.tex)

This example produces the following output:

![Graphic showing the output produced by latexdiff](/files/1MSLCnkgsSfQoNgn6M8B)

#### 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](https://tex.stackexchange.com/a/603311), 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](/latex/in-depth-articles/28-how-to-use-latexmkrc-with-overleaf.md).

### Tom Hejda’s 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](#how-this-second-solution-works) for as a short explanation of this code.

[Open Tom Hejda's solution in Overleaf.](https://www.overleaf.com/docs?engine=\&snip_name\[]=diff.tex\&snip\[]=%5CRequirePackage%7Bshellesc%7D%0A%5CShellEscape%7Blatexdiff+main.tex+main2.tex+%3E+main-d.tex%7D%0A%5Cinput%7Bmain-d%7D%0A%5Cdocumentclass%7Bdummy%7D%0A%5Ctitle%7BDemonstrating+latexdiff%7D\&snip_name\[]=main.tex\&snip\[]=%5Cdocumentclass%7Barticle%7D%0A%5Ctitle%7BTom+Hejda%27s+solution%7D%0A%5Cauthor%7Bme%7D%0A%5Cdate%7BMay+2024%7D%0A%5Cbegin%7Bdocument%7D%0A%5Cmaketitle%0A%5Csection%7BIntroduction%7D%0Azzz%0A%5Cend%7Bdocument%7D\&snip_name\[]=main2.tex\&snip\[]=%5Cdocumentclass%7Barticle%7D%0A%5Ctitle%7Blatexdiff%7D%0A%5Cauthor%7Bme%5Cand+you%7D%0A%5Cdate%7BMay+2023%7D%0A%0A%5Cbegin%7Bdocument%7D%0A%0A%5Cmaketitle%0A%0A%5Csection%7BIntroduction%7D%0Azzz+zzz%0A%0A%5Csection%7BSection%7D%0Azzz%0A%5Cend%7Bdocument%7D\&main_document=diff.tex)

**diff.tex**

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

**main.tex**

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

**main2.tex**

```latex
\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.](https://www.overleaf.com/docs?engine=\&snip_name\[]=diff.tex\&snip\[]=%5CRequirePackage%7Bshellesc%7D%0A%5CShellEscape%7Blatexdiff+main.tex+main2.tex+%3E+main-d.tex%7D%0A%5Cinput%7Bmain-d%7D%0A%5Cdocumentclass%7Bdummy%7D%0A%5Ctitle%7BDemonstrating+latexdiff%7D\&snip_name\[]=main.tex\&snip\[]=%5Cdocumentclass%7Barticle%7D%0A%5Ctitle%7BTom+Hejda%27s+solution%7D%0A%5Cauthor%7Bme%7D%0A%5Cdate%7BMay+2024%7D%0A%5Cbegin%7Bdocument%7D%0A%5Cmaketitle%0A%5Csection%7BIntroduction%7D%0Azzz%0A%5Cend%7Bdocument%7D\&snip_name\[]=main2.tex\&snip\[]=%5Cdocumentclass%7Barticle%7D%0A%5Ctitle%7Blatexdiff%7D%0A%5Cauthor%7Bme%5Cand+you%7D%0A%5Cdate%7BMay+2023%7D%0A%0A%5Cbegin%7Bdocument%7D%0A%0A%5Cmaketitle%0A%0A%5Csection%7BIntroduction%7D%0Azzz+zzz%0A%0A%5Csection%7BSection%7D%0Azzz%0A%5Cend%7Bdocument%7D\&main_document=diff.tex)

This example produces the following output:

![Graphic showing the output produced by latexdiff](/files/WZIn8OQJ9OlAYm7neZrx)

#### 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](https://ctan.org/pkg/shellesc?lang=en), 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](http://mirrors.ctan.org/support/latexdiff/doc/latexdiff-man.pdf).


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://overleaf-pro.ayaka.space/latex/in-depth-articles/27-how-to-use-latexdiff-on-overleaf.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
