> 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-tw/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's solution}
\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/2d09ff64dbb0adb546f95f520e34ea5f7a5c0d69)

#### 這個第一個解決方案如何運作

此解決方案會比較檔案 `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-tw/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/9dd4c8db5c6378fa3b731dc4e26428e8818d4104)

#### 這個第二個解決方案如何運作

此解決方案會比較檔案 `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-tw/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.
