> 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/ja/xiang-xi-ji-shi/27-how-to-use-latexdiff-on-overleaf.md).

# Overleafでlatexdiffを使う方法

この記事では、2通りの使い方を示します [latexdiff](https://ctan.org/pkg/latexdiff?lang=en) Overleaf上で。

## latexdiffとは？

`latexdiff` は、2つの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を使う方法

ここでは2つの解決策を紹介します。どちらも tex.stackexchange で公開され、議論されています。 [最初の方法](#david-carlisles-solution) は [David Carlisle によって提案されました](https://tex.stackexchange.com/a/603311)、 [2つ目の解決策](#tom-hejdas-solution) は [は Tom Hejda によって提案されました](https://tex.stackexchange.com/a/603351)、Overleafのサポートマネージャーです。

### David Carlisle の解決策

この解決策を示すソースファイルを以下に、Overleafプロジェクトとして開くリンクとともに掲載しています。 [この最初の解決策の仕組み](#how-this-first-solution-works) このコードの簡単な説明について。

[David Carlisle の解決策を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 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"
```

[David Carlisle の解決策を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)

この例では次の出力が生成されます:

![latexdiffによって生成された出力を示す図](/files/e1d8fd7f576f56c07fc6757e7ef1e372d5e72f4e)

#### この最初の解決策の仕組み

この解決策はファイルを比較し `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/ja/xiang-xi-ji-shi/28-how-to-use-latexmkrc-with-overleaf.md).

### Tom Hejda の解決策

この解決策を示すソースファイルを以下に、Overleafプロジェクトとして開くリンクとともに掲載しています。 [この2つ目の解決策の仕組み](#how-this-second-solution-works) このコードの簡単な説明について。

[Tom Hejda の解決策を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{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}
```

[Tom Hejda の解決策を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)

この例では次の出力が生成されます:

![latexdiffによって生成された出力を示す図](/files/002d9ed5ae215f28ed219cb6a67594e2b6b8e3d9)

#### この2つ目の解決策の仕組み

この解決策はファイルを比較し `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/ja/xiang-xi-ji-shi/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.
