Algorithicx를 사용하여 같은 줄에 명령문을 두지 마십시오.

Algorithicx를 사용하여 같은 줄에 명령문을 두지 마십시오.

간단한 의사 코드를 작성 중인데 반복..until 블록에 문제가 있습니다. 기본적으로 이 루프의 끝을 따르는 return 문은 Until 절과 같은 줄에 렌더링됩니다. 이는 보기 흉하지만 이를 자체 줄에 배치하는 방법을 찾을 수 없습니다.올바른 들여쓰기로.

여기에 예가 있습니다.

\documentclass[a4paper,10pt]{article}
\usepackage{algorithm}
\usepackage[noend]{algpseudocode}
\begin{document}
\begin{algorithm}
\caption{test di Fermat}\label{alg:test-fermat}
\begin{algorithmic}[1]
\Procedure{testFermat}{$n, prove$}
    \Repeat
        \State{$a \gets$ numero casuale tra 2 e $n-1$}
        \If{$a^n \not\equiv a \bmod n$}
            \Return composto
        \EndIf
        \State{$prove \gets prove - 1$}
    \Until{$prove > 0$}
    \Return forse primo
\EndProcedure
\end{algorithmic}
\end{algorithm}
\end{document}

를 사용하여 줄바꿈을 추가하려고 했지만 \\이로 인해 들여쓰기가 깨졌습니다. 수동으로 들여쓰기를 추가하려고 을 사용해 보았지만 \algorithmicindent공백 대신 "1.5em"이 표시됩니다. 뒤에 \State또는 를 추가하려고 했지만 이로 인해 들여쓰기도 깨졌습니다.\Statex\Until

\Return들여쓰기를 깨지 않고 마지막 줄을 단독으로 배치하는 간단한 방법이 있습니까 ?

그건 그렇고, 이것은 If 블록 내부의 반환에서도 발생하지만, When이 있는 경우에는 if가 그다지 나빠 보이지 않기 때문에 더 걱정됩니다.

편집 : 나는 읽었습니다이것질문이 있지만 문제가 해결되지는 않습니다.

varwidth나는 루프를 수정해야 하기 때문에 모든 들여쓰기를 수동으로 관리해야 하기 때문에 사용하고 싶지 않습니다 [ varwidth루프 중간에서 환경을 시작하면 다른 들여쓰기가 중단됩니다].

답변1

\Return이것은 아마도 사람들이 다른 진술과 같은 줄에 놓일 것인지 아니면 단독으로 배치할 것인지 를 선택할 수 있도록 하기 위한 상태별 선택이었을 것입니다 . 기본적으로 자체적으로 배치하려면 추가하십시오.

\algrenewcommand\Return{\State \algorithmicreturn{} }%

문서 서문에.

여기에 이미지 설명을 입력하세요

\documentclass[a4paper,10pt]{article}
\usepackage{algorithm}% http://ctan.org/pkg/algorithms
\usepackage[noend]{algpseudocode}% http://ctan.org/pkg/algorithmicx
\algrenewcommand\Return{\State \algorithmicreturn{} }%
\begin{document}
\begin{algorithm}
  \caption{test di Fermat}\label{alg:test-fermat}
  \begin{algorithmic}[1]
    \Procedure{testFermat}{$n, prove$}
      \Repeat
        \State{$a \gets$ numero casuale tra 2 e $n-1$}
        \If{$a^n \not\equiv a \bmod n$}
          \Return composto
        \EndIf
        \State{$prove \gets prove - 1$}
      \Until{$prove > 0$}
      \Return forse primo
    \EndProcedure
  \end{algorithmic}
\end{algorithm}
\end{document}​

답변2

가장 간단한 해결책은 명령에 return 문을 State다음과 같이 래핑하는 것입니다.

 \State{\Return{composto}}

관련 정보