> 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/29-how-do-i-include-sections-of-r-code-within-my-latex-document.md).

# LaTeX 문서에 R 코드의 일부를 어떻게 포함하나요?

우리는 다음을 권장합니다. [`listings` 패키지](https://ctan.org/pkg/listings?lang=en) LaTeX 문서에 R 코드를 포함하는 데 사용됩니다. 만약

```latex
\usepackage{listings}
```

를 프리앰블에 추가하면, 다음을 삽입할 수 있습니다. `오` 이 예제에서 보여 주는 것처럼 코드를:

```latex
\documentclass{article}
\usepackage{listings}
\begin{document}
\begin{lstlisting}[language=R]
fib <- function(n) {
  if (n < 2)
    n
  그렇지 않으면
    fib(n - 1) + fib(n - 2)
}
fib(10) # => 55
\end{lstlisting}
\end{document}
```

[이 예제를 Overleaf에서 열기](https://www.overleaf.com/docs?engine=\&snip_name=Listing+R+code+in+a+LaTeX+document\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cusepackage%7Blistings%7D%0A%5Cbegin%7Bdocument%7D%0A%5Cbegin%7Blstlisting%7D%5Blanguage%3DR%5D%0Afib+%3C-+function%28n%29+%7B%0A++if+%28n+%3C+2%29%0A++++n%0A++else%0A++++fib%28n+-+1%29+%2B+fib%28n+-+2%29%0A%7D%0Afib%2810%29+%23+%3D%3E+55%0A%5Cend%7Blstlisting%7D%0A%5Cend%7Bdocument%7D)

이 예제는 다음 출력을 생성합니다:

![Overleaf의 LaTeX에서 조판된 R 코드](/files/1343421aea7cc680b7659b79a873e12461bb4d02)

저희의 [Knitr 사용하기에 관한 글은](/latex/ko/more-topics/29-knitr.md) 도 관심을 가질 만합니다.


---

# 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/29-how-do-i-include-sections-of-r-code-within-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.
