> 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/more-topics/02-algorithms.md).

# 알고리즘

## 소개

*(프로그램 코드 목록을 작성하려면 다음을 참조하세요* [*이 도움말 페이지*](/latex/ko/formatting/11-code-listing.md) *대신.)*

LaTeX에서 알고리즘이나 의사코드를 조판하려면 다음 옵션 중 하나를 사용할 수 있습니다:

* 다음 중 하나를 선택하세요 (`algpseudocode` 또는 `algcompatible` 또는 `algorithmic`) 패키지는 알고리즘 본문을 조판하는 데 사용하고, 그리고 `algorithm` 알고리즘에 캡션을 붙이는 데 사용하는 패키지입니다.
* 다음 `algorithm2e` 패키지.

**위의 패키지 그룹 중 하나만 선택하고, 선택한 패키지가 제공하는 명령과 구문만 사용해야 합니다.** 이 패키지들은 동시에 불러올 수 없습니다. 그렇지 않으면 많은 오류가 발생합니다.

## algpseudocode 및 algorithm 패키지는

다음 `algpseudocode` 패키지는 `algorithmic` 환경과 몇 가지 유용한 명령을 제공합니다. 다음을 할 수 있습니다 [Overleaf에서 전체 예제를 열어보세요](https://www.overleaf.com/project/new/template/20763?id=71202437\&templateName=algpseudocode+%2B+algorithm+example\&latexEngine=pdflatex\&texImage=texlive-full%3A2020.1\&mainFile=) , 그리고 이 섹션에서 몇 가지 세부 사항을 살펴보겠습니다.

다음은 첫 번째 알고리즘으로, 다음의 환경과 명령을 사용합니다: `algpseudocode` 패키지를 불러올 때 추가하세요:

```latex
\documentclass{article}
\usepackage{algpseudocode}
\begin{document}
\begin{algorithmic}
\State $i \gets 10$
\If{$i\geq 5$}
    \State $i \gets i-1$
\Else
    \If{$i\leq 3$}
        \State $i \gets i+2$
    \EndIf
\EndIf
\end{algorithmic}

\end{document}
```

[이 algpseudocode 짧은 예제를 Overleaf에서 열어보세요](https://www.overleaf.com/docs?engine=\&snip_name=algpseudocode+short+example\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cusepackage%7Balgpseudocode%7D%0A%5Cbegin%7Bdocument%7D%0A%5Cbegin%7Balgorithmic%7D%0A%5CState+%24i+%5Cgets+10%24%0A%5CIf%7B%24i%5Cgeq+5%24%7D+%0A++++%5CState+%24i+%5Cgets+i-1%24%0A%5CElse%0A++++%5CIf%7B%24i%5Cleq+3%24%7D%0A++++++++%5CState+%24i+%5Cgets+i%2B2%24%0A++++%5CEndIf%0A%5CEndIf+%0A%5Cend%7Balgorithmic%7D%0A%0A%5Cend%7Bdocument%7D)

결과는 다음과 같습니다:

![Algpseudocode-0.png](/files/ef74bbac93723a3e85d46898ce87f594274bc8dc)

*다음을 불러오면 안 됩니다: `algorithm2e`, `algcompatible`, `algorithmic` 이미 불러온 경우 패키지를 `algpseudocode`.*

다음이 제공하는 명령 이름은 일반적으로 제목식 대문자 표기입니다. 예: `algpseudocode` 은 일반적으로 제목식 대문자 표기입니다. 예를 들어 `\State`, `\While`, `\EndWhile`.

알고리즘에 줄 번호를 추가하려면, 첫 번째 줄 번호를 다음에 추가할 수 있습니다: `algorithmic` 환경을 다음처럼: `\begin{algorithmic}[1]` 그러면 다음과 같은 출력이 나옵니다:

![Algpseudocode-00.png](/files/0e66ca741899745ac75e6c97be6e23aa9ea96114)

위의 알고리즘 예제에는 캡션도 번호도 없습니다. 캡션이 있는 알고리즘이 필요하다면, 다음도 불러와야 합니다: `algorithm` 패키지를 불러오고, 다음을 추가하세요:

```latex
\begin{algorithm}
\caption{...}
...
\end{algorithm}
```

주위에 `algorithmic` 환경을 둘러싸야 합니다. 다음을 사용할 수 있습니다: `\label{...}` 다음 뒤에 `\caption{...}`, 그래서 알고리즘 번호를 다음으로 상호 참조할 수 있습니다: `\ref{...}`.

```latex
\documentclass{article}
\usepackage{algorithm}
\usepackage{algpseudocode}
\begin{document}
\begin{algorithm}
\caption{An algorithm with caption}\label{alg:cap}
\begin{algorithmic}
\Require $n \geq 0$
\Ensure $y = x^n$
\State $y \gets 1$
\State $X \gets x$
\State $N \gets n$
\While{$N \neq 0$}
\If{$N$ is even}
    \State $X \gets X \times X$
    \State $N \gets \frac{N}{2}$  \Comment{이것은 주석입니다}
\ElsIf{$N$ is odd}
    \State $y \gets y \times X$
    \State $N \gets N - 1$
\EndIf
\EndWhile
\end{algorithmic}
\end{algorithm}

\end{document}
```

[이 algorithm+algpseudocode 짧은 예제를 Overleaf에서 열어보세요](https://www.overleaf.com/docs?engine=\&snip_name=Captioned+algorithm%2Balgpseudocode+example\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cusepackage%7Balgorithm%7D%0A%5Cusepackage%7Balgpseudocode%7D%0A%5Cbegin%7Bdocument%7D%0A%5Cbegin%7Balgorithm%7D%0A%5Ccaption%7BAn+algorithm+with+caption%7D%5Clabel%7Balg%3Acap%7D%0A%5Cbegin%7Balgorithmic%7D%0A%5CRequire+%24n+%5Cgeq+0%24%0A%5CEnsure+%24y+%3D+x%5En%24%0A%5CState+%24y+%5Cgets+1%24%0A%5CState+%24X+%5Cgets+x%24%0A%5CState+%24N+%5Cgets+n%24%0A%5CWhile%7B%24N+%5Cneq+0%24%7D%0A%5CIf%7B%24N%24+is+even%7D%0A++++%5CState+%24X+%5Cgets+X+%5Ctimes+X%24%0A++++%5CState+%24N+%5Cgets+%5Cfrac%7BN%7D%7B2%7D%24++%5CComment%7BThis+is+a+comment%7D%0A%5CElsIf%7B%24N%24+is+odd%7D%0A++++%5CState+%24y+%5Cgets+y+%5Ctimes+X%24%0A++++%5CState+%24N+%5Cgets+N+-+1%24%0A%5CEndIf%0A%5CEndWhile%0A%5Cend%7Balgorithmic%7D%0A%5Cend%7Balgorithm%7D%0A%0A%5Cend%7Bdocument%7D)

![Algpseudocode-1.png](/files/cfb6ebc04677211d0aa7223388824a57eff7b500)

다음 `algorithm` 환경은 [float](/latex/ko/figures-and-tables/02-positioning-images-and-tables.md) 다음과 같은 `table` 및 `figure`, 따라서 부동체 배치 수식자를 추가할 수 있습니다 `[hbt!]` 뒤에 `\begin{algorithm}` 필요한 경우입니다. 이는 또한 긴 `algorithmic` 환경 자체는 페이지를 넘어 나눌 수 있지만, 한 `algorithm` 환경은 그렇지 않습니다.

다음 `algorithm` 패키지는 또한 `\listofalgorithms` 다음과 같이 작동하는 명령을 제공합니다: `\listoffigures`, 하지만 캡션이 있는 알고리즘용입니다. 예를 들면 이렇습니다.

![Algpseudocode-2.png](/files/ff3dba7983a316d813b614f310bd1aabd8149125)

[Overleaf에서 전체 예제를 열어보세요](https://www.overleaf.com/project/new/template/20763?id=71202437\&templateName=algpseudocode+%2B+algorithm+example\&latexEngine=pdflatex\&texImage=texlive-full%3A2020.1\&mainFile=)

## algcompatible/algorithmic 및 algorithm 패키지는

다음 `algorithmic` 패키지는 다음과 유사한 구문을 사용합니다: `algpseudocode`; **하지만** 명령 이름이 모두 대문자로 표기됩니다. 예: `\STATE`, `\WHILE`, `\ENDWHILE`.

반면에, `algcompatible` 대문자 표기 및 제목식 대문자 표기 명령 이름을 인식하므로 `\STATE`, `\WHILE`, `\ENDWHILE`, `\State`, `\While`, `\EndWhile` 모두 인식됩니다. 명령 이름을 제외하면, `algcompatible` 및 `algorithmic` 명령은 다음과 같은 인자 구문을 사용합니다: `algpseudocode`.

```latex
\documentclass{article}
\usepackage{algcompatible}
% 또는 \usepackage{algorithmic}
\begin{document}
\begin{algorithmic}
\STATE $i\gets 10$
\IF {$i\geq 5$}
  \STATE $i\gets i-1$
\ELSE
  \IF {$i\leq 3$}
    \STATE $i\gets i+2$
  \ENDIF
\ENDIF
\end{algorithmic}

\end{document}
```

[이 algcompatible 짧은 예제를 Overleaf에서 열어보세요](https://www.overleaf.com/docs?engine=\&snip_name=algcompatible\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cusepackage%7Balgcompatible%7D%0A%25+OR+%5Cusepackage%7Balgorithmic%7D%0A%5Cbegin%7Bdocument%7D%0A%5Cbegin%7Balgorithmic%7D%0A%5CSTATE+%24i%5Cgets+10%24%0A%5CIF+%7B%24i%5Cgeq+5%24%7D+%0A++%5CSTATE+%24i%5Cgets+i-1%24%0A%5CELSE%0A++%5CIF+%7B%24i%5Cleq+3%24%7D%0A++++%5CSTATE+%24i%5Cgets+i%2B2%24%0A++%5CENDIF%0A%5CENDIF+%0A%5Cend%7Balgorithmic%7D%0A%0A%5Cend%7Bdocument%7D)

일부 오래된 템플릿이나 문서 클래스는 이미 다음을 불러왔을 수 있습니다: `algorithmic`, 따라서 제공된 구문과 명령 이름을 따라야 합니다. *다음을 불러오면 안 됩니다: `algorithm2e`, `algpseudocode` 패키지를, 만약 `algorithmic` 또는 `algcompatible` 패키지가 이미 불러와져 있다면.*

다음 `algorithm` 패키지는 다음과 함께 사용할 수 있습니다: `algorithmic`/`algcompatible` 알고리즘에 번호가 매겨진 캡션을 추가하기 위해서입니다.

[Overleaf에서 전체 예제를 열어보세요](https://www.overleaf.com/project/new/template/20768?id=71235978\&templateName=algpseudocode+%2B+algorithm+example\&latexEngine=pdflatex\&texImage=texlive-full%3A2020.1\&mainFile=)

## algorithm2e 패키지는

다음 `algorithm2e` 패키지는 다음과는 상당히 다른 구문 구조를 가집니다: `algpseudocode`, `algcompatible` 및 `algorithmic` 패키지들과 다르므로, 어떤 패키지를 사용할지 또는 템플릿에 어떤 패키지가 불러와져 있는지 주의해야 합니다.

다음 `algorithm2e` 패키지는 다음을 제공합니다: `algorithm` 환경:

```latex
\documentclass{article}
\usepackage{algorithm2e}
\begin{document}
\begin{algorithm}
$i\gets 10$\;
\eIf{$i\geq 5$}
{
    $i\gets i-1$\;
}{
    \If{$i\leq 3$}
    {
        $i\gets i+2$\;
    }
}
\end{algorithm}

\end{document}
```

[이 algorithm2e 짧은 예제를 Overleaf에서 열어보세요](https://www.overleaf.com/docs?engine=\&snip_name=algorithm2e+example\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cusepackage%7Balgorithm2e%7D%0A%5Cbegin%7Bdocument%7D%0A%5Cbegin%7Balgorithm%7D%0A%24i%5Cgets+10%24%5C%3B%0A%5CeIf%7B%24i%5Cgeq+5%24%7D%0A%7B%0A++++%24i%5Cgets+i-1%24%5C%3B%0A%7D%7B%0A++++%5CIf%7B%24i%5Cleq+3%24%7D%0A++++%7B%0A++++++++%24i%5Cgets+i%2B2%24%5C%3B%0A++++%7D%0A%7D%0A%5Cend%7Balgorithm%7D%0A%0A%5Cend%7Bdocument%7D)

![Algorithm2e-0.png](/files/ebe3e90137b05d23dd8276c359e895f22c206066)

소스 코드의 모든 줄은 *반드시* 다음으로 끝나야 합니다: `\;` 그렇지 않으면 알고리즘이 출력에서 같은 텍스트 줄에서 계속됩니다. 블록을 시작하는 매크로가 있는 줄만 다음으로 끝나지 않아야 합니다: `\;`.

다음을 사용할 때: `algorithm2e` 다음을 사용할 수 있습니다: `\caption{...}\ref{...}` 이 `algorithm` 환경 안에서 직접 사용할 수 있으며, 다른 패키지를 불러올 필요가 없습니다. 그러나 알고리즘에 주석을 추가하려면 먼저 사용할 명령 이름을 선언해야 합니다:

```latex
%% 이는 \Comment 명령을 선언합니다
%% 인자는 /* ... */ 로 둘러싸입니다
\SetKwComment{Comment}{/* }{ */}

\begin{algorithm}
\caption{An algorithm with caption}\label{alg:two}
\KwData{$n \geq 0$}
\KwResult{$y = x^n$}
$y \gets 1$\;
$X \gets x$\;
$N \gets n$\;
\While{$N \neq 0$}{
  \eIf{$N$ is even}{
    $X \gets X \times X$\;
    $N \gets \frac{N}{2}$ \Comment*[r]{이것은 주석입니다}
  }{\If{$N$ is odd}{
      $y \gets y \times X$\;
      $N \gets N - 1$\;
    }
  }
}
\end{algorithm}
```

[캡션이 있는 이 algorithm2e 예제를 Overleaf에서 열어보세요](https://www.overleaf.com/docs?engine=\&snip_name=Captioned+algorithm2e+example\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cusepackage%7Balgorithm2e%7D%0A%5Cbegin%7Bdocument%7D%0A%25%25+This+declares+a+command+%5CComment%0A%25%25+The+argument+will+be+surrounded+by+%2F%2A+...+%2A%2F%0A%5CSetKwComment%7BComment%7D%7B%2F%2A+%7D%7B+%2A%2F%7D%0A%0A%5Cbegin%7Balgorithm%7D%0A%5Ccaption%7BAn+algorithm+with+caption%7D%5Clabel%7Balg%3Atwo%7D%0A%5CKwData%7B%24n+%5Cgeq+0%24%7D%0A%5CKwResult%7B%24y+%3D+x%5En%24%7D%0A%24y+%5Cgets+1%24%5C%3B%0A%24X+%5Cgets+x%24%5C%3B%0A%24N+%5Cgets+n%24%5C%3B%0A%5CWhile%7B%24N+%5Cneq+0%24%7D%7B%0A++%5CeIf%7B%24N%24+is+even%7D%7B%0A++++%24X+%5Cgets+X+%5Ctimes+X%24%5C%3B%0A++++%24N+%5Cgets+%5Cfrac%7BN%7D%7B2%7D%24+%5CComment%2A%5Br%5D%7BThis+is+a+comment%7D%0A++%7D%7B%5CIf%7B%24N%24+is+odd%7D%7B%0A++++++%24y+%5Cgets+y+%5Ctimes+X%24%5C%3B%0A++++++%24N+%5Cgets+N+-+1%24%5C%3B%0A++++%7D%0A++%7D%0A%7D%0A%5Cend%7Balgorithm%7D%0A%0A%5Cend%7Bdocument%7D)

![Algorithm2e-1.png](/files/72812f9795e174672f66692e872b5c1bef08a7d9)

기본적으로 `plain` 알고리즘 스타일이 사용됩니다. 하지만 알고리즘과 캡션 주위를 선으로 둘러싼 형식을 선호한다면 다음을 추가할 수 있습니다: `ruled` 를 불러올 때 패키지 옵션으로 `algorithm2e`, 또는 다음을 쓰세요: `\RestyleAlgo{ruled}` 문서에 포함하세요. 그러면 캡션이 있는 알고리즘이 다음과 같이 조판됩니다:

![Algorithm2e-11.png](/files/7f1f07798d603054aeae930f369ec4e2feff9deb)

다음 `algorithm2e` 패키지는 많은 사용자 지정 옵션을 제공합니다. 예를 들어 while—end while, if—end if 블록을 표시하는 세로선을 제거하려면 다음을 추가할 수 있습니다: `noline` 를 불러올 때 패키지 옵션으로 `algorithm2e`, 또는 다음을 쓰세요: `\SetNoline`. 또한 `\listofalgorithms` 명령도 다음에서 사용할 수 있습니다: `algorithm2e`.

[Overleaf에서 전체 예제를 열어보세요](https://www.overleaf.com/project/new/template/20787?id=71277562\&templateName=algpseudocode+%2B+algorithm+example\&latexEngine=pdflatex\&texImage=texlive-full%3A2020.1\&mainFile=)

## 추가 읽을거리

* [`algpseudocode` 패키지 문서](https://texdoc.org/pkg/algorithmicx) (3.1절 참조)
* [`algorithmic` 및 `알고리즘` 패키지 문서](https://texdoc.org/pkg/algorithms)
* [`algorithm2e` 패키지 문서](https://texdoc.org/pkg/algorithm2e)


---

# 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/more-topics/02-algorithms.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.
