> 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/dinh-dang/12-code-highlighting-with-minted.md).

# Tô sáng mã với minted

## Giới thiệu

Bài viết này cho thấy cách sử dụng [`minted` gói](https://ctan.org/pkg/minted?lang=en) để định dạng và tô sáng mã nguồn của ngôn ngữ lập trình trong tài liệu LaTeX, bắt đầu với một ví dụ:

```latex
\documentclass{article}
\usepackage{minted}
\begin{document}
\begin{minted}{python}
import numpy as np

def incmatrix(genl1,genl2):
    m = len(genl1)
    n = len(genl2)
    M = None #để trở thành ma trận liên thuộc
    VT = np.zeros((n*m,1), int)  #biến giả

    #tính ma trận XOR theo từng bit
    M1 = bitxormatrix(genl1)
    M2 = np.triu(bitxormatrix(genl2),1)

    for i in range(m-1):
        for j in range(i+1, m):
            [r,c] = np.where(M2 == M1[i,j])
            for k in range(len(r)):
                VT[(i)*n + r[k]] = 1;
                VT[(i)*n + c[k]] = 1;
                VT[(j)*n + r[k]] = 1;
                VT[(j)*n + c[k]] = 1;

                if M is None:
                    M = np.copy(VT)
                else:
                    M = np.concatenate((M, VT), 1)

                VT = np.zeros((n*m,1), int)

    return M
\end{minted}
\end{document}
```

[Mở ví dụ này trong Overleaf](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Example+of+the+minted+package\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cusepackage%7Bminted%7D%0A%5Cbegin%7Bdocument%7D%0A%5Cbegin%7Bminted%7D%7Bpython%7D%0Aimport+numpy+as+np%0A++++%0Adef+incmatrix%28genl1%2Cgenl2%29%3A%0A++++m+%3D+len%28genl1%29%0A++++n+%3D+len%28genl2%29%0A++++M+%3D+None+%23to+become+the+incidence+matrix%0A++++VT+%3D+np.zeros%28%28n%2Am%2C1%29%2C+int%29++%23dummy+variable%0A++++%0A++++%23compute+the+bitwise+xor+matrix%0A++++M1+%3D+bitxormatrix%28genl1%29%0A++++M2+%3D+np.triu%28bitxormatrix%28genl2%29%2C1%29+%0A%0A++++for+i+in+range%28m-1%29%3A%0A++++++++for+j+in+range%28i%2B1%2C+m%29%3A%0A++++++++++++%5Br%2Cc%5D+%3D+np.where%28M2+%3D%3D+M1%5Bi%2Cj%5D%29%0A++++++++++++for+k+in+range%28len%28r%29%29%3A%0A++++++++++++++++VT%5B%28i%29%2An+%2B+r%5Bk%5D%5D+%3D+1%3B%0A++++++++++++++++VT%5B%28i%29%2An+%2B+c%5Bk%5D%5D+%3D+1%3B%0A++++++++++++++++VT%5B%28j%29%2An+%2B+r%5Bk%5D%5D+%3D+1%3B%0A++++++++++++++++VT%5B%28j%29%2An+%2B+c%5Bk%5D%5D+%3D+1%3B%0A++++++++++++++++%0A++++++++++++++++if+M+is+None%3A%0A++++++++++++++++++++M+%3D+np.copy%28VT%29%0A++++++++++++++++else%3A%0A++++++++++++++++++++M+%3D+np.concatenate%28%28M%2C+VT%29%2C+1%29%0A++++++++++++++++%0A++++++++++++++++VT+%3D+np.zeros%28%28n%2Am%2C1%29%2C+int%29%0A++++%0A++++return+M%0A%5Cend%7Bminted%7D%0A%5Cend%7Bdocument%7D)

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

![Ví dụ hiển thị đầu ra của gói minted](/files/032f6b467565374902f3d28f7a16b3bd9ed47bee)

Có hai lệnh quan trọng ở đây. Trong phần tiền đề, gói được nhập bằng cách viết

```latex
\usepackage{minted}
```

sau đó các thẻ `\begin{minted}{python}` và `\end{minted}` tạo thành một môi trường in văn bản nguyên văn bằng phông chữ đơn cách và cũng áp dụng màu cho chú thích, từ khóa và hàm. Tham số `python` là ngôn ngữ lập trình mà mã nguồn được viết bằng. `minted` hỗ trợ hơn 150 ngôn ngữ lập trình và ngôn ngữ đánh dấu cũng như các tệp cấu hình, xem [hướng dẫn tham khảo](#reference-guide) để xem danh sách các ngôn ngữ được hỗ trợ.

**Lưu ý**: Để `minted` hoạt động với *cục bộ* bản phân phối LaTeX của bạn, cần cài đặt thêm một chương trình có tên là [Pygments](https://pygments.org/) phải được cài đặt. [Overleaf](https://www.overleaf.com/) có thể giúp bạn khỏi phải cài đặt nó và khỏi phải chạy các lệnh đặc biệt để biên dịch tài liệu của bạn—trên Overleaf, các tài liệu sử dụng `minted` sẽ hoạt động "ngay khi dùng".

## Cách sử dụng cơ bản

Như được minh họa trong ví dụ sau, `minted` môi trường có thể được cấu hình để thay đổi cách trình bày trực quan của mã đã dàn trang. Ở đây, `minted` môi trường sử dụng một số tham số phân tách bằng dấu phẩy theo dạng `key=value`:

```latex
\documentclass{article}
\usepackage{minted}
\usepackage{xcolor} % để truy cập màu có tên LightGray
\definecolor{LightGray}{gray}{0.9}
\begin{document}
\begin{minted}
[
frame=lines,
framesep=2mm,
baselinestretch=1.2,
bgcolor=LightGray,
fontsize=\footnotesize,
linenos
]
{python}
import numpy as np

def incmatrix(genl1,genl2):
    m = len(genl1)
    n = len(genl2)
    M = None #để trở thành ma trận liên thuộc
    VT = np.zeros((n*m,1), int)  #biến giả

    #tính ma trận XOR theo từng bit
    M1 = bitxormatrix(genl1)
    M2 = np.triu(bitxormatrix(genl2),1)

    for i in range(m-1):
        for j in range(i+1, m):
            [r,c] = np.where(M2 == M1[i,j])
            for k in range(len(r)):
                VT[(i)*n + r[k]] = 1;
                VT[(i)*n + c[k]] = 1;
                VT[(j)*n + r[k]] = 1;
                VT[(j)*n + c[k]] = 1;

                if M is None:
                    M = np.copy(VT)
                else:
                    M = np.concatenate((M, VT), 1)

                VT = np.zeros((n*m,1), int)

    return M
\end{minted}
\end{document}
```

[Mở ví dụ này trong Overleaf](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Another+example+of+the+minted+package\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cusepackage%7Bminted%7D%0A%5Cusepackage%7Bxcolor%7D+%25+to+access+the+named+colour+LightGray%0A%5Cdefinecolor%7BLightGray%7D%7Bgray%7D%7B0.9%7D%0A%5Cbegin%7Bdocument%7D%0A%5Cbegin%7Bminted%7D%0A%5B%0Aframe%3Dlines%2C%0Aframesep%3D2mm%2C%0Abaselinestretch%3D1.2%2C%0Abgcolor%3DLightGray%2C%0Afontsize%3D%5Cfootnotesize%2C%0Alinenos%0A%5D%0A%7Bpython%7D%0Aimport+numpy+as+np%0A++++%0Adef+incmatrix%28genl1%2Cgenl2%29%3A%0A++++m+%3D+len%28genl1%29%0A++++n+%3D+len%28genl2%29%0A++++M+%3D+None+%23to+become+the+incidence+matrix%0A++++VT+%3D+np.zeros%28%28n%2Am%2C1%29%2C+int%29++%23dummy+variable%0A++++%0A++++%23compute+the+bitwise+xor+matrix%0A++++M1+%3D+bitxormatrix%28genl1%29%0A++++M2+%3D+np.triu%28bitxormatrix%28genl2%29%2C1%29+%0A%0A++++for+i+in+range%28m-1%29%3A%0A++++++++for+j+in+range%28i%2B1%2C+m%29%3A%0A++++++++++++%5Br%2Cc%5D+%3D+np.where%28M2+%3D%3D+M1%5Bi%2Cj%5D%29%0A++++++++++++for+k+in+range%28len%28r%29%29%3A%0A++++++++++++++++VT%5B%28i%29%2An+%2B+r%5Bk%5D%5D+%3D+1%3B%0A++++++++++++++++VT%5B%28i%29%2An+%2B+c%5Bk%5D%5D+%3D+1%3B%0A++++++++++++++++VT%5B%28j%29%2An+%2B+r%5Bk%5D%5D+%3D+1%3B%0A++++++++++++++++VT%5B%28j%29%2An+%2B+c%5Bk%5D%5D+%3D+1%3B%0A++++++++++++++++%0A++++++++++++++++if+M+is+None%3A%0A++++++++++++++++++++M+%3D+np.copy%28VT%29%0A++++++++++++++++else%3A%0A++++++++++++++++++++M+%3D+np.concatenate%28%28M%2C+VT%29%2C+1%29%0A++++++++++++++++%0A++++++++++++++++VT+%3D+np.zeros%28%28n%2Am%2C1%29%2C+int%29%0A++++%0A++++return+M%0A%5Cend%7Bminted%7D%0A%5Cend%7Bdocument%7D)

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

![Ví dụ áp dụng định dạng cho mã đã dàn trang do gói minted tạo ra](/files/972c7d5c670f30780ee4af5faf0e0947ac460d2d)

Các tham số dùng trong ví dụ này là:

* `frame=lines`: vẽ hai đường, một ở trên và một ở dưới mã để tạo khung. Các giá trị khác có thể là `leftline`, `topline`, `bottomline` và `single`.
* `framesep=2mm`: khoảng cách của khung được đặt là 2mm. Các [đơn vị độ dài](/latex/vi/dinh-dang/01-lengths-in-latex.md) khác cũng có thể được sử dụng.
* `baselinestretch=1.2`: khoảng cách dòng của mã được đặt là 1.2.
* `bgcolor=LightGray`: màu nền được đặt thành `LightGray`. Bạn cần nhập gói `xcolor` để điều này hoạt động. Xem [Sử dụng màu trong LaTeX](/latex/vi/dinh-dang/13-using-colors-in-latex.md) để tìm hiểu thêm về thao tác màu.
* `fontsize=\footnotesize`: cỡ chữ được đặt thành `footnotesize`. Bất kỳ [cỡ chữ](/latex/vi/phong-chu/01-font-sizes-families-and-styles.md#reference-guide) nào khác cũng có thể được đặt.
* `linenos`: bật số dòng.

Các tùy chọn khác có thể hữu ích là:

* `mathescape`: bật chế độ toán trong chú thích mã.
* `rulecolor`: thay đổi màu của khung.
* `showspaces`: bật một ký tự đặc biệt để làm hiện khoảng trắng.

## Bao gồm mã từ một tệp

Mã thường được lưu trong một tệp nguồn, vì vậy một lệnh tự động nhập mã từ tệp là rất tiện lợi, như được minh họa trong ví dụ sau:

```latex
\documentclass{article}
\usepackage{minted}
\title{Nhập tệp bằng minted}
\begin{document}
Mã tiếp theo sẽ được nhập trực tiếp từ một tệp:

\inputminted{octave}{BitXorMatrix.m}
\end{document}
```

[Mở ví dụ này trong Overleaf](https://www.overleaf.com/docs?engine=\&snip_name\[]=main.tex\&snip\[]=%5Cdocumentclass%7Barticle%7D%0A%5Cusepackage%7Bminted%7D%0A%5Ctitle%7BImporting+files+using+minted%7D%0A%5Cbegin%7Bdocument%7D%0AThe+next+code+will+be+directly+imported+from+a+file%3A%0A%0A%5Cinputminted%7Boctave%7D%7BBitXorMatrix.m%7D%0A%5Cend%7Bdocument%7D\&snip_name\[]=BitXorMatrix.m\&snip\[]=function+X+%3D+BitXorMatrix%28A%2CB%29%0A%25function+to+compute+the+sum+without+charge+of+two+vectors%0A%09%0A%09%25convert+elements+into+usigned+integers%0A%09A+%3D+uint8%28A%29%3B%0A%09B+%3D+uint8%28B%29%3B%0A%0A%09m1+%3D+length%28A%29%3B%0A%09m2+%3D+length%28B%29%3B%0A%09X+%3D+uint8%28zeros%28m1%2C+m2%29%29%3B%0A%09for+n1%3D1%3Am1%0A%09%09for+n2%3D1%3Am2%0A%09%09%09X%28n1%2C+n2%29+%3D+bitxor%28A%28n1%29%2C+B%28n2%29%29%3B%0A%09%09end%0A%09end\&main_document=main.tex)

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

![Dùng minted để nhập một tệp mã](/files/24742e418325b4a05898155cedb2e61d7f1fc6d2)

Lệnh `\inputminted{octave}{BitXorMatrix.m}` nhập mã từ tệp `BitXorMatrix.m`, tham số `octave` cho LaTeX biết ngôn ngữ lập trình của mã. Lệnh này có thể nhận thêm hai tham số để chỉ nhập một phần của tệp; ví dụ, để nhập mã từ dòng 2 đến dòng 12, lệnh sẽ trở thành:

```latex
\inputminted[firstline=2, lastline=12]{octave}{BitXorMatrix.m}
```

## Mã một dòng

Nếu bạn chỉ cần nhập một dòng mã, lệnh `\mint`, với cú pháp được trình bày trong ví dụ tiếp theo, sẽ đáp ứng được nhu cầu.

```latex
Định dạng mã một dòng cũng hoạt động với \texttt{minted}. Ví dụ, một đoạn HTML nhỏ như sau:
\mint{html}|<h2>Điều gì đó <b>ở đây</b></h2>|
\noindent có thể được định dạng chính xác.
```

[Mở ví dụ này trong Overleaf](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=One+line+code+example+with+minted\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cusepackage%7Bminted%7D%0A%5Cbegin%7Bdocument%7D%0AOne-line+code+formatting+also+works+with+%5Ctexttt%7Bminted%7D.+For+example%2C+a+small+fragment+of+HTML+like+this%3A%0A%5Cmint%7Bhtml%7D%7C%3Ch2%3ESomething+%3Cb%3Ehere%3C%2Fb%3E%3C%2Fh2%3E%7C%0A%5Cnoindent+can+be+formatted+correctly.%0A%5Cend%7Bdocument%7D)

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

![Ví dụ mã một dòng với minted](/files/a955f9c5db2655ed53590307b96c97be40d49446)

Tham số nằm giữa dấu ngoặc nhọn đặt ngôn ngữ lập trình (`html` là ngôn ngữ đánh dấu trong trường hợp này) với văn bản thực tế cần định dạng được phân tách bởi ký tự '|'.

## Lexer tùy chỉnh

**(Xin lưu ý rằng do các thay đổi trong `minted` kể từ năm 2023, cách tiếp cận sau chỉ hoạt động trong** [**TeX Live 2022 hoặc cũ hơn**](/latex/vi/co-so-tri-thuc/148-using-the-overleaf-project-menu.md#tex-live-version)**.)**

Theo mặc định, `minted` chỉ hỗ trợ các ngôn ngữ có lexer đã được cài đặt hoặc được đăng ký với `pygmentize`. Nếu bạn đã viết một lexer tùy chỉnh, hoặc muốn dùng một lexer cho ngôn ngữ chưa được cài đặt trên Overleaf, bạn vẫn có thể sử dụng nó trong dự án Overleaf của riêng mình bằng cách dùng phương pháp đã nêu [ở đây](https://tex.stackexchange.com/questions/18083/how-to-add-custom-c-keywords-to-be-recognized-by-minted#comment930474_42392).

Giả sử bạn đã định nghĩa một lexer trong tệp `nl-lexer.py`, chứa lớp `NetLogoLexer` cho ngôn ngữ NetLogo. Tải lên `nl-lexer.py` vào dự án Overleaf của bạn, rồi chỉ định `nl-lexer.py:NetLogoLexer` làm "tên ngôn ngữ" khi sử dụng `minted`. Ví dụ:

```latex
\begin{minted}{nl-lexer.py:NetLogoLexer -x}
   ... mã của bạn ở đây ...
\end{minted}
```

[Đây là](http://quantixed.org/2018/10/23/new-lexicon-how-to-add-a-custom-minted-lexer-in-overleaf/) một ví dụ khác cho ngôn ngữ ImageJ Macro.

## Màu sắc và bảng kiểu

Các bảng màu dùng cho tô sáng mã được lưu trong các stylesheet. Bạn có thể tự tạo hoặc dùng một bảng đã có sẵn trong bản phân phối LaTeX của mình. Xem [hướng dẫn tham khảo](#reference-guide) để xem danh sách các stylesheet đi kèm trong [Overleaf](https://www.overleaf.com).

```latex
\documentclass{article}
\usepackage{minted}
\usemintedstyle{borland}
\begin{document}
\begin{minted}{python}
import numpy as np

def incmatrix(genl1,genl2):
    m = len(genl1)
    n = len(genl2)
    M = None #để trở thành ma trận liên thuộc
    VT = np.zeros((n*m,1), int)  #biến giả

    #tính ma trận XOR theo từng bit
    M1 = bitxormatrix(genl1)
    M2 = np.triu(bitxormatrix(genl2),1)

    for i in range(m-1):
        for j in range(i+1, m):
            [r,c] = np.where(M2 == M1[i,j])
            for k in range(len(r)):
                VT[(i)*n + r[k]] = 1;
                VT[(i)*n + c[k]] = 1;
                VT[(j)*n + r[k]] = 1;
                VT[(j)*n + c[k]] = 1;

                if M is None:
                    M = np.copy(VT)
                else:
                    M = np.concatenate((M, VT), 1)

                VT = np.zeros((n*m,1), int)

    return M
\end{minted}
\end{document}
```

[Mở ví dụ này trong Overleaf](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Using+stylesheets+with+minted+package\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cusepackage%7Bminted%7D%0A%5Cusemintedstyle%7Bborland%7D%0A%5Cbegin%7Bdocument%7D%0A%5Cbegin%7Bminted%7D%7Bpython%7D%0Aimport+numpy+as+np%0A++++%0Adef+incmatrix%28genl1%2Cgenl2%29%3A%0A++++m+%3D+len%28genl1%29%0A++++n+%3D+len%28genl2%29%0A++++M+%3D+None+%23to+become+the+incidence+matrix%0A++++VT+%3D+np.zeros%28%28n%2Am%2C1%29%2C+int%29++%23dummy+variable%0A++++%0A++++%23compute+the+bitwise+xor+matrix%0A++++M1+%3D+bitxormatrix%28genl1%29%0A++++M2+%3D+np.triu%28bitxormatrix%28genl2%29%2C1%29+%0A%0A++++for+i+in+range%28m-1%29%3A%0A++++++++for+j+in+range%28i%2B1%2C+m%29%3A%0A++++++++++++%5Br%2Cc%5D+%3D+np.where%28M2+%3D%3D+M1%5Bi%2Cj%5D%29%0A++++++++++++for+k+in+range%28len%28r%29%29%3A%0A++++++++++++++++VT%5B%28i%29%2An+%2B+r%5Bk%5D%5D+%3D+1%3B%0A++++++++++++++++VT%5B%28i%29%2An+%2B+c%5Bk%5D%5D+%3D+1%3B%0A++++++++++++++++VT%5B%28j%29%2An+%2B+r%5Bk%5D%5D+%3D+1%3B%0A++++++++++++++++VT%5B%28j%29%2An+%2B+c%5Bk%5D%5D+%3D+1%3B%0A++++++++++++++++%0A++++++++++++++++if+M+is+None%3A%0A++++++++++++++++++++M+%3D+np.copy%28VT%29%0A++++++++++++++++else%3A%0A++++++++++++++++++++M+%3D+np.concatenate%28%28M%2C+VT%29%2C+1%29%0A++++++++++++++++%0A++++++++++++++++VT+%3D+np.zeros%28%28n%2Am%2C1%29%2C+int%29%0A++++%0A++++return+M%0A%5Cend%7Bminted%7D%0A%5Cend%7Bdocument%7D)

Sử dụng `borland` stylesheet cho ra kết quả sau:

![Kết quả của gói minted khi sử dụng stylesheet borland](/files/fcf92f0f16b4b8ef6da85057b38826dae7eb24a1)

Cú pháp để đặt một kiểu tô màu rất đơn giản, lệnh `\usemintedstyle{borland}` sử dụng chủ đề màu `borland` để định dạng mã nguồn. Bạn có thể tìm thêm nhiều bảng màu hơn trong [hướng dẫn tham khảo](#reference-guide).

## Chú thích, nhãn và danh sách liệt kê

Các đoạn mã được định dạng với **minted** có thể được đưa vào một phần tử nổi, giống như [hình](/latex/vi/chu-de-khac/27-inserting-images.md#positioning) và [bảng](/latex/vi/hinh-va-bang/01-tables.md#positioning-tables). Có thể gán chú thích và nhãn cho các đoạn mã, rồi sau đó được tham chiếu và đưa vào "Danh sách liệt kê".

```latex
\documentclass{article}
\usepackage{minted}
\title{Liệt kê các ví dụ mã}
\begin{document}
\begin{listing}[!ht]
\inputminted{octave}{BitXorMatrix.m}
\caption{Ví dụ từ tệp bên ngoài}
\label{listing:1}
\end{listing}

\begin{listing}[!ht]
\begin{minted}{c}
#include <stdio.h>
int main() {
   printf("Xin chào, Thế giới!"); /*printf() xuất ra chuỗi được đặt trong dấu ngoặc kép*/
   return 0;
}
\end{minted}
\caption{Xin chào, Thế giới! trong C}
\label{listing:2}
\end{listing}

\begin{listing}[!ht]
\begin{minted}{lua}
function fact (n)--định nghĩa một hàm giai thừa
  if n == 0 then
    return 1
  else
    return n * fact(n-1)
  end
end

print("nhập một số:")
a = io.read("*number") -- đọc một số
print(fact(a))
\end{minted}
\caption{Ví dụ từ tài liệu hướng dẫn Lua}
\label{listing:3}
\end{listing}
\noindent\texttt{minted} dàn trang các đoạn liệt kê \ref{listing:1}, \ref{listing:2} và \ref{listing:3} rất tốt.
\renewcommand\listoflistingscaption{Danh sách mã nguồn}
\listoflistings
\end{document}
```

[Mở ví dụ này trong Overleaf](https://www.overleaf.com/docs?engine=\&snip_name\[]=main.tex\&snip\[]=%5Cdocumentclass%7Barticle%7D%0A%5Cusepackage%7Bminted%7D%0A%5Ctitle%7BListing+code+examples%7D%0A%5Cbegin%7Bdocument%7D%0A%5Cbegin%7Blisting%7D%5B%21ht%5D%0A%5Cinputminted%7Boctave%7D%7BBitXorMatrix.m%7D%0A%5Ccaption%7BExample+from+external+file%7D%0A%5Clabel%7Blisting%3A1%7D%0A%5Cend%7Blisting%7D%0A%0A%5Cbegin%7Blisting%7D%5B%21ht%5D%0A%5Cbegin%7Bminted%7D%7Bc%7D%0A%23include+%3Cstdio.h%3E%0Aint+main%28%29+%7B%0A+++printf%28%22Hello%2C+World%21%22%29%3B+%2F%2Aprintf%28%29+outputs+the+quoted+string%2A%2F%0A+++return+0%3B%0A%7D%0A%5Cend%7Bminted%7D%0A%5Ccaption%7BHello+World+in+C%7D%0A%5Clabel%7Blisting%3A2%7D%0A%5Cend%7Blisting%7D%0A%0A%5Cbegin%7Blisting%7D%5B%21ht%5D%0A%5Cbegin%7Bminted%7D%7Blua%7D%0Afunction+fact+%28n%29--defines+a+factorial+function%0A++if+n+%3D%3D+0+then%0A++++return+1%0A++else%0A++++return+n+%2A+fact%28n-1%29%0A++end%0Aend%0A%0Aprint%28%22enter+a+number%3A%22%29%0Aa+%3D+io.read%28%22%2Anumber%22%29+--+read+a+number%0Aprint%28fact%28a%29%29%0A%5Cend%7Bminted%7D%0A%5Ccaption%7BExample+from+the+Lua+manual%7D%0A%5Clabel%7Blisting%3A3%7D%0A%5Cend%7Blisting%7D%0A%5Cnoindent%5Ctexttt%7Bminted%7D+makes+a+nice+job+of+typesetting+listings+%5Cref%7Blisting%3A1%7D%2C+%5Cref%7Blisting%3A2%7D+and+%5Cref%7Blisting%3A3%7D.%0A%5Crenewcommand%5Clistoflistingscaption%7BList+of+source+codes%7D%0A%5Clistoflistings%0A%5Cend%7Bdocument%7D\&snip_name\[]=BitXorMatrix.m\&snip\[]=%25This+code+is+directly+imported+from+a+file%0A%25A+function+to+compute+the+sum+without+charge+of+two+vectors%0Afunction+X+%3D+BitXorMatrix%28A%2CB%29%0A%25convert+elements+into+unsigned+integers%0AA+%3D+uint8%28A%29%3B%0AB+%3D+uint8%28B%29%3B%0A%0Am1+%3D+length%28A%29%3B%0Am2+%3D+length%28B%29%3B%0AX+%3D+uint8%28zeros%28m1%2C+m2%29%29%3B%0A+for+n1%3D1%3Am1%0A++for+n2%3D1%3Am2%0A+++X%28n1%2C+n2%29+%3D+bitxor%28A%28n1%29%2C+B%28n2%29%29%3B%0A++end%0Aend\&main_document=main.tex)

Trang đầu tiên của ví dụ này chứa kết quả sau:

![Ví dụ các đoạn mã liệt kê](/files/baa7ef26d7ff466045f5bc5eee6377e4bc8c14a3)

Để in danh sách với tất cả các phần tử "listing", hãy dùng `\listoflistings`. Trong ví dụ trên, tiêu đề mặc định `Danh sách liệt kê` được đổi thành `Danh sách mã nguồn` bằng cách viết

```latex
\renewcommand\listoflistingscaption{Danh sách mã nguồn}
\listoflistings % Bây giờ dàn trang danh sách
```

Trang thứ hai được tạo ra bởi ví dụ trên chứa đoạn liệt kê sau:

![Một ví dụ về liệt kê mã nguồn](/files/aa68dc21846e006e5cbc98295d493b6773709853)

## Hướng dẫn tham khảo

**Các kiểu màu cho minted**

| tên      | kết quả                                                               | tên      | kết quả                                                                |
| -------- | --------------------------------------------------------------------- | -------- | ---------------------------------------------------------------------- |
| manni    | ![MintedStyles1.png](/files/309f084b1a7bd7e5f31bf0e65ba27f5a9a1a0974) | fruity   | ![MintedStyles10.png](/files/72b0c9b1b205c95bd6ef956df28472f24e4d0a83) |
| rrt      | ![MintedStyles2.png](/files/719102bba58e8d9a1a99fd8343d6086de1090d4a) | autumn   | ![MintedStyles11.png](/files/c9f8fca500d67a04ee619df1d60b5cd2c07359d4) |
| perldoc  | ![MintedStyles3.png](/files/e68a69ae86b58b815320dc86af08f0821f64f8d8) | bw       | ![MintedStyles12.png](/files/7151c6ac06e0773909be7e7e88b909da2bedee56) |
| borland  | ![MintedStyles4.png](/files/06b4b2033e8faac63a139df2c77a9c987a9613af) | emacs    | ![MintedStyles13.png](/files/f8b8175857ed78cec56469eff6fd8919017279d0) |
| colorful | ![MintedStyles5.png](/files/59827212a39d8cb6e200827fb445a49a99009088) | vim      | ![MintedStyles14.png](/files/31b9de7546f04d8c3f4af51178283c802a95ea53) |
| murphy   | ![MintedStyles6.png](/files/b09c51d52bfe40951c398b5352d87f4119cdb8c8) | pastie   | ![MintedStyles15.png](/files/7fb1f5a6231848775e92cbc748a104ef920067a3) |
| vs       | ![MintedStyles7.png](/files/bf86431d0b4f88420fdfd3415d146d90a33f603c) | friendly | ![MintedStyles16.png](/files/357c97e3e3655dc00494a24ca9f282c8f5e2523e) |
| trac     | ![MintedStyles8.png](/files/22164bfa6690d40e207c5cd11c9af36e7024214c) | native   | ![MintedStyles17.png](/files/8f45c3f892b1d36308e340a1a4eea5cb0766715b) |
| tango    | ![MintedStyles9.png](/files/5c9222d44f96c88ca6f96d1d06d4eb57c8713341) | monokai  | ![MintedStyles18.png](/files/a99a578e8e46dd15d10194f91546504fe92c1463) |

Một số bảng màu cần nền tối để có thể đọc được.

**Các ngôn ngữ lập trình và tệp cấu hình được hỗ trợ chính**

|          |            |             |           |
| -------- | ---------- | ----------- | --------- |
| cucumber | abap       | ada         | ahk       |
| antlr    | apacheconf | applescript | as        |
| aspectj  | autoit     | asy         | awk       |
| basemake | bash       | bat         | bbcode    |
| befunge  | bmax       | boo         | brainfuck |
| bro      | bugs       | c           | ceylon    |
| cfm      | cfs        | cheetah     | clj       |
| cmake    | cobol      | cl          | console   |
| control  | coq        | cpp         | croc      |
| csharp   | css        | cuda        | cyx       |
| d        | dg         | diff        | django    |
| dpatch   | duel       | dylan       | ec        |
| erb      | evoque     | fan         | fancy     |
| fortran  | gas        | genshi      | glsl      |
| gnuplot  | go         | gosu        | groovy    |
| gst      | haml       | haskell     | hxml      |
| html     | http       | hx          | idl       |
| irc      | ini        | java        | jade      |
| js       | json       | jsp         | kconfig   |
| koka     | lasso      | livescrit   | llvm      |
| logos    | lua        | mako        | mason     |
| matlab   | minid      | monkey      | moon      |
| mxml     | myghty     | mysql       | nasm      |
| newlisp  | newspeak   | numpy       | ocaml     |
| octave   | ooc        | perl        | php       |
| plpgsql  | postgresql | postscript  | pot       |
| prolog   | psql       | puppet      | python    |
| qml      | ragel      | raw         | ruby      |
| rhtml    | sass       | scheme      | smalltalk |
| sql      | ssp        | tcl         | tea       |
| tex      | text       | vala        | vgl       |
| vim      | xml        | xquery      | yaml      |

## Đọc thêm

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

* [Độ dài trong LaTeX](/latex/vi/dinh-dang/01-lengths-in-latex.md)
* [Liệt kê mã](/latex/vi/dinh-dang/11-code-listing.md)
* [Bộ đếm](/latex/vi/dinh-dang/10-counters.md)
* [Sử dụng màu trong LaTeX](/latex/vi/dinh-dang/13-using-colors-in-latex.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)
* [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)
* [Mục lục](/latex/vi/cau-truc-tai-lieu/02-table-of-contents.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)
* [Tính năng **minted** tài liệu gói](https://texdoc.org/serve/minted/0)


---

# 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/dinh-dang/12-code-highlighting-with-minted.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.
