算法中长单语句的自动生成换行符处的缩进会丢失

算法中长单语句的自动生成换行符处的缩进会丢失

梅威瑟:

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

结果:

在此处输入图片描述

如何使用 'algpseudocode' 并在自动使用新行时保持缩进?我已经尝试了以下解决方案这里但没有成功。

答案1

稍微改变了@Wrner 的回答问题并采纳给您的 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}

在此处输入图片描述

编辑:为了获得更多垂直空间,\parbox您可以添加到\multiline命令的定义\strut(如@Johannes Z 所指出的)或\smallskip˙

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

考虑到MWE的这种变化结果是:

在此处输入图片描述

相关内容