> 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/vi/bai-viet-chuyen-sau/27-how-to-use-latexdiff-on-overleaf.md).

# Cách dùng latexdiff trên Overleaf

Bài viết này minh họa hai cách sử dụng [latexdiff](https://ctan.org/pkg/latexdiff?lang=en) trên Overleaf.

## latexdiff là gì?

`latexdiff` là một tập lệnh Perl được thiết kế để so sánh nội dung của hai tệp LaTeX. Nó tạo ra mã LaTeX hiển thị trực quan mọi khác biệt đáng kể giữa các tệp đó.

Để so sánh `new.tex` với `old.tex` và xuất kết quả ra một tệp có tên `diff.tex`, bạn sẽ dùng `latexdiff` như sau:

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

`OPTIONS` là các tham số dòng lệnh mà bạn có thể dùng để cấu hình `latexdiff`hành vi của nó—hãy tham khảo [`latexdiff` tài liệu](http://mirrors.ctan.org/support/latexdiff/doc/latexdiff-man.pdf) để biết danh sách các tùy chọn có sẵn.

## Cách dùng latexdiff trên Overleaf

Chúng tôi cung cấp hai giải pháp, cả hai đều được đăng và thảo luận trên tex.stackexchage. Phương pháp [phương pháp đầu tiên](#david-carlisles-solution) đã [được David Carlisle đề xuất](https://tex.stackexchange.com/a/603311), còn [giải pháp thứ hai](#tom-hejdas-solution) đã [do Tom Hejda đề xuất](https://tex.stackexchange.com/a/603351), Quản lý Hỗ trợ của Overleaf.

### Giải pháp của David Carlisle

Các tệp nguồn để minh họa giải pháp này được liệt kê bên dưới, cùng với các liên kết để mở chúng dưới dạng một dự án Overleaf. Xem [Cách hoạt động của giải pháp đầu tiên này](#how-this-first-solution-works) để có lời giải thích ngắn gọn về đoạn mã này.

[Mở giải pháp của David Carlisle trong 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{Giải pháp của David Carlisle}
\author{tôi}
\date{Tháng 5 năm 2024}
\begin{document}
\maketitle
\section{Giới thiệu}
zzz
\end{document}
```

**main2.tex**

```latex
\documentclass{article}
\title{latexdiff}
\author{tôi\and bạn}
\date{Tháng 5 năm 2023}

\begin{document}

\maketitle

\section{Giới thiệu}
zzz zzz

\section{Mục}
zzz
\end{document}
```

**latexmkrc**

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

[Mở giải pháp của David Carlisle trong 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)

Ví dụ này tạo ra kết quả sau:

![Ảnh minh họa đầu ra do latexdiff tạo ra](/files/4e7da92dfd4768ac3fd184c453be284660d5326f)

#### Cách hoạt động của giải pháp đầu tiên này

Giải pháp này so sánh các tệp `main.tex` và `main2.tex` và ghi kết quả vào một tệp có tên `main-d.tex`. Lưu ý rằng vì `main-d.tex` tệp là một *được tạo ra* tệp, tức là được tạo bởi mã của chúng tôi, nên nó sẽ không hiển thị (được liệt kê) trong các tệp của dự án.

Nó hoạt động bằng cách sử dụng một tệp có tên `latexmkrc` chứa mã Perl để tùy chỉnh quy trình biên dịch của Overleaf. Ở đây, lệnh biên dịch bằng pdfLaTeX được định nghĩa lại để trước tiên chạy `latexdiff` sau đó thực thi pdfLaTeX để biên dịch `main-d.tex`, tệp được tạo bởi `latexdiff`.

Như [tác giả của giải pháp đã lưu ý](https://tex.stackexchange.com/a/603311), để dừng chạy `latexdiff` và dàn trang nội dung thực của `main.tex` hoặc `main2.tex`, chỉ cần thêm một `#`, ký hiệu chú thích của Perl, vào `latexmkrc` dòng lệnh, khiến nó trông như sau:

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

Bây giờ bạn có thể biên dịch `main.tex` hoặc `main2.tex` để dàn trang nội dung thực của chúng, chứ không phải phần khác biệt giữa chúng.

Để biết thêm thông tin, hãy xem bài viết Overleaf [Cách dùng `latexmkrc` với Overleaf](/latex/vi/bai-viet-chuyen-sau/28-how-to-use-latexmkrc-with-overleaf.md).

### Giải pháp của Tom Hejda

Các tệp nguồn để minh họa giải pháp này được liệt kê bên dưới, cùng với các liên kết để mở chúng dưới dạng một dự án Overleaf. Xem [Cách hoạt động của giải pháp thứ hai này](#how-this-second-solution-works) để có lời giải thích ngắn gọn về đoạn mã này.

[Mở giải pháp của Tom Hejda trong 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{Minh họa latexdiff}
```

**main.tex**

```latex
\documentclass{article}
\title{Giải pháp của Tom Hejda}
\author{tôi}
\date{Tháng 5 năm 2024}
\begin{document}
\maketitle
\section{Giới thiệu}
zzz
\end{document}
```

**main2.tex**

```latex
\documentclass{article}
\title{latexdiff}
\author{tôi\and bạn}
\date{Tháng 5 năm 2023}

\begin{document}

\maketitle

\section{Giới thiệu}
zzz zzz

\section{Mục}
zzz
\end{document}
```

[Mở giải pháp của Tom Hejda trong 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)

Ví dụ này tạo ra kết quả sau:

![Ảnh minh họa đầu ra do latexdiff tạo ra](/files/676bb3e4d11e4989a2419241b7d8e9d5a45bea2e)

#### Cách hoạt động của giải pháp thứ hai này

Giải pháp này so sánh các tệp `main.tex` và `main2.tex` và ghi kết quả vào một tệp có tên `main-d.tex`. Lưu ý rằng vì `main-d.tex` tệp là một *được tạo ra* tệp, tức là được tạo bởi mã của chúng tôi, nên nó sẽ không hiển thị (được liệt kê) trong các tệp của dự án.

Một tệp có tên `diff.tex` sử dụng `\ShellEscape` lệnh, do [`shellesc` gói](https://ctan.org/pkg/shellesc?lang=en), để thực thi `latexdiff`. Kết quả của phép so sánh, được chứa trong `main-d.tex`, được `\input` vào `diff.tex` để dàn trang.

`diff.tex` chứa dòng `\documentclass{dummy}` để đánh lừa trình biên dịch của Overleaf tin rằng `diff.tex` có thể được biên dịch. Điều đó hoạt động vì quy trình biên dịch của Overleaf kiểm tra tệp đang mở hiện tại xem có chứa `\documentclass`. Nếu không tìm thấy, Overleaf sẽ biên dịch tệp được liệt kê trong Menu như là **tệp chính** .

Để biết thêm thông tin về **Biên dịch lại** với `diff.tex` hãy xem `main.tex` hoặc `main2.tex` vì cả hai đều chứa một `\documentclass` lệnh này.

Để biết thêm thông tin về `latexdiff` hãy xem [tài liệu chính thức](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/vi/bai-viet-chuyen-sau/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.
