> 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/zh-cn/shen-ru-wen-zhang/27-how-to-use-latexdiff-on-overleaf.md).

# 如何在 Overleaf 中使用 latexdiff

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

## 什么是 latexdiff？

`latexdiff` 是一个旨在比较两个 LaTeX 文件内容的 Perl 脚本。它会生成 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 的解决方案}
\author{我}
\date{2024 年 5 月}
\begin{document}
\maketitle
\section{引言}
zzz
\end{document}
```

**main2.tex**

```latex
\documentclass{article}
\title{latexdiff}
\author{我\and 你}
\date{2023 年 5 月}

\begin{document}

\maketitle

\section{引言}
zzz zzz

\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-ru-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{演示 latexdiff}
```

**main.tex**

```latex
\documentclass{article}
\title{Tom Hejda 的解决方案}
\author{我}
\date{2024 年 5 月}
\begin{document}
\maketitle
\section{引言}
zzz
\end{document}
```

**main2.tex**

```latex
\documentclass{article}
\title{latexdiff}
\author{我\and 你}
\date{2023 年 5 月}

\begin{document}

\maketitle

\section{引言}
zzz zzz

\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://overleaf-pro.ayaka.space/latex/zh-cn/shen-ru-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.
