알고리즘에서 긴 문장을 들여쓰는 방법

알고리즘에서 긴 문장을 들여쓰는 방법

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

이 알고리즘 형식에서는 "j번째를 고려합니다...."가 개행에서도 전체적으로 동일한 들여쓰기를 갖기를 원합니다.

코드는 다음과 같습니다:

\documentclass{article}
\usepackage{amsmath}
\usepackage{algorithm}
\usepackage[noend]{algpseudocode}
\makeatletter
\def\BState{\State\hskip-\ALG@thistlm}
\makeatother
\begin{document}
\begin{algorithm}
\caption{hello}\label{euclid}
\begin{algorithmic}[1]
\Procedure{ASAPP}{}
\State $\textit{i} \gets \text{0}$, $\textit{j} \gets \text{0}$, $\textit{k} \gets \text{0}$
\While {i $\leq$ nom}
\While {j $\leq C_{i}$}
\State Considering the \textit{j}th edge of the \textit{i}th metabolite, \textit{x} and \textit{y} contains the vertices of the \textit{j}th edge
\State $\textit{k} \gets \text{\textit{j} + 1}$
\While { k $\leq C_{i}$}
\State Considering the \textit{k}th edge of the \textit{i}th metabolite, \textit{x1} and \textit{y1} contains the vertices of the \textit{k}th edge.
\EndWhile
\EndWhile
\EndWhile
\BState \emph{top}:
\EndProcedure
\end{algorithmic}
\end{algorithm}
\end{document}

답변1

당신이 사용할 수있는tabularx줄 끝까지 들어갈 수 있는 상자의 너비를 알아내려면 다음을 수행하십시오.

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

\documentclass{article}

\usepackage{amsmath,algorithm,tabularx}
\usepackage[noend]{algpseudocode}

\makeatletter
\newcommand{\multiline}[1]{%
  \begin{tabularx}{\dimexpr\linewidth-\ALG@thistlm}[t]{@{}X@{}}
    #1
  \end{tabularx}
}
\makeatother

\begin{document}

\begin{algorithm}
  \caption{hello}
  \begin{algorithmic}[1]
    \Procedure{ASAPP}{}
      \State $i \gets 0$, $j \gets 0$, $k \gets 0$
      \While { $i \leq \text{nom}$ }
        \While {$j \leq C_i$ }
          \State \multiline{%
            Considering the $j$-th edge of the $i$-th metabolite,~$x$ and~$y$ 
            contains the vertices of the $j$-th edge}
          \State $k \gets j + 1$
          \While { $k \leq C_i$ }
            \State \multiline{%
              Considering the $k$-th edge of the $i$-th metabolite,~$x_1$ and~$y_1$ 
              contains the vertices of the $k$-th edge.}
          \EndWhile
        \EndWhile
      \EndWhile
    \EndProcedure
  \end{algorithmic}
\end{algorithm}

\end{document}

\multiline{<stuff>}-column 내부의 op tabularx에 정렬된 a에 내용을 설정합니다 . 이 열의 너비는 일반 줄에서 현재 알고리즘 줄 들여쓰기를 뺀 차이입니다.[t]X\linewidth

답변2

수동으로 줄 바꿈을 할 수 있다면 a \Longunderstack[l]{}는 옵션입니다.

\documentclass{article}
\usepackage{amsmath}
\usepackage{algorithm}
\usepackage[noend]{algpseudocode}
\usepackage{stackengine}
\setstackEOL{\\}
\makeatletter
\def\BState{\State\hskip-\ALG@thistlm}
\makeatother
\begin{document}
\begin{algorithm}
\caption{hello}\label{euclid}
\begin{algorithmic}[1]
\Procedure{ASAPP}{}
\State $\textit{i} \gets \text{0}$, $\textit{j} \gets \text{0}$, 
  $\textit{k} \gets \text{0}$
\While {i $\leq$ nom}
\While {j $\leq C_{i}$}
\State \Longunderstack[l]{Considering the \textit{j}th edge of the
   \textit{i}th metabolite, \textit{x} and \textit{y}\\ 
  contains the vertices of the \textit{j}th edge}
\State $\textit{k} \gets \text{\textit{j} + 1}$
\While { k $\leq C_{i}$}
\State \Longunderstack[l]{Considering the \textit{k}th edge of the 
   \textit{i}th metabolite, \textit{x1} and \textit{y1}\\ 
  contains the vertices of the \textit{k}th edge.}
\EndWhile
\EndWhile
\EndWhile
\BState \emph{top}:
\EndProcedure
\end{algorithmic}
\end{algorithm}
\end{document}

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

관련 정보