> 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/phong-chu/03-xelatex.md).

# Hỗ trợ phông hiện đại với XƎLaTeX

## Giới thiệu

[XeTeX](https://tug.org/xetex/) là một bộ máy dàn trang có nguồn gốc từ phần mềm TeX nguyên bản của Donald Knuth. Tuy nhiên, không giống chương trình gốc của Knuth, XeTeX tự đọc (nhập) văn bản Unicode được mã hóa UTF-8 và, với sự hỗ trợ của bộ máy [HarfBuzz](https://harfbuzz.github.io/) xử lý có sẵn, hỗ trợ các định dạng phông chữ hiện đại như [OpenType (OTF)](https://docs.microsoft.com/en-us/typography/opentype/) và [công nghệ phông Graphite của SIL](https://scripts.sil.org/cms/scripts/page.php?site_id=projects\&item_id=graphite_home). Trình biên dịch LaTeX tương ứng [trình biên dịch](/latex/vi/chu-de-khac/08-choosing-a-latex-compiler.md) trên Overleaf được gọi là `xelatex`. Bất kỳ ai quan tâm muốn hiểu rõ hơn về sự tiến hóa của các bộ máy TeX khác nhau có thể muốn đọc bài viết của Overleaf [Tên gọi là gì: Hướng dẫn về nhiều biến thể của TeX](/latex/vi/bai-viet-chuyen-sau/55-what-s-in-a-name-a-guide-to-the-many-flavours-of-tex.md).

## Những điều cơ bản: Times New Roman

Times New Roman là một phông chữ được dùng phổ biến và dưới đây là một ví dụ về cách sử dụng nó trên Overleaf thông qua trình biên dịch XeLaTeX. Trong ví dụ này, phông chữ của tài liệu được đặt bằng `\usemainfont{Times New Roman}`, trong đó lệnh `\usemainfont{...}` được cung cấp bởi gói `fontspec`.

```latex
\documentclass[12pt]{article}
\usepackage{fontspec}

\setmainfont{Times New Roman}
\title{Tài liệu phông chữ mẫu}
\author{Overleaf}
\date{\today}

\begin{document}
\maketitle

Đây là một \textit{ví dụ} về một tài liệu được biên dịch
với \textbf{XeLaTeX}. LuaLaTeX cũng sẽ hoạt động.

\end{document}
```

[Ví dụ mở Times New Roman bằng XeLaTeX trên Overleaf](https://www.overleaf.com/docs?engine=xelatex\&snip_name=Times+New+Roman+%28via+XeLaTeX%29\&snip=%5Cdocumentclass%5B12pt%5D%7Barticle%7D%0A%5Cusepackage%7Bfontspec%7D%0A+%0A%5Csetmainfont%7BTimes+New+Roman%7D%0A%5Ctitle%7BSample+font+document%7D%0A%5Cauthor%7BOverleaf%7D%0A%5Cdate%7B%5Ctoday%7D%0A+++%0A%5Cbegin%7Bdocument%7D%0A%5Cmaketitle%0A+++++%0AThis+is+an+%5Ctextit%7Bexample%7D+of+a+document+compiled+%0Awith+%5Ctextbf%7BXeLaTeX%7D.+LuaLaTeX+should+also+work+too.%0A%0A%5Cend%7Bdocument%7D)

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

![XeLaTeXOverleaf.png](/files/9c727ea936bc0a6b29590adf62f76ed46bbc6877)

Bạn cũng có thể [dùng LuaLaTeX để chạy cùng đoạn mã](https://www.overleaf.com/docs?engine=lualatex\&snip_name=Times+New+Roman+%28via+LuaLaTeX%29\&snip=%5Cdocumentclass%5B12pt%5D%7Barticle%7D%0A%5Cusepackage%7Bfontspec%7D%0A+%0A%5Csetmainfont%7BTimes+New+Roman%7D%0A%5Ctitle%7BSample+font+document%7D%0A%5Cauthor%7BOverleaf%7D%0A%5Cdate%7B%5Ctoday%7D%0A+++%0A%5Cbegin%7Bdocument%7D%0A%5Cmaketitle%0A+++++%0AThis+is+an+%5Ctextit%7Bexample%7D+of+a+document+compiled+%0Awith+%5Ctextbf%7BLuaLaTeX%7D.%0A%0A%5Cend%7Bdocument%7D) .

## Thiết lập phông chữ cho các phần tử LaTeX khác nhau

Các phần tử khác nhau trong tài liệu LaTeX được định dạng bằng các phông chữ khác nhau; ví dụ, trong một `verbatim` môi trường, văn bản được hiển thị bằng kiểu chữ máy đánh chữ. Có thể đặt các kiểu phông chữ khác nhau cho từng ngữ cảnh:

```latex
\documentclass[12pt]{article}
\usepackage{fontspec}

%-----------------------------------------------------------------------
\setmainfont{Times New Roman}
\setsansfont{Arial}
\setmonofont[Color={0019D4}]{Courier New}
%-----------------------------------------------------------------------

\title{Tài liệu phông chữ mẫu}
\author{Overleaf}
\date{\today}
\begin{document}

\maketitle

Đây là một \textit{ví dụ} của tài liệu được biên dịch với
trình biên dịch \textbf{XeLaTeX}. Nếu bạn cần viết một số mã, bạn có thể
dùng môi trường \texttt{verbatim}:

    \begin{verbatim}
    Thông thường môi trường này được dùng để hiển thị mã,
    vì vậy đây là một chương trình C đơn giản.

    #include <stdio.h>
    #define NUMYAYS 10

    int main()
    {
        int i;
        for (i = 0; i <= NUMYAYS; i++)
        {
            printf("Yay! Overleaf thật tuyệt!\n");
        }
        return 0;
    }
    \end{verbatim}
    {\sffamily Đây là một đoạn văn bản mẫu bằng kiểu phông \textbf{Sans Serif}}

\end{document}
```

[Để xem kết quả, hãy mở ví dụ XeLaTeX này trên Overleaf](https://www.overleaf.com/docs?engine=xelatex\&snip_name=Document+formatted+with+different+fonts\&snip=%5Cdocumentclass%5B12pt%5D%7Barticle%7D%0A%5Cusepackage%7Bfontspec%7D%0A%0A%25-----------------------------------------------------------------------%0A%5Csetmainfont%7BTimes+New+Roman%7D%0A%5Csetsansfont%7BArial%7D%0A%5Csetmonofont%5BColor%3D%7B0019D4%7D%5D%7BCourier+New%7D%0A%25-----------------------------------------------------------------------%0A%0A%5Ctitle%7BSample+font+document%7D%0A%5Cauthor%7BOverleaf%7D%0A%5Cdate%7B%5Ctoday%7D%0A%5Cbegin%7Bdocument%7D%0A++++%0A%5Cmaketitle%0A+++++%0AThis+an+%5Ctextit%7Bexample%7D+of+document+compiled+with+the++%0A%5Ctextbf%7BXeLaTeX%7D+compiler.+If+you+have+to+write+some+code+you+can+%0Ause+the+%5Ctexttt%7Bverbatim%7D+environment%3A%0A%0A++++%5Cbegin%7Bverbatim%7D%0A++++Usually+this+environment+is+used+to+display+code%2C+%0A++++so+here%27s+a+simple+C+program.%0A%0A++++%23include+%3Cstdio.h%3E%0A++++%23define+NUMYAYS+10%0A++++++%0A++++int+main%28%29%0A++++%7B%0A++++++++int+i%3B+++%0A++++++++for+%28i+%3D+0%3B+i+%3C%3D+NUMYAYS%3B+i%2B%2B%29+%0A++++++++%7B%0A++++++++++++printf%28%22Yay%21+Overleaf+is+Awesome%21%5Cn%22%29%3B%0A++++++++%7D%0A++++++++return+0%3B%0A++++%7D%0A++++%5Cend%7Bverbatim%7D%0A++++%7B%5Csffamily+This+is+a+sample+text+in+%5Ctextbf%7BSans+Serif+Font+Typeface%7D%7D%0A+++++++%0A%5Cend%7Bdocument%7D)

Trong ví dụ trước, ba phông chữ khác nhau được dùng; các dòng tiếp theo xác định những phần tử sẽ dùng các phông chữ này:

**\setmainfont{Times New Roman}**

Đây là phông chữ bình thường được dùng trong phần lớn tài liệu, Times New Roman trong ví dụ.

**\setsansfont{Arial}**

Các phần tử cần phông sans, được khai báo rõ ràng bằng \ssfamily trong ví dụ, sẽ được dàn trang bằng phông Arial.

**\setmonofont{Courier New}**

Mọi thứ được định dạng bằng phông chữ có độ rộng cố định (giống máy đánh chữ) sẽ dùng phông Courier New. Lệnh này có thêm một tham số tùy chọn bên trong dấu ngoặc nhọn:

**Color={0019D4}**

Điều này đặt màu của văn bản, sử dụng giá trị Color= theo định dạng thập lục phân của HTML. Việc sử dụng màu văn bản một cách chọn lọc và cẩn thận có thể hữu ích khi làm bài thuyết trình.

## Phông chữ trên Overleaf

Có [rất nhiều phông chữ được cài đặt trên Overleaf](/latex/vi/cau-hoi-and-tra-loi/85-which-otf-or-ttf-fonts-are-supported-via-fontspec.md) mà bạn có thể dùng trong dự án của mình, dễ dàng nhất là thông qua `fontspec`. Các ví dụ sau cho thấy việc dùng các phông chữ có sẵn trên máy chủ của Overleaf, chẳng hạn như Google Noto, cùng với các phông chữ bạn cần tải lên từ [Google Fonts](https://fonts.google.com/).

### Phông chữ được cài đặt trên máy chủ của Overleaf: các phông chữ Google Noto

Bởi vì [TeX Live](https://www.tug.org/texlive/)—được Overleaf sử dụng—bao gồm [các phông chữ Google Noto](https://www.google.com/get/noto/) bạn có thể dùng chúng, thông qua gói `fontspec` trong các dự án Overleaf được biên dịch bằng trình biên dịch XeLaTeX hoặc LuaLaTeX. Đây là một dự án mẫu sử dụng [`noto` gói](https://ctan.org/pkg/noto?lang=en) để cấu hình tài liệu của bạn cho việc dàn trang với các họ phông NotoSerif, NotoSans và NotoSansMono. Tài liệu cho gói `noto` có thể tìm thấy trong tệp [readme trên CTAN](http://mirrors.ctan.org/fonts/noto/README).

```latex
\documentclass{article}
\usepackage{xcolor}
\usepackage{noto}
\usepackage{hyperref}
\title{Sử dụng phông chữ Google Noto}
\author{Overleaf}
\date{Tháng 4 năm 2021}

\begin{document}

\maketitle

\section{Giới thiệu}
Dự án mẫu này sử dụng gói \href{https://ctan.org/pkg/noto?lang=en}{\color{blue}\texttt{noto}} để dàn trang tài liệu của bạn bằng các phông chữ Noto của Google\footnote{\url{https://www.google.com/get/noto/}}:
\begin{itemize}
\item \verb|\textbf{bold}| tạo ra \textbf{đậm}
\item \verb|\textit{italic}| tạo ra \textit{nghiêng}
\item \verb|\textbf{\textit{bold italic}}| tạo ra \textbf{\textit{đậm nghiêng}}
\item \verb|\emph{emphasis}| tạo ra \emph{nhấn mạnh}
\item \verb|\textbf{\emph{bold italic}}| tạo ra \textbf{\emph{đậm nghiêng}}
\end{itemize}

\subsection{Phông chữ có độ rộng cố định}
Bạn có thể dùng các phông chữ có độ rộng cố định của Noto cho văn bản \texttt{thường} và \texttt{\textbf{đậm}} có độ rộng cố định.

\subsection{Phông chữ sans serif}
Đây là một số \textsf{văn bản được dàn trang bằng phông sans serif} cùng với \textbf{\textsf{văn bản được dàn trang bằng sans serif đậm}}.

\section{Đọc thêm}
Tài liệu cho gói \texttt{noto} có thể được tìm thấy trong tệp \href{http://mirrors.ctan.org/fonts/noto/README}{\color{blue}\texttt{readme} trên CTAN}.

\end{document}
```

[Mở ví dụ XeLaTeX này trên Overleaf để khám phá các phông chữ Noto](https://www.overleaf.com/docs?engine=xelatex\&snip_name=Using+Google+Noto+fonts\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cusepackage%7Bxcolor%7D%0A%5Cusepackage%7Bnoto%7D%0A%5Cusepackage%7Bhyperref%7D%0A%5Ctitle%7BUsing+Google+Noto+fonts%7D%0A%5Cauthor%7BOverleaf%7D%0A%5Cdate%7BApril+2021%7D%0A%0A%5Cbegin%7Bdocument%7D%0A%0A%5Cmaketitle%0A%0A%5Csection%7BIntroduction%7D%0AThis+example+project+uses+the+%5Chref%7Bhttps%3A%2F%2Fctan.org%2Fpkg%2Fnoto%3Flang%3Den%7D%7B%5Ccolor%7Bblue%7D%5Ctexttt%7Bnoto%7D%7D+package+to+typeset+your+document+using+Google%27s+Noto+fonts%5Cfootnote%7B%5Curl%7Bhttps%3A%2F%2Fwww.google.com%2Fget%2Fnoto%2F%7D%7D%3A%0A%5Cbegin%7Bitemize%7D%0A%5Citem+%5Cverb%7C%5Ctextbf%7Bbold%7D%7C+produces+%5Ctextbf%7Bbold%7D%0A%5Citem+%5Cverb%7C%5Ctextit%7Bitalic%7D%7C+produces+%5Ctextit%7Bitalic%7D%0A%5Citem+%5Cverb%7C%5Ctextbf%7B%5Ctextit%7Bbold+italic%7D%7D%7C+produces+%5Ctextbf%7B%5Ctextit%7Bbold+italic%7D%7D%0A%5Citem+%5Cverb%7C%5Cemph%7Bemphasis%7D%7C+produces+%5Cemph%7Bemphasis%7D%0A%5Citem+%5Cverb%7C%5Ctextbf%7B%5Cemph%7Bbold+italic%7D%7D%7C+produces+%5Ctextbf%7B%5Cemph%7Bbold+italic%7D%7D%0A%5Cend%7Bitemize%7D%0A%0A%5Csubsection%7BMonospaced+fonts%7D%0AYou+can+use+Noto%27s+monospaced+fonts+for+%5Ctexttt%7Bregular%7D+and+%5Ctexttt%7B%5Ctextbf%7Bbold%7D%7D+monospaced+text.%0A%0A%5Csubsection%7BSans+serif+fonts%7D%0AHere+is+some+%5Ctextsf%7Btext+is+typeset+in+a+sans+serif+font%7D+together+with+%5Ctextbf%7B%5Ctextsf%7Btext+typeset+in+bold+sans+serif%7D%7D.%0A%0A%5Csection%7BFurther+reading%7D%0ADocumentation+for+the+%5Ctexttt%7Bnoto%7D+package+can+be+found+in+its+%5Chref%7Bhttp%3A%2F%2Fmirrors.ctan.org%2Ffonts%2Fnoto%2FREADME%7D%7B%5Ccolor%7Bblue%7D%5Ctexttt%7Breadme%7D+file+on+CTAN%7D.%0A%0A%5Cend%7Bdocument%7D)

![Sử dụng phông chữ Google Noto để dàn trang một tài liệu LaTeX](/files/05d0cce51de0a23148487cbd8bd77c4644b9dc97)

### Sử dụng các phông chữ không được cài đặt trên máy chủ của Overleaf: ví dụ Google Fonts

Để sử dụng các phông chữ OpenType không được cài đặt trên máy chủ của Overleaf, trước hết bạn phải tải các tệp phông chữ lên dự án Overleaf của mình—miễn là giấy phép của phông chữ cho phép. Sau khi các phông chữ OpenType đã được tải lên, hãy đặt bộ biên dịch của dự án thành XƎLaTeX (hoặc LuaLaTeX) và dùng `fontspec` để cấu hình các phông chữ của bạn.

Dự án mẫu được phác thảo dưới đây cho thấy cách tạo thư mục, tải phông chữ lên và cấu hình chúng với `fontspec`. Với ví dụ của chúng ta, ta sẽ dùng bộ phông sau từ [Google Fonts](https://fonts.google.com/):

* Phông chữ serif: [Brygada1918](https://fonts.google.com/specimen/Brygada+1918)
* Phông chữ sans serif: [Asap](https://fonts.google.com/specimen/Asap)
* Phông chữ monospace: [JetBrains Mono](https://fonts.google.com/specimen/JetBrains+Mono)

Lưu ý: Chúng tôi không cho rằng ví dụ này cung cấp một sự kết hợp hài hòa về mặt kiểu chữ, nó chỉ đơn thuần cho thấy cách tải lên, cấu hình và sử dụng các phông chữ theo lựa chọn của bạn.

Với mỗi kiểu chữ, bạn cần tải xuống họ phông chữ từ Google Fonts:

![GoogleDownload.png](/files/e6f2bbcc161d272d29001fd175e9290196d38a79)

Lưu ý: Một số phông chữ Google có sẵn dưới dạng phông OpenType "tĩnh" và phông OpenType "biến đổi". Nếu tệp ZIP đã tải xuống có một thư mục tên là "static" thì bạn nên tải lên các phông chữ trong thư mục đó, chứ không phải các phiên bản phông biến đổi:

![OpenTypeStatic.png](/files/3030f0c96710e07498c733ee78e730cf26d3e063)

Tiếp theo, bạn cần tải các tệp phông chữ lên dự án của mình, nhưng vì các họ phông chữ chứa nhiều tệp phông chữ, bạn có thể muốn tạo các thư mục dự án Overleaf riêng lẻ để chứa các tệp đó—mỗi thư mục cho một họ phông chữ. Video sau đây cho thấy cách làm điều đó. Sau này, chúng ta sẽ cần hướng dẫn XƎLaTeX tìm nơi chứa các phông chữ của dự án bạn—vì chúng không được cài sẵn trên máy chủ của Overleaf.

{% embed url="<https://videos.ctfassets.net/nrgyaltdicpt/4lQFkZ1Ni1FHFdHJUJSNH4/4aee7c5bafeb021a6f21b4637d0fe6ec/UploadingGoogleFonts.mp4>" %}

### Cấu hình fontspec để dùng các phông chữ

Sau khi tải các tệp họ phông chữ vào các thư mục dự án Overleaf, chúng ta có thể cấu hình để sử dụng chúng thông qua `fontspec`. Trong đoạn mã LaTeX sau, hãy lưu ý cách chúng ta đã dùng tham số `Path` để báo cho `fontspec` rằng các tệp phông chữ nằm trong một thư mục cục bộ trong hệ thống tệp của dự án chúng ta:

```latex
\setmainfont{Brygada1918}[
    Path=./BrygadaFontFiles/,
    Extension = .ttf,
    UprightFont=*-Regular,
    BoldFont=*-Bold,
    ItalicFont=*-Italic,
    BoldItalicFont=*-BoldItalic
    ]

\setsansfont{Asap}[
    Path=./AsapFontFiles/,
    Scale=0.9,
    Extension = .ttf,
    UprightFont=*-Regular,
    BoldFont=*-Bold,
    ItalicFont=*-Italic,
    BoldItalicFont=*-BoldItalic
    ]

\setmonofont{JetBrainsMono}[
    Path=./JetbrainsFontFiles/,
    Scale=0.85,
    Extension = .ttf,
    UprightFont=*-Regular,
    BoldFont=*-Bold,
    ItalicFont=*-Italic,
    BoldItalicFont=*-BoldItalic
    ]
```

![FontSpecPathUpdated.png](/files/949b8674abcd032b70916121a8fba29763301ee8)

[Mở bản demo XeLaTeX và Google Fonts trên Overleaf](https://www.overleaf.com/project/new/template/31226?id=835225587\&templateName=XeLaTeX+font+demo+with+Google+Fonts\&latexEngine=xelatex\&texImage=texlive-full%3A2022.1\&mainFile=)

Tính năng [`fontspec` gói](https://ctan.org/pkg/fontspec?lang=en) cung cấp rất nhiều tùy chọn để cấu hình phông OpenType dùng với LuaLaTeX hoặc XƎLaTeX—phần [tài liệu xuất sắc](http://mirrors.ctan.org/macros/unicodetex/latex/fontspec/fontspec.pdf) chứa nhiều ví dụ mà bạn có thể khám phá để tinh chỉnh thiết kế và việc dàn trang của tài liệu.

## Đọc thêm

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

* [In đậm, in 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)
* [Định dạng đoạn văn](/latex/vi/dinh-dang/04-articles-how-to-change-paragraph-spacing-in-latex.md)
* [Ngắt dòng và khoảng trắng](/latex/vi/dinh-dang/05-line-breaks-and-blank-spaces.md)
* [Căn chỉnh văn bản](/latex/vi/dinh-dang/06-text-alignment.md)
* [Nhiều cột](/latex/vi/dinh-dang/09-multiple-columns.md)
* [Quản lý trong một dự án lớn](/latex/vi/cau-truc-tai-lieu/07-management-in-a-large-project.md)
* [Các dự án LaTeX nhiều tệp](/latex/vi/cau-truc-tai-lieu/08-multi-file-latex-projects.md)
* [Hỗ trợ ngôn ngữ quốc tế](/latex/vi/ngon-ngu/03-international-language-support.md)
* [Chọn một bộ biên dịch LaTeX](/latex/vi/chu-de-khac/08-choosing-a-latex-compiler.md)
* [Danh sách các phông OTF và TTF được cài đặt trên Overleaf](/latex/vi/cau-hoi-and-tra-loi/85-which-otf-or-ttf-fonts-are-supported-via-fontspec.md)
* [bộ sưu tập phông chữ của Google](http://www.google.com/fonts/)
* [`fontspec` sổ tay hướng dẫn gói](http://tug.ctan.org/tex-archive/macros/latex/contrib/fontspec/fontspec.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/phong-chu/03-xelatex.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.
