How to use latexdiff on Overleaf
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 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 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:
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 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 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:
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
- Creating a document in Overleaf
- Uploading a project
- Copying a project
- Creating a project from a template
- Using the Overleaf project menu
- Including images in Overleaf
- Exporting your work from Overleaf
- Working offline in Overleaf
- Using Track Changes in Overleaf
- Using bibliographies in Overleaf
- Sharing your work with others
- Using the History feature
- Debugging Compilation timeout errors
- How-to guides
- Guide to Overleaf’s premium features
LaTeX Basics
- Creating your first LaTeX document
- Choosing a LaTeX Compiler
- Paragraphs and new lines
- Bold, italics and underlining
- Lists
- Errors
Mathematics
- Mathematical expressions
- Subscripts and superscripts
- Brackets and Parentheses
- Matrices
- Fractions and Binomials
- Aligning equations
- Operators
- Spacing in math mode
- Integrals, sums and limits
- Display style in math mode
- List of Greek letters and math symbols
- Mathematical fonts
- Using the Symbol Palette in Overleaf
Figures and tables
- Inserting Images
- Tables
- Positioning Images and Tables
- Lists of Tables and Figures
- Drawing Diagrams Directly in LaTeX
- TikZ package
References and Citations
- Bibliography management with bibtex
- Bibliography management with natbib
- Bibliography management with biblatex
- Bibtex bibliography styles
- Natbib bibliography styles
- Natbib citation styles
- Biblatex bibliography styles
- Biblatex citation styles
Languages
- Multilingual typesetting on Overleaf using polyglossia and fontspec
- Multilingual typesetting on Overleaf using babel and fontspec
- International language support
- Quotations and quotation marks
- Arabic
- Chinese
- French
- German
- Greek
- Italian
- Japanese
- Korean
- Portuguese
- Russian
- Spanish
Document structure
- Sections and chapters
- Table of contents
- Cross referencing sections, equations and floats
- Indices
- Glossaries
- Nomenclatures
- Management in a large project
- Multi-file LaTeX projects
- Hyperlinks
Formatting
- Lengths in LaTeX
- Headers and footers
- Page numbering
- Paragraph formatting
- Line breaks and blank spaces
- Text alignment
- Page size and margins
- Single sided and double sided documents
- Multiple columns
- Counters
- Code listing
- Code Highlighting with minted
- Using colours in LaTeX
- Footnotes
- Margin notes
Fonts
Presentations
Commands
Field specific
- Theorems and proofs
- Chemistry formulae
- Feynman diagrams
- Molecular orbital diagrams
- Chess notation
- Knitting patterns
- CircuiTikz package
- Pgfplots package
- Typesetting exams in LaTeX
- Knitr
- Attribute Value Matrices
Class files
- Understanding packages and class files
- List of packages and class files
- Writing your own package
- Writing your own class