> For the complete documentation index, see [llms.txt](https://ayakaleaf-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://ayakaleaf-pro.ayaka.space/latex/zh-cn/shen-du-wen-zhang/27-how-to-use-latexdiff-on-overleaf.md).

# 如何在 Overleaf 上使用 latexdiff

本文演示了两种使用 [latexdiff](https://ctan.org/pkg/latexdiff?lang=en) 在 Overleaf 上。

## 什么是 latexdiff？

`latexdiff` 是一个 Perl 脚本，用于比较两个 LaTeX 文件的内容。它会生成 LaTeX 代码，以可视化方式显示这些文件之间的任何显著差异。

要比较 `new.tex` 与 `old.tex` 并将结果输出到名为 `diff.tex`的文件，你会使用 `latexdiff` 如下：

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

`OPTIONS` 是你可以用来配置 `latexdiff`的行为——请参阅 [`latexdiff` 文档](http://mirrors.ctan.org/support/latexdiff/doc/latexdiff-man.pdf) 以查看可用选项列表。

## 如何在 Overleaf 上使用 latexdiff

我们提供两种解决方案，二者都已在 tex.stackexchage 上发布并讨论。 [第一种方法](#david-carlisles-solution) 由 [David Carisle 提出](https://tex.stackexchange.com/a/603311)， [第二种解决方案](#tom-hejdas-solution) 由 [由 Tom Hejda 提出](https://tex.stackexchange.com/a/603351)，他是 Overleaf 的支持经理。

### David Carlisle 的解决方案

用于演示此解决方案的源文件列在下方，并附有将它们作为 Overleaf 项目打开的链接。有关 [此第一个解决方案的工作原理](#how-this-first-solution-works) 以了解对这段代码的简短说明。

[在 Overleaf 中打开 David Carlisle 的解决方案。](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"
```

[在 Overleaf 中打开 David Carlisle 的解决方案。](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)

此示例生成如下输出：

![显示 latexdiff 生成的输出的图形](/files/9df9a08f1c99c062b0001ee1f488c820f12f7f0a)

#### 此第一个解决方案的工作原理

此解决方案比较文件 `main.tex` 和 `main2.tex` 并将结果写入名为 `main-d.tex`。请注意，由于 `main-d.tex` 文件是一个 *生成的* 文件，也就是由我们的代码创建的文件，它不会在项目文件中可见（列出）。

它通过使用一个名为 `latexmkrc` 的文件来工作，该文件包含用于自定义 Overleaf 编译流程的 Perl 代码。在这里，使用 pdfLaTeX 编译的命令被重新定义为先运行 `latexdiff` 然后执行 pdfLaTeX 来编译 `main-d.tex`，即由 `latexdiff`.

正如 [该解决方案作者指出的](https://tex.stackexchange.com/a/603311)，要停止运行 `latexdiff` 并排版 `main.tex` 或 `main2.tex`的实际内容，只需在 `#`中添加一个 Perl 注释标记 `latexmkrc` 到命令行中，效果如下：

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

现在你可以编译 `main.tex` 或 `main2.tex` 以排版它们的实际内容，而不是它们之间的差异。

有关更多信息，请参阅 Overleaf 文章 [如何使用 `latexmkrc` 与 Overleaf 配合使用](/latex/zh-cn/shen-du-wen-zhang/28-how-to-use-latexmkrc-with-overleaf.md).

### Tom Hejda 的解决方案

用于演示此解决方案的源文件列在下方，并附有将它们作为 Overleaf 项目打开的链接。有关 [第二种解决方案的工作原理](#how-this-second-solution-works) 以了解对这段代码的简短说明。

[在 Overleaf 中打开 Tom Hejda 的解决方案。](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}
```

[在 Overleaf 中打开 Tom Hejda 的解决方案。](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)

此示例生成如下输出：

![显示 latexdiff 生成的输出的图形](/files/ccfe38b372847d0e854cbc4d5a88197e7fa31d47)

#### 第二种解决方案的工作原理

此解决方案比较文件 `main.tex` 和 `main2.tex` 并将结果写入名为 `main-d.tex`。请注意，由于 `main-d.tex` 文件是一个 *生成的* 文件，也就是由我们的代码创建的文件，它不会在项目文件中可见（列出）。

一个名为 `diff.tex` 使用 `\ShellEscape` 命令，由 [`shellesc` 宏包](https://ctan.org/pkg/shellesc?lang=en)提供，用于执行 `latexdiff`。比较结果包含在 `main-d.tex`中，并被 `\input` 输入到 `diff.tex` 中进行排版。

`diff.tex` 包含第 `\documentclass{dummy}` 行，以欺骗 Overleaf 的编译器相信 `diff.tex` 可以被编译。这是因为 Overleaf 的编译流程会检查当前打开的文件中是否包含 `\documentclass`。如果找不到，Overleaf 就会编译菜单中列为 **主** 文件中使用的

如果你选择 **重新编译** 与 `diff.tex` 在编辑器中打开，它将排版比较结果。你可以选择并编译 `main.tex` 或 `main2.tex` 因为它们都包含一个 `\documentclass` 该命令。

有关更多关于 `latexdiff` 请查看 [官方文档](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://ayakaleaf-pro.ayaka.space/latex/zh-cn/shen-du-wen-zhang/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.
