[Tex/LaTex] How to have continuation indent in algorithmicx algorithm for long comments and States

algorithmicx

Assuming that I can apply the same solution to my document, previously I asked how to have continuation indent in line comments on somebody else's MWE and got an answer.

However, I could not apply that solution to MY document. So I am re-posting a MWE with keeping my preamble, with following questions:

1-) How to have continuation indent in the line comment below?

2-) While you are at it, can you add continuation indent to the long \State, too?

% 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{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}

preview

Best Answer

The key problem was that there was a necessity to use comment as a parameter to \LineCommentCont, instead of \LineCommentRun Run the... which took only the first letter we must write \LineCommentRun{Run the...}. I created a new command named \MyState to handle multiline state situations. I enclose an example and a preview of the result.

%! *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}

mwe