> 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/ja/to-2/16-how-can-i-include-matlab-code-in-my-latex-document.md).

# LaTeX文書にMATLABコードを含めるにはどうすればよいですか？

この [`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 コード...
\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` パラメーターを使って変更でき、3 つの値をサポートします:

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

以下の例では、これらのスタイルの使い方を示します。

### 例

以下は、MATLAB コードを用いた例です。 `matlab-prettifier`’s ドキュメントで、以下の `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
%{
row vector x を作成し、それを逆順にする
%}
x = linspace(0,1,101);
y = x(end:-1:1);
\end{lstlisting}
\end{document}
```

&#x20;[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/961897b1d0f37e98291bf3da578d1d5ed00551ac)

次の例では、 `frame` や `numbers` パラメーターに `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
%{
row vector x を作成し、それを逆順にする
%}
x = linspace(0,1,101);
y = x(end:-1:1);
\end{lstlisting}
\end{document}
```

&#x20;[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/4a4ad708137fba953f4a4968dbc051136cd1a7d1)

### ファイルに保存された MATLAB コードの使用

MATLAB コードを LaTeX 文書に直接含める代わりに、 `\lstinputlisting` コマンドを使用して、外部ファイルに含まれるコードをインポートし、整形組版します:

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

ここで

* `<options>` は、キーと値のペアとして提供されるコンマ区切りのパラメーター一覧です [（上記参照）](#matopts);
* `<filename>` は MATLAB コードを含むファイルへのパスです。

たとえば、ファイル `sample.m` をインポートし、 `Matlab-bw` style で整形組版するには、次のように書きます

```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
%{
row vector x を作成し、それを逆順にする
%}
x = linspace(0,1,101);
y = x(end:-1:1);
```

&#x20;[この例を 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/f0c104c37ff7c2152d41ae2cf8d5848f023f4ee0)


---

# 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/ja/to-2/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.
