如何在 algorithmicx 算法中对长注释和状态进行连续缩进

如何在 algorithmicx 算法中对长注释和状态进行连续缩进

假设我可以将相同的解决方案应用于我的文档,之前我问过如何在行注释中继续缩进在其他人的 MWE 上并得到了答案。

但是,我无法将该解决方案应用于我的文档。因此,我重新发布了 MWE,保留了我的序言,并提出了以下问题:

1-) 如何在下面的行注释中进行连续缩进?

2-) 当您这样做时,您\State也可以为较长的添加连续缩进吗?

% page layout ------------------------
\documentclass[]{article}
%\usepackage[paperheight=22.9cm, paperwidth=8.1cm, margin=0.1cm]{geometry}

% preamble ---------------------------
\usepackage[T1]{fontenc}
\usepackage{float}
\usepackage[ampersand]{easylist}
\usepackage{cite}
\usepackage[pdftex]{graphicx}
\graphicspath{{./}{./fig/}}
\DeclareGraphicsExtensions{.pdf,.jpeg,.jpg,.png,.svg}
\usepackage{todonotes}
\newcommand{\fixme}[1]{\todo[inline]{#1}}
\usepackage[cmex10]{amsmath}
\interdisplaylinepenalty=2500
\usepackage{tabulary}
\usepackage{algorithm}
\usepackage{algpseudocode}
\usepackage[english]{babel}
\usepackage[utf8]{inputenc}
\usepackage{authblk}
\usepackage[caption=false,font=footnotesize]{subfig}
\usepackage{bm}
\usepackage{mathtools}
\date{}



\renewcommand{\vec}[1]{\bm{#1}}


\begin{document}




\newcommand{\To}{\textbf{to\ }}
\newcommand{\Step}{\textbf{step\ }}
\newcommand{\Gets}{$ \gets\ $}
\renewcommand{\algorithmicforall}{\textbf{for each}}
\floatname{algorithm}{Pseudocode}

\makeatletter
\newlength{\trianglerightwidth}
\settowidth{\trianglerightwidth}{$\triangleright$~}
\algnewcommand{\LineCommentCont}[1]{\Statex \hskip\ALG@thistlm%
  \parbox[t]{\dimexpr\linewidth-\ALG@thistlm}{\hangindent=\trianglerightwidth \hangafter=1 \strut$\triangleright$ #1\strut}}
\makeatother



\begin{algorithm}[H]
\begin{algorithmic}[1]

\Procedure{Compare}{P,A,I,E}

\ForAll{$p \in P$}
    \ForAll{$a \in A$}
        \For{$i$ \Gets $1$ \To $I$ \Step 1}
            \LineCommentCont Run the algorithm $a$ on problem $p$ to get list of (function evaluation, corresponding global best solution value) pairs $Y$. Algorithm $a$ terminates after doing $E$ function evaluations.

            \State $Y_i$ \Gets $a(p,E)$ aaaa bbbb ccccc aaaa bbbb ccccc aaaa bbbb ccccc aaaa bbbb ccccc aaaa bbbb ccccc aaaa bbbb ccccc aaaa bbbb ccccc aaaa bbbb ccccc aaaa bbbb ccccc aaaa bbbb ccccc aaaa bbbb ccccc 
        \EndFor
    \EndFor
\EndFor
\EndProcedure
\end{algorithmic}
\caption{T }
\label{alg:compare}
\end{algorithm}



\end{document}

预览

答案1

关键问题是,必须使用注释作为参数\LineCommentCont,而不是\LineCommentRun Run the...只接受第一个字母,我们必须写入\LineCommentRun{Run the...}。我创建了一个名为的新命令\MyState来处理多行状态情况。我附上了一个示例和结果预览。

%! *latex mal-algorithm.tex
\documentclass[a4paper]{article}
\pagestyle{empty}
\usepackage{algorithm}
\usepackage{algpseudocode}
\usepackage[caption=false,font=footnotesize]{subfig}
\begin{document}
\frenchspacing
\newcommand{\To}{\textbf{to\ }}
\newcommand{\Step}{\textbf{step\ }}
\newcommand{\Gets}{$ \gets\ $}
\renewcommand{\algorithmicforall}{\textbf{for each}}
\floatname{algorithm}{Pseudocode}
\makeatletter
% First new command...
\newlength{\trianglerightwidth}
\settowidth{\trianglerightwidth}{$\triangleright$~}
\algnewcommand{\LineCommentCont}[1]{\Statex \hskip\ALG@thistlm%
  \parbox[t]{\dimexpr\linewidth-\ALG@thistlm}
{\leftskip=\algorithmicindent
  \hangindent=\algorithmicindent 
  \hangafter=1%
  \strut\makebox[\algorithmicindent][c]{$\triangleright$}#1\strut}
  } % \trianglerightwidth
% Second new command...
\algnewcommand{\MyState}[1]{\State
\parbox[t]{\dimexpr\linewidth-\ALG@thistlm}{\hangindent=\algorithmicindent\strut\hangafter=1#1\strut}}
\makeatother
\begin{algorithm}[H]
\begin{algorithmic}[1]
\Procedure{Compare}{P,A,I,E}
\ForAll{$p \in P$}
    \ForAll{$a \in A$}
        \For{$i$ \Gets $1$ \To $I$ \Step 1}
            \LineCommentCont{Run the algorithm $a$ on problem $p$ to get list of (function evaluation, corresponding global best solution value) pairs $Y$. Algorithm $a$ terminates after doing $E$ function evaluations.}
            \MyState{$Y_i$ \Gets $a(p,E)$ aaaa bbbb ccccc aaaa bbbb ccccc aaaa bbbb ccccc aaaa bbbb ccccc aaaa bbbb ccccc aaaa bbbb ccccc aaaa bbbb ccccc aaaa bbbb ccccc aaaa bbbb ccccc aaaa bbbb ccccc aaaa bbbb ccccc}
        \EndFor
    \EndFor
\EndFor
\EndProcedure
\end{algorithmic}
\caption{My first algorithm}
\label{alg:compare}
\end{algorithm}
\end{document}

姆韦

相关内容