> 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-hoi-and-tra-loi/16-how-can-i-include-matlab-code-in-my-latex-document.md).

# Làm thế nào để tôi chèn mã MATLAB vào tài liệu LaTeX của mình?

Tính năng [`matlab-prettifier` gói](https://www.ctan.org/pkg/matlab-prettifier) có thể được dùng để dàn trang mã MATLAB và được viết để đáp lại một [câu hỏi trên tex.stackexchange](https://tex.stackexchange.com/questions/75116/what-can-i-use-to-typeset-matlab-code-in-my-document).

### Môi trường lstlisting

Sau khi bao gồm `matlab-prettifier` gói trong tài liệu của bạn thông qua

```latex
\usepackage{matlab-prettifier}
```

bạn có thể sử dụng `lstlisting` môi trường để dàn trang mã MATLAB:

```latex
\begin{lstlisting}[<options>]
Mã MATLAB...
\end{lstlisting}
```

trong đó `<options>` là một danh sách các tham số được phân tách bằng dấu phẩy, trong đó các giá trị được đặt bằng các cặp khóa–giá trị (tức là, `parameter=value`).

* **Lưu ý**: Gói `matlab-prettifier` được xây dựng dựa trên `listings` gói, cho phép sử dụng các tính năng do `listings`, bao gồm các tham số trong `<options>` của `lstlisting` môi trường. Tuy nhiên, một số `listings` tham số nên *không* được sử dụng, và người đọc được khuyên tham khảo [`matlab-prettifier` tài liệu](http://mirrors.ctan.org/macros/latex/contrib/matlab-prettifier/matlab-prettifier.pdf) để biết chi tiết thêm.

Hình thức hiển thị trực quan của mã MATLAB được dàn trang có thể được thay đổi bằng `style` tham số, hỗ trợ ba giá trị:

* `Matlab-editor`
* `Matlab-bw`
* `Matlab-Pyglike`

Các ví dụ sau minh họa cách sử dụng những kiểu đó.

### Ví dụ

Sau đây là một ví dụ, sử dụng mã MATLAB từ `matlab-prettifier`trong tài liệu của nó, áp dụng `Matlab-editor` tham số style:

```latex
\documentclass[a4paper]{article}
\usepackage{matlab-prettifier}
\begin{document}

\begin{lstlisting}[style=Matlab-editor]
%% Mã MATLAB mẫu
!mv test.txt test2.txt
A = [1, 2, 3;... foo
     4, 5, 6];
s = 'abcd';
for k = 1:4
  Disp(s(k)) % bar
end
%{
tạo vectơ hàng x, sau đó đảo ngược nó
%}
x = linspace(0,1,101);
y = x(end:-1:1);
\end{lstlisting}
\end{document}
```

[Mở ví dụ này trong Overleaf](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=A+matlab-prettifier+example\&snip=%5Cdocumentclass%5Ba4paper%5D%7Barticle%7D%0A%5Cusepackage%7Bmatlab-prettifier%7D%0A%5Cbegin%7Bdocument%7D%0A%0A%5Cbegin%7Blstlisting%7D%5Bstyle%3DMatlab-editor%5D%0A%25%25+Sample+Matlab+code%0A%21mv+test.txt+test2.txt%0AA+%3D+%5B1%2C+2%2C+3%3B...+foo%0A+++++4%2C+5%2C+6%5D%3B%0As+%3D+%27abcd%27%3B%0Afor+k+%3D+1%3A4%0A++Disp%28s%28k%29%29+%25+bar%0Aend%0A%25%7B%0Acreate+row+vector+x%2C+then+reverse+it%0A%25%7D%0Ax+%3D+linspace%280%2C1%2C101%29%3B%0Ay+%3D+x%28end%3A-1%3A1%29%3B%0A%5Cend%7Blstlisting%7D%0A%5Cend%7Bdocument%7D)

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

![Mã MATLAB dàn trang bằng LaTeX trên Overleaf](/files/564818b31a1b6643b5797381deb55fef6e7eedda)

Ví dụ tiếp theo đặt giá trị cho `khung` và `số dòng` tham số do `listings` gói:

```latex
\documentclass[a4paper]{article}
\usepackage{matlab-prettifier}
\begin{document}

\begin{lstlisting}[
frame=single,
numbers=left,
style=Matlab-Pyglike]
%% Mã MATLAB mẫu
!mv test.txt test2.txt
A = [1, 2, 3;... foo
     4, 5, 6];
s = 'abcd';
for k = 1:4
  Disp(s(k)) % bar
end
%{
tạo vectơ hàng x, sau đó đảo ngược nó
%}
x = linspace(0,1,101);
y = x(end:-1:1);
\end{lstlisting}
\end{document}
```

[Mở ví dụ này trong Overleaf](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=A+second+matlab-prettifier+example\&snip=%5Cdocumentclass%5Ba4paper%5D%7Barticle%7D%0A%5Cusepackage%7Bmatlab-prettifier%7D%0A%5Cbegin%7Bdocument%7D%0A%0A%5Cbegin%7Blstlisting%7D%5B%0Aframe%3Dsingle%2C%0Anumbers%3Dleft%2C%0Astyle%3DMatlab-Pyglike%5D%0A%25%25+Sample+Matlab+code%0A%21mv+test.txt+test2.txt%0AA+%3D+%5B1%2C+2%2C+3%3B...+foo%0A+++++4%2C+5%2C+6%5D%3B%0As+%3D+%27abcd%27%3B%0Afor+k+%3D+1%3A4%0A++Disp%28s%28k%29%29+%25+bar%0Aend%0A%25%7B%0Acreate+row+vector+x%2C+then+reverse+it%0A%25%7D%0Ax+%3D+linspace%280%2C1%2C101%29%3B%0Ay+%3D+x%28end%3A-1%3A1%29%3B%0A%5Cend%7Blstlisting%7D%0A%5Cend%7Bdocument%7D)

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

![Mã MATLAB dàn trang bằng LaTeX trên Overleaf](/files/e816ba21c4ebf61af8f02aaf924bd3648a538963)

### Sử dụng mã MATLAB được lưu trong một tệp

Thay vì đưa trực tiếp mã MATLAB vào tài liệu LaTeX của bạn, hãy dùng `\lstinputlisting` lệnh để nhập và dàn trang mã nằm trong một tệp bên ngoài:

```latex
\lstinputlisting[<options>]{<filename>}
```

trong đó

* `<options>` là một danh sách tham số được phân tách bằng dấu phẩy, được cung cấp dưới dạng các cặp khóa–giá trị [(xem ở trên)](#matopts);
* `<filename>` là đường dẫn tới một tệp chứa mã MATLAB.

Ví dụ, để nhập tệp `sample.m` và dàn trang nó theo `Matlab-bw` kiểu, bạn sẽ viết

```latex
\lstinputlisting[style=Matlab-bw]{sample.m}
```

Ví dụ tiếp theo đặt một khung quanh mã, đánh số các dòng mã (ở bên trái), và dùng `Matlab-bw` tham số style:

```latex
\lstinputlisting[
frame=single,
numbers=left,
style=Matlab-editor
]{sample.m}
```

Chọn `Mở ví dụ này trong Overleaf` liên kết ([xem bên dưới](#openol)) để tự động tạo một dự án Overleaf bao gồm `sample.m` tệp mã MATLAB.

**`main.tex`**

```latex
\documentclass[a4paper]{article}
\usepackage{matlab-prettifier}
\begin{document}
\title{Importing and typesetting MATLAB code contained in a file}
\lstinputlisting[
frame=single,
numbers=left,
style=Matlab-bw
]{sample.m}

\end{document}
```

**`sample.m`**

```latex
%% Mã MATLAB mẫu
!mv test.txt test2.txt
A = [1, 2, 3;... foo
     4, 5, 6];
s = 'abcd';
for k = 1:4
  Disp(s(k)) % bar
end
%{
tạo vectơ hàng x, sau đó đảo ngược nó
%}
x = linspace(0,1,101);
y = x(end:-1:1);
```

[Mở ví dụ này trong Overleaf (nó sẽ tạo ra `sample.m` tệp).](https://www.overleaf.com/docs?engine=pdflatex\&snip_name\[]=main.tex\&snip\[]=%5Cdocumentclass%5Ba4paper%5D%7Barticle%7D%0A%5Cusepackage%7Bmatlab-prettifier%7D%0A%5Cbegin%7Bdocument%7D%0A%5Ctitle%7BImporting+and+typesetting+MATLAB+code+contained+in+a+file%7D%0A%5Clstinputlisting%5B%0Aframe%3Dsingle%2C%0Anumbers%3Dleft%2C%0Astyle%3DMatlab-bw%0A%5D%7Bsample.m%7D%0A%0A%5Cend%7Bdocument%7D\&snip_name\[]=sample.m\&snip\[]=%25%25+Sample+Matlab+code%0A%21mv+test.txt+test2.txt%0AA+%3D+%5B1%2C+2%2C+3%3B...+foo%0A+++++4%2C+5%2C+6%5D%3B%0As+%3D+%27abcd%27%3B%0Afor+k+%3D+1%3A4%0A++Disp%28s%28k%29%29+%25+bar%0Aend%0A%25%7B%0Acreate+row+vector+x%2C+then+reverse+it%0A%25%7D%0Ax+%3D+linspace%280%2C1%2C101%29%3B%0Ay+%3D+x%28end%3A-1%3A1%29%3B\&main_document=main.tex)

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

![Mã MATLAB dàn trang bằng LaTeX trên Overleaf](/files/9aa93adbb8a17815f19f53918aaff11071e08412)


---

# 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-hoi-and-tra-loi/16-how-can-i-include-matlab-code-in-my-latex-document.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.
