> 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/cau-truc-tai-lieu/08-multi-file-latex-projects.md).

# Các dự án LaTeX nhiều tệp

## Giới thiệu

Trong [các tài liệu LaTeX lớn](/latex/vi/cau-truc-tai-lieu/07-management-in-a-large-project.md) thường có nhiều `.tex` tệp, một tệp cho mỗi chương hoặc phần, rồi chúng được ghép lại để tạo ra một đầu ra duy nhất. Điều này giúp mọi thứ gọn gàng hơn và dễ gỡ lỗi tài liệu hơn, nhưng khi tài liệu càng lớn thì thời gian biên dịch càng lâu. Điều này có thể gây khó chịu vì thường mỗi lần người ta chỉ quan tâm làm việc trong một tệp cụ thể.

Cách tự nhiên để khắc phục điều này là biên dịch từng tệp riêng biệt. Có hai gói chính cho phép biên dịch các tệp đơn lẻ trong một dự án nhiều tệp. Lựa chọn bạn đưa ra phụ thuộc vào nhu cầu của bạn.

* Với [trang **subfiles** gói](#the-subfiles-package) bạn có thể biên dịch từng subfile một cách độc lập và mỗi subfile sẽ tự động dùng phần tiền đề trong tệp chính.
* Với [trang **standalone** gói](#the-standalone-package) mỗi subfile hoạt động như một tệp độc lập, các subfile sau này có thể được ghép lại trong một tài liệu chính sẽ lấy phần tiền đề từ từng tệp trong số đó. Đặc biệt hữu ích nếu bạn cần tái sử dụng cùng một tệp trong hơn một tài liệu; một hình TikZ là ví dụ điển hình cho điều này.

## Ghi chú về \documentclass trong các tệp .tex của Overleaf

Như minh họa trong đoạn video sau, ngay cả khi bạn đã đặt tệp chính của dự án `.tex` bạn vẫn có thể chọn một tệp khác để biên dịch—miễn là nó có chứa một `\documentclass` khai báo \documentclass. Tuy nhiên, để đảm bảo mọi thành phần của dự án được biên dịch đúng cách—chẳng hạn như bảng thuật ngữ, truy cập đường dẫn đến `.bib` các tệp, v.v.—chúng tôi đặc biệt khuyên bạn chỉ nên biên dịch các tệp nằm trong thư mục gốc của dự án.

{% embed url="<https://videos.ctfassets.net/nrgyaltdicpt/U2nGfRx9NwNGX0vWXNebP/1ca4b9a4b2f36d5ca9c0dbaa87f44bfa/maincompile.mp4>" %}

## Gói subfiles

Gói này phù hợp với hầu hết các tình huống, nó thực sự rất dễ dùng.

Các ví dụ trong phần này có cấu trúc tệp phân cấp như sau:

![CompileSubfilesEx1OverleafV2.png](/files/4cd3adf17ec64a6cc029ed87797f34f5a999a408)

### Tệp chính

Trong tệp chính cần hai lệnh đặc biệt.

```latex
\documentclass{article}
\usepackage{graphicx}
\graphicspath{{images/}}

\usepackage{blindtext}

\usepackage{subfiles} % Nên được nạp cuối cùng trong phần tiền đề

\title{Ví dụ về gói subfiles}
\author{Overleaf}
\date{ }

\begin{document}

\maketitle

\section{Giới thiệu}

\subfile{sections/introduction}

\section{Phần thứ hai}

\subfile{sections/section2}

\end{document}
```

![CompileSubfilesEx1aOverleafV2.png](/files/9ba3ddb33cd13bed7d8a0a82ce26ba294ad1f393)

Biên dịch theo từng tệp yêu cầu dòng

```latex
\usepackage{subfiles}
```

trong phần tiền đề, điều này kích hoạt `subfiles` gói này. Sau đó mỗi subfile bên ngoài phải được nhập bằng lệnh `\subfile{}`. Trong ví dụ, các tệp `introduction.tex` và `section2.tex` được nhập vào tệp chính từ thư mục `sections` . Lưu ý rằng phần mở rộng của tệp không bắt buộc.

[Mở một `subfiles` ví dụ gói này trong Overleaf](https://www.overleaf.com/project/new/template/19626?id=66392908\&templateName=Subfiles+package+example\&latexEngine=pdflatex\&texImage=texlive-full%3A2020.1\&mainFile=)

### Các subfiles

Sau khi bạn đã thiết lập tệp chính, mỗi subfile phải có một cấu trúc đặc biệt.

```latex
\documentclass[../main.tex]{subfiles}
\graphicspath{{\subfix{../images/}}}
\begin{document}
\textbf{Xin chào thế giới!}
\begin{figure}[bh]
\centering
\includegraphics[width=3cm]{overleaf-logo}

\label{fig:img1}
\caption{Logo Overleaf}
\end{figure}

Xin chào, đây là một đoạn văn bản không mang ý nghĩa gì...

\end{document}
```

![CompileSubfilesEx2OverleafV2.png](/files/3b95d98eaf7903f0a471c30b224a9cacc2daa4be)

Bây giờ tệp này có thể được biên dịch như một tệp độc lập; lớp tài liệu và phần còn lại của tiền đề sẽ giống như đã được định nghĩa trong tài liệu chính.

Lệnh đầu tiên ở đây là

```latex
\documentclass[../main.tex]{subfiles}
```

tham số bên trong dấu ngoặc vuông, `../main.tex`, là đường dẫn tương đối đến tài liệu chính. Trong trường hợp này, tệp `introduction.tex` nằm bên trong thư mục `sections`, do đó tệp `main.tex` nằm cao hơn một cấp so với thư mục hiện tại (điều này nghĩa là `../` ).

Bạn cũng sẽ cần dùng lệnh `\subfix` với đường dẫn thư mục tương đối khi chỉ định `\graphicspath` trong `introduction.tex`:

```latex
\graphicspath{{\subfix{../images/}}}
```

Sau đó phần nội dung thực tế được nhập bên trong `\begin{document}` và `\end{document}`. Mọi thứ bên ngoài môi trường này sẽ bị bỏ qua, hay cụ thể hơn, sẽ được xem là một phần của phần tiền đề. Tránh để các dòng trống ở đầu và cuối tệp.

[Mở một `subfiles` ví dụ gói này trong Overleaf](https://www.overleaf.com/project/new/template/19626?id=66392908\&templateName=Subfiles+package+example\&latexEngine=pdflatex\&texImage=texlive-full%3A2020.1\&mainFile=)

## Gói standalone

Gói **standalone** cung cấp cùng chức năng như **subfiles** và linh hoạt hơn, nhưng cú pháp phức tạp hơn và dễ gây lỗi hơn. Khác biệt chính là mỗi subfile có phần tiền đề riêng của nó.

Các ví dụ trong phần này có cấu trúc tệp phân cấp như sau:

![CompileSubfilesEx3OverleafV2.png](/files/b0fdad21ef19c0748f489551bfcb71d0b1ff43a7)

### Tệp chính

Tệp chính rất giống với của bất kỳ dự án nhiều tệp nào khác.

```latex
\documentclass{article}
\usepackage[subpreambles=true]{standalone}
\usepackage{import}

\title{Ví dụ về gói Standalone}
\author{Overleaf}
\date{Tháng 5 năm 2021}

\begin{document}

\maketitle

\section{Phần thứ nhất}
\import{sections/}{introduction}

\section{Phần thứ hai}
\import{sections/}{section2}

\end{document}
```

![CompileSubfilesEx3Overleaf.png](/files/5897e7806983684fe5b7ef136f383958551fe568)

Dòng

```latex
\usepackage[subpreambles=true]{standalone}
```

kích hoạt **standalone** gói này, nó nên được đặt sớm trong tài liệu. Tham số bên trong dấu ngoặc vuông cho LaTeX biết nhập phần tiền đề từ mỗi subfile (các gói chỉ được nhập một lần); nếu bỏ qua, hãy đảm bảo rằng bạn có tất cả các lệnh cần thiết trong phần tiền đề của tài liệu chính để các subfile hoạt động. Cần cẩn thận vì có thể xảy ra không tương thích giữa các phần tiền đề khác nhau.

Trong phần thân của tài liệu chính, mỗi subfile được nhập bằng `\import{}{}`. Lệnh chuẩn `\input{}` cũng có thể được dùng, nhưng lệnh trong ví dụ này được khuyến nghị để [quản lý các dự án lớn](/latex/vi/cau-truc-tai-lieu/07-management-in-a-large-project.md) vì nó ngăn lỗi trong các tệp lồng nhau.

[Mở một ví dụ của `standalone` gói này trong Overleaf](https://www.overleaf.com/project/new/template/19636?id=66440758\&templateName=Standalone+package+example\&latexEngine=pdflatex\&texImage=\&mainFile=)

### Các subfiles

Mỗi subfile phải có phần tiền đề riêng và nhập tất cả các gói cần thiết để hoạt động như một tài liệu độc lập.

```latex
\documentclass[class=article, crop=false]{standalone}
\usepackage[subpreambles=true]{standalone}
\usepackage{import}
\usepackage{blindtext}

\begin{document}
Một hình TikZ sẽ được hiển thị bên dưới dòng này

\begin{figure}[ht]
\centering
\subimport{../}{diagram.tex}
\label{fig:tikzexample}
\caption{Một sơ đồ đơn giản đẹp}
\end{figure}

\blindtext
\end{document}
```

![CompileSubfilesEx4OverleafV2.png](/files/b4db93acf10a81e3745e7e4c066e101d04d4a404)

Dòng đầu tiên trong subfile là

```latex
\documentclass[class=article, crop=false]{standalone}
```

Điều này khai báo rằng đây là một tệp được dùng với **standalone** gói này, có hai tham số tùy chọn bên trong dấu ngoặc vuông.

* `class=article`. Đặt `article` làm lớp cơ sở, có thể dùng bất kỳ lớp nào khác: book, report, v.v. (trừ beamer).
* `crop=false`. Nếu bỏ tùy chọn này, đầu ra sẽ được cắt gọn về kích thước tối thiểu.

Lệnh tiếp theo không bắt buộc

```latex
\usepackage[subpreambles=true]{standalone}
```

nhưng phải được dùng trong ví dụ này vì ở đây có một **standalone** tệp lồng nhau. Một [hình TikZ](/latex/vi/hinh-va-bang/05-tikz-package.md) được chèn bằng [`\subimport{../}{diagram.tex}`](/latex/vi/cau-truc-tai-lieu/07-management-in-a-large-project.md#importing-files), bạn có thể xem nội dung của tệp "diagram.tex" bên dưới:

```latex
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}

\begin{tikzpicture}[
roundnode/.style={circle, draw=green!60, fill=green!5, very thick, minimum size=7mm},
squarednode/.style={rectangle, draw=red!60, fill=red!5, very thick, minimum size=5mm},
]
%Nút
\node[squarednode]      (maintopic)                              {2};
\node[roundnode]        (uppercircle)       [above=of maintopic] {1};
\node[squarednode]      (rightsquare)       [right=of maintopic] {3};
\node[roundnode]        (lowercircle)       [below=of maintopic] {4};

%Các đường
\draw[->] (uppercircle.south) -- (maintopic.north);
\draw[->] (maintopic.east) -- (rightsquare.west);
\draw[->] (rightsquare.south) .. controls +(down:7mm) and +(right:7mm) .. (lowercircle.east);

\end{tikzpicture}
\end{document}
```

[Mở ví dụ Standalone TikZ này trong Overleaf](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Standalone+TikZ+example\&snip=%5Cdocumentclass%7Bstandalone%7D%0A%5Cusepackage%7Btikz%7D%0A%5Cusetikzlibrary%7Bpositioning%7D%0A%5Cbegin%7Bdocument%7D%0A%0A%5Cbegin%7Btikzpicture%7D%5B%0Aroundnode%2F.style%3D%7Bcircle%2C+draw%3Dgreen%2160%2C+fill%3Dgreen%215%2C+very+thick%2C+minimum+size%3D7mm%7D%2C%0Asquarednode%2F.style%3D%7Brectangle%2C+draw%3Dred%2160%2C+fill%3Dred%215%2C+very+thick%2C+minimum+size%3D5mm%7D%2C%0A%5D%0A%25Nodes%0A%5Cnode%5Bsquarednode%5D++++++%28maintopic%29++++++++++++++++++++++++++++++%7B2%7D%3B%0A%5Cnode%5Broundnode%5D++++++++%28uppercircle%29+++++++%5Babove%3Dof+maintopic%5D+%7B1%7D%3B%0A%5Cnode%5Bsquarednode%5D++++++%28rightsquare%29+++++++%5Bright%3Dof+maintopic%5D+%7B3%7D%3B%0A%5Cnode%5Broundnode%5D++++++++%28lowercircle%29+++++++%5Bbelow%3Dof+maintopic%5D+%7B4%7D%3B%0A%0A%25Lines%0A%5Cdraw%5B-%3E%5D+%28uppercircle.south%29+--+%28maintopic.north%29%3B%0A%5Cdraw%5B-%3E%5D+%28maintopic.east%29+--+%28rightsquare.west%29%3B%0A%5Cdraw%5B-%3E%5D+%28rightsquare.south%29+..+controls+%2B%28down%3A7mm%29+and+%2B%28right%3A7mm%29+..+%28lowercircle.east%29%3B%0A%0A%5Cend%7Btikzpicture%7D%0A%5Cend%7Bdocument%7D)

![CompileSubfilesEx5.png](/files/ee0f88fa4c9de8a4c43000926f4b29511f50767a)

Đây là tính năng chính trong **standalone**, bạn có thể nhập tệp này vào bất kỳ tài liệu nào khác và tái sử dụng mã. Ví dụ, sơ đồ này sau đó có thể được dùng trong một bài thuyết trình mà không cần thay đổi gì thêm.

[Mở một ví dụ của `standalone` gói này trong Overleaf](https://www.overleaf.com/project/new/template/19636?id=66440758\&templateName=Standalone+package+example\&latexEngine=pdflatex\&texImage=\&mainFile=)

## Đọc thêm

Để biết thêm thông tin, xem

* [Quản lý trong một dự án lớn](/latex/vi/cau-truc-tai-lieu/07-management-in-a-large-project.md)
* [Liên kết chéo các phần và phương trình](/latex/vi/cau-truc-tai-lieu/03-cross-referencing-sections-equations-and-floats.md)
* [Chỉ mục](/latex/vi/cau-truc-tai-lieu/04-indices.md)
* [Bảng thuật ngữ](/latex/vi/cau-truc-tai-lieu/05-glossaries.md)
* [Siêu liên kết](/latex/vi/cau-truc-tai-lieu/09-hyperlinks.md)
* [Đánh số trang](/latex/vi/dinh-dang/03-page-numbering.md)
* [Tài liệu một mặt và hai mặt](/latex/vi/dinh-dang/08-single-sided-and-double-sided-documents.md)
* [Nhiều cột](/latex/vi/dinh-dang/09-multiple-columns.md)
* [Định dạng đoạn văn](/latex/vi/dinh-dang/04-articles-how-to-change-paragraph-spacing-in-latex.md)
* [Kích cỡ trang và lề](/latex/vi/dinh-dang/07-page-size-and-margins.md)
* [Bộ đếm](/latex/vi/dinh-dang/10-counters.md)
* [Ghi chú lề](/latex/vi/dinh-dang/15-margin-notes.md)
* [Đậm, nghiêng và gạch chân](/latex/vi/kien-thuc-co-ban-ve-latex/03-bold-italics-and-underlining.md)
* [Kích thước phông chữ, họ phông và kiểu chữ](/latex/vi/phong-chu/01-font-sizes-families-and-styles.md)
* [Kiểu chữ](/latex/vi/phong-chu/02-font-typefaces.md)
* [Hỗ trợ phông chữ hiện đại với XeLaTeX](/latex/vi/phong-chu/03-xelatex.md)
* [Hỗ trợ ngôn ngữ quốc tế](/latex/vi/ngon-ngu/03-international-language-support.md)
* [Kích thước phông chữ, họ phông và kiểu chữ](/latex/vi/phong-chu/01-font-sizes-families-and-styles.md)
* [Biên dịch các dự án lớn](/latex/vi/co-so-tri-thuc/038-fixing-and-preventing-compile-timeouts.md#compiling-big-projects)
* [Chèn hình ảnh](/latex/vi/chu-de-khac/27-inserting-images.md)
* [Gói TikZ](/latex/vi/hinh-va-bang/05-tikz-package.md)
* [Viết gói của riêng bạn](/latex/vi/tep-lop/03-writing-your-own-package.md)
* [Viết lớp của riêng bạn](/latex/vi/tep-lop/04-writing-your-own-class.md)
* [Tính năng **subfiles** tài liệu gói](http://repositorios.cpai.unb.br/ctan/macros/latex/contrib/subfiles/subfiles.pdf)
* [Tính năng **standalone** tài liệu gói](http://repositorios.cpai.unb.br/ctan/macros/latex/contrib/standalone/standalone.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/cau-truc-tai-lieu/08-multi-file-latex-projects.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.
