> 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/zh-tw/wen-yu-da/16-how-can-i-include-matlab-code-in-my-latex-document.md).

# 我該如何將 MATLAB 程式碼納入我的 LaTeX 文件？

該 [`matlab-prettifier` 套件](https://www.ctan.org/pkg/matlab-prettifier) 可以用來排版 MATLAB 程式碼，並且是為了回應一個 [在 tex.stackexchange 上的一個問題](https://tex.stackexchange.com/questions/75116/what-can-i-use-to-typeset-matlab-code-in-my-document).

### lstlisting 環境

在文件中加入 `matlab-prettifier` 套件後，透過

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

你就可以使用 `lstlisting` 環境來排版 MATLAB 程式碼：

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

其中 `<options>` 是以逗號分隔的參數清單，其值使用鍵值配對設定（亦即， `參數=值`).

* **注意**： `matlab-prettifier` 套件以 `listings` 套件為基礎，讓你能使用由 `listings`提供的功能，包括 `<options>` 中的參數 `lstlisting` 環境。不過，有些 `listings` 參數應該 *不* 使用，並建議讀者參考 [`matlab-prettifier` 說明文件](http://mirrors.ctan.org/macros/latex/contrib/matlab-prettifier/matlab-prettifier.pdf) 以取得更多詳細資訊。

排版後的 MATLAB 程式碼外觀可以透過 `style` 參數來變更，它支援三個值：

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

以下範例示範如何使用這些樣式。

### 範例

以下是一個範例，使用來自 MATLAB 程式碼 `matlab-prettifier`的文件中的程式碼，套用了 `Matlab-editor` 樣式：

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

\begin{lstlisting}[style=Matlab-editor]
%% 範例 MATLAB 程式碼
!mv test.txt test2.txt
A = [1, 2, 3;... foo
     4, 5, 6];
s = 'abcd';
for k = 1:4
  Disp(s(k)) % bar
end
%{
建立列向量 x，然後將其反轉
%}
x = linspace(0,1,101);
y = x(end:-1:1);
\end{lstlisting}
\end{document}
```

[在 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)

此範例會產生以下輸出：

![在 Overleaf 上以 LaTeX 排版的 MATLAB 程式碼](/files/a79116ace51ce553ba70722459461d717d134ccd)

下一個範例為 `框線` 和 `數字` 參數設定值，這些參數由 `listings` 套件的範例程式碼：

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

\begin{lstlisting}[
frame=single,
numbers=left,
style=Matlab-Pyglike]
%% 範例 MATLAB 程式碼
!mv test.txt test2.txt
A = [1, 2, 3;... foo
     4, 5, 6];
s = 'abcd';
for k = 1:4
  Disp(s(k)) % bar
end
%{
建立列向量 x，然後將其反轉
%}
x = linspace(0,1,101);
y = x(end:-1:1);
\end{lstlisting}
\end{document}
```

[在 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)

此範例會產生以下輸出：

![在 Overleaf 上以 LaTeX 排版的 MATLAB 程式碼](/files/181e5d309222abf092af9f354deff7d1e26337c0)

### 使用儲存在檔案中的 MATLAB 程式碼

不要直接在 LaTeX 文件中加入 MATLAB 程式碼，而是使用 `\lstinputlisting` 命令來匯入並排版外部檔案中的程式碼：

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

其中

* `<options>` 是以逗號分隔的參數清單，以鍵值配對形式提供 [（見上文）](#matopts);
* `<filename>` 是包含 MATLAB 程式碼的檔案路徑。

例如，要匯入檔案 `sample.m` 並以 `Matlab-bw` 樣式排版，你可以寫成

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

下一個範例會在程式碼外加上框線，為程式碼行編號（在左側），並使用 `Matlab-bw` 樣式：

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

選擇 `在 Overleaf 中開啟這個範例` 連結（[見下文](#openol)）來自動建立一個包含 `sample.m` MATLAB 程式碼檔案的 Overleaf 專案。

**`main.tex`**

```latex
\documentclass[a4paper]{article}
\usepackage{matlab-prettifier}
\begin{document}
\title{匯入並排版儲存在檔案中的 MATLAB 程式碼}
\lstinputlisting[
frame=single,
numbers=left,
style=Matlab-bw
]{sample.m}

\end{document}
```

**`sample.m`**

```latex
%% 範例 MATLAB 程式碼
!mv test.txt test2.txt
A = [1, 2, 3;... foo
     4, 5, 6];
s = 'abcd';
for k = 1:4
  Disp(s(k)) % bar
end
%{
建立列向量 x，然後將其反轉
%}
x = linspace(0,1,101);
y = x(end:-1:1);
```

[在 Overleaf 中開啟此範例（它會建立 `sample.m` 檔案，則你可能不需要新增這個）。](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)

此範例會產生以下輸出：

![在 Overleaf 上以 LaTeX 排版的 MATLAB 程式碼](/files/87767cdfd7bf3ee794dc8d43335af2577f18fc12)


---

# 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/zh-tw/wen-yu-da/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.
