> 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/ko/questions-and-answers/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>` 는 값이 키–값 쌍(즉, `parameter=value`).

* **참고**: 이 `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` style:

```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/55be212b8fca163c425ac7166c4001808b907e23)

다음 예시는 다음의 값들을 설정합니다 `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
%{
행 벡터 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/a1052ed1d2c8ad2552d4cd92997d98cb72c46359)

### 파일에 저장된 MATLAB 코드 사용

MATLAB 코드를 LaTeX 문서에 직접 포함하는 대신, 다음을 사용하십시오 `\lstinputlisting` 명령을 사용하여 외부 파일에 포함된 코드를 가져와 조판합니다:

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

여기서

* `<options>` 는 키–값 쌍으로 제공되는 매개변수의 쉼표로 구분된 목록입니다 [(위 참조)](#matopts);
* `<filename>` 는 MATLAB 코드가 들어 있는 파일의 경로입니다.

예를 들어, 다음 파일을 가져오려면 `sample.m` 그리고 이를 다음 `Matlab-bw` 스타일로 조판하려면 다음과 같이 작성합니다

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

다음 예시는 코드 주위에 프레임을 두고, 코드 줄에 번호를 매기며(왼쪽에), 다음을 사용합니다 `Matlab-bw` style:

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

다음을 선택하세요 `이 예제를 Overleaf에서 열기` 링크 ([아래 참조](#openol))를 사용하여 다음을 포함하는 Overleaf 프로젝트를 자동으로 만듭니다 `sample.m` MATLAB 코드 파일.

**`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/aac81103b7f0869ce9f5268f0bf480fa9ca44b12)


---

# 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/ko/questions-and-answers/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.
