So rücken Sie einen langen Satz in einem Algorithmus ein

So rücken Sie einen langen Satz in einem Algorithmus ein

Bildbeschreibung hier eingeben

In diesem Algorithmusformat möchte ich, dass „Unter Berücksichtigung des j-ten …“ überall die gleiche Einrückung hat, auch im Zeilenumbruch.

Der Code lautet wie folgt:

\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}

Antwort1

Sie könnentabularxum die Breite einer Box zu ermitteln, die bis zum Ende der Zeile passen würde:

Bildbeschreibung hier eingeben

\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>}setzt seinen Inhalt in eine tabularx, die am [t]op innerhalb einer X-Spalte ausgerichtet ist. Die Breite dieser Spalte ist die Differenz aus einem normalen \linewidthZeileneinzug abzüglich des aktuellen Algorithmus.

Antwort2

Wenn man mit manuellen Zeilenumbrüchen leben kann, dann \Longunderstack[l]{}ist ein eine Option

\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}

Bildbeschreibung hier eingeben

verwandte Informationen