> 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/da/dybtgaende-artikler/27-how-to-use-latexdiff-on-overleaf.md).

# Sådan bruger du latexdiff i Overleaf

Denne artikel viser to måder at bruge [latexdiff](https://ctan.org/pkg/latexdiff?lang=en) i Overleaf.

## Hvad er latexdiff?

`latexdiff` er et Perl-script, der er designet til at sammenligne indholdet af to LaTeX-filer. Det genererer LaTeX-kode, som visuelt viser eventuelle væsentlige forskelle mellem disse filer.

For at sammenligne `new.tex` med `old.tex` og sende resultaterne til en fil kaldet `diff.tex`, ville du bruge `latexdiff` som dette:

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

`OPTIONS` er kommandolinjeparametre, du kan bruge til at konfigurere `latexdiff`'s adfærd—se [`latexdiff` dokumentationen](http://mirrors.ctan.org/support/latexdiff/doc/latexdiff-man.pdf) for en liste over tilgængelige muligheder.

## Sådan bruger du latexdiff i Overleaf

Vi giver to løsninger, begge publiceret og diskuteret på tex.stackexchage. Den [første metode](#david-carlisles-solution) blev [foreslået af David Carisle](https://tex.stackexchange.com/a/603311), den [anden løsning](#tom-hejdas-solution) blev [foreslået af Tom Hejda](https://tex.stackexchange.com/a/603351), Overleafs supportmanager.

### David Carlisles løsning

Kildefilerne til at demonstrere denne løsning er angivet nedenfor sammen med links til at åbne dem som et Overleaf-projekt. Se [Hvordan denne første løsning fungerer](#how-this-first-solution-works) for en kort forklaring af denne kode.

[Åbn David Carlisles løsning i 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 Carlisles løsning}
\author{mig}
\date{maj 2024}
\begin{document}
\maketitle
\section{Introduktion}
zzz
\end{document}
```

**main2.tex**

```latex
\documentclass{article}
\title{latexdiff}
\author{mig\and dig}
\date{maj 2023}

\begin{document}

\maketitle

\section{Introduktion}
zzz zzz

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

**latexmkrc**

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

[Åbn David Carlisles løsning i 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)

Dette eksempel giver følgende output:

![Grafik, der viser outputtet genereret af latexdiff](/files/31d55ebdd9cbf72bb1a0eb7504caea84de0388db)

#### Hvordan denne første løsning fungerer

Denne løsning sammenligner filerne `main.tex` og `main2.tex` og skriver resultaterne til en fil kaldet `main-d.tex`. Bemærk, at fordi `main-d.tex` filen er en *genereret* fil, dvs. oprettet af vores kode, vil den ikke være synlig (listet) i projektets filer.

Den fungerer ved at bruge en fil kaldet `latexmkrc` som indeholder Perl-kode til at tilpasse Overleafs kompileringsproces. Her redefineres kommandoen til at kompilere med pdfLaTeX først til at køre `latexdiff` derefter udføre pdfLaTeX for at kompilere `main-d.tex`, filen genereret af `latexdiff`.

Som [bemærket af løsningens forfatter](https://tex.stackexchange.com/a/603311), for at stoppe med at køre `latexdiff` og sætte det faktiske indhold af `main.tex` eller `main2.tex`, skal du blot tilføje en `#`, Perl-kommentartegnet, til `latexmkrc` kommandolinjen, så det ser sådan ud:

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

Nu kan du kompilere `main.tex` eller `main2.tex` for at sætte deres faktiske indhold, ikke forskellene mellem dem.

For mere information henvises til Overleaf-artiklen [Sådan bruger du `latexmkrc` med Overleaf](/latex/da/dybtgaende-artikler/28-how-to-use-latexmkrc-with-overleaf.md).

### Tom Hejdas løsning

Kildefilerne til at demonstrere denne løsning er angivet nedenfor sammen med links til at åbne dem som et Overleaf-projekt. Se [Hvordan denne anden løsning fungerer](#how-this-second-solution-works) for en kort forklaring af denne kode.

[Åbn Tom Hejdas løsning i 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{Demonstration af latexdiff}
```

**main.tex**

```latex
\documentclass{article}
\title{Tom Hejdas løsning}
\author{mig}
\date{maj 2024}
\begin{document}
\maketitle
\section{Introduktion}
zzz
\end{document}
```

**main2.tex**

```latex
\documentclass{article}
\title{latexdiff}
\author{mig\and dig}
\date{maj 2023}

\begin{document}

\maketitle

\section{Introduktion}
zzz zzz

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

[Åbn Tom Hejdas løsning i 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)

Dette eksempel giver følgende output:

![Grafik, der viser outputtet genereret af latexdiff](/files/be99a0eb080bcdeeef982c51bef32f13ad5898be)

#### Hvordan denne anden løsning fungerer

Denne løsning sammenligner filerne `main.tex` og `main2.tex` og skriver resultaterne til en fil kaldet `main-d.tex`. Bemærk, at fordi `main-d.tex` filen er en *genereret* fil, dvs. oprettet af vores kode, vil den ikke være synlig (listet) i projektets filer.

En fil kaldet `diff.tex` bruger `\ShellEscape` kommandoen, leveret af [`shellesc` pakken](https://ctan.org/pkg/shellesc?lang=en), til at udføre `latexdiff`. Resultater af sammenligningen, indeholdt i `main-d.tex`, er `\input` i `diff.tex` til typesætning.

`diff.tex` indeholder linje `\documentclass{dummy}` for at narre Overleafs compiler til at tro `diff.tex` kan kompileres. Det virker, fordi Overleafs kompileringsproces tjekker den aktuelt åbne fil for tilstedeværelsen af `\documentclass`. Hvis den ikke finder den, kompilerer Overleaf filen angivet i Menuen som **Main** filen.

Hvis du vælger **Genkompilér** med `diff.tex` åbne i editoren, vil den sætte resultaterne af sammenligningen. Du kan vælge og kompilere `main.tex` eller `main2.tex` fordi de begge indeholder en `\documentclass` kommandoen.

For mere information om `latexdiff` se [den officielle dokumentation](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/da/dybtgaende-artikler/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.
