1

MWE:

\documentclass[11pt,a4paper,notitlepage]{report}
\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{amssymb}
\usepackage{algorithm}% http://ctan.org/pkg/algorithms
\usepackage{algpseudocode}% http://ctan.org/pkg/algorithmicx
\begin{document}
\begin{algorithm}[h!]
\caption{xxx} 
\label{alg:a_star}
    \begin{algorithmic}[1]
        \State start 
        \While{$O \neq \emptyset$}
            \State very loooooooooooong, long, long, long ,long ,long ,long ,long, long ,long, long ,long   ,long,long, long, long ,long ,long ,long ,long, long ,long, long ,long ,long, long, long ,long, long long long statement
        \EndWhile
        \end{algorithmic}
\end{algorithm}
\end{document}

Result:

enter image description here

How can I use 'algpseudocode' and keep the indent when automatically a new line is used? I already tried the solutions from here but without success.

d4rty
  • 779
  • 2
    Did you look at https://tex.stackexchange.com/questions/314023/how-to-indent-a-long-sentence-in-an-algorithm – albert Jun 02 '19 at 11:33

1 Answers1

4

Slightly changed @Wrner's answer on question and adopted to your MWE:

\documentclass[11pt,a4paper,notitlepage]{report}
\usepackage{algorithm}% http://ctan.org/pkg/algorithms
\usepackage{algpseudocode}% http://ctan.org/pkg/algorithmicx

\makeatletter
\newcommand\multiline[1]{\parbox[t]{\dimexpr\linewidth-\ALG@thistlm}{#1}}
\makeatother

\begin{document}
\begin{algorithm}[h!]
\caption{xxx}
\label{alg:a_star}
    \begin{algorithmic}[1]
        \State start
        \While{$O \neq \emptyset$}
            \State\multiline{ very loooooooooooong, long, long, long ,long, long, long, long, long, long, long, long, long, long, long, long, long, long, long, long, long, long, long, long, long, long, long, long statement}
        \EndWhile
        \end{algorithmic}
\end{algorithm}

enter image description here

Edit: For more vertical space after \parboxyou can add to definition of\multiline command \strut (as pointed @Johannes Z) or \smallskip˙:

\makeatletter
\newcommand\multiline[1]{\parbox[t]{\dimexpr\linewidth-\ALG@thistlm}{#1\smallskip}}  % or \strut
\makeatother

Considering this change result of MWE is:

enter image description here

Zarko
  • 296,517
  • This makes the vertical space between the last line of \multiline and \EndWhile too small. Changing definition of \multiline to \parbox[t]{...}{#1\strut} will fix this, as suggested in https://latex.org/forum/viewtopic.php?t=15251 . – muzimuzhi Z Dec 05 '19 at 08:41
  • @JohannesZ, indeed, vertical space can be increase by \strut or by \smallskip. Added to answer. – Zarko Dec 05 '19 at 10:59
  • There are differences. \smallskip always adds a font-size-independent amount (e.g., 3pt plus 1pt minus 1pt for class article with 10pt) of vertical skip, while \strut inserts an invisible (means zero-width) box with font-size-dependent dimensions (0.7em height and 0.3em depth). They differ in a) whether font-size-dependent and b) whether "always take effect" (since if the last line has height and depth both larger than that of a \strut, then \strut has no visual effects). – muzimuzhi Z Dec 06 '19 at 05:02