[Tex/LaTex] How to indent long comment continuation in the algorithmicx pseudocode

algorithmicxindentationwidth

In the MWE, I try to add line continuation indent to the answer given in Properly indent comments with no line-numbering in algorithmicx

E.g. I am trying to write the \LineCommentCont .

I am using the linegoalpackage to set the width parameter of the \parboxautomatically, and \hangindent to indent the continuation.

I have two problems: 1) \linegoal seems to return a little less space than what is available. 2) How should I assign right \hangindent amount?

Alternatively, you can supply an answer with a totally different approach.

\documentclass{article}
\usepackage{algorithm}% http://ctan.org/pkg/algorithms
\usepackage{algpseudocode}% http://ctan.org/pkg/algorithmicx

\usepackage{linegoal}

\makeatletter
\algnewcommand{\LineComment}[1]{\Statex \hskip\ALG@thistlm \(\triangleright\) #1}
\algnewcommand{\LineCommentCont}[1]{\Statex \hskip\ALG@thistlm \parbox[t]{\linegoal}{\hangindent=1em\hangafter=1 $\triangleright$ #1}}

\makeatother
\begin{document}
\begin{algorithm}[!ht]
  \caption{My Algo.}\label{myalgo}
  \begin{algorithmic}[1]
    \State $\epsilon$ = 1.0; \Comment{Explore Latency Dimension}
    \While {explorationTime $\leq$ timeLimit}
      \State $\epsilon = \epsilon / 2$;
      \State calculateIncrements($\epsilon$);
      \LineComment{Explore L dimension}
      \While {lQuery $\leq$ lUpperLimit}
        \State Query (0, Query, bQuery, pQuery);
        \If {result = WORKING}
          \State mark points 
          \LineComment{no need to explore more. we just want to stop over here. no need to explore more. we just want to stop over here. no need to explore more. we just want to stop over here. }
          \LineCommentCont{no need to explore more. we just want to stop over here. no  to explore more. we just want to stop over here. no need to explore more. we just want to stop over here. }
          \State Break
        \Else
          \If {result = NOT WORKING}
            \State mark from 0 to lQuery as NOT WORKING.
          \EndIf
        \EndIf
        \State lQuery += lEpsIncr;
      \EndWhile
    \EndWhile
    \State calcPoints()
  \end{algorithmic}
\end{algorithm}
\end{document}

Comment line continuation in algorithmicx

Best Answer

Use the following definition of \LineCommentCont:

enter image description here

\documentclass{article}
\usepackage{algorithm}% http://ctan.org/pkg/algorithms
\usepackage{algpseudocode}% http://ctan.org/pkg/algorithmicx

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

\begin{document}
\begin{algorithm}[!ht]
  \caption{My Algo.}\label{myalgo}
  \begin{algorithmic}[1]
    \State $\epsilon$ = 1.0; \Comment{Explore Latency Dimension}
    \While {explorationTime $\leq$ timeLimit}
      \State $\epsilon = \epsilon / 2$;
      \State calculateIncrements($\epsilon$);
      \LineComment{Explore L dimension}
      \While {lQuery $\leq$ lUpperLimit}
        \State Query (0, Query, bQuery, pQuery);
        \If {result = WORKING}
          \State mark points 
          \LineComment{no need to explore more. we just want to stop over here. no need to explore more. we just want to stop over here. no need to explore more. we just want to stop over here. }
          \LineCommentCont{no need to explore more. we just want to stop over here. no  to explore more. we just want to stop over here. no need to explore more. we just want to stop over here.}
          \State Break
        \Else
          \If {result = NOT WORKING}
            \State mark from 0 to lQuery as NOT WORKING.
          \EndIf
        \EndIf
        \State lQuery += lEpsIncr;
      \EndWhile
    \EndWhile
    \State calcPoints()
  \end{algorithmic}
\end{algorithm}
\end{document}

Since you're indenting the "list" (algorithm) by \ALG@thistlm you only need to subtract that from \linewidth in order to have the correct width of you \parbox. There is therefore no need to use linegoal to determine the remaining width of the line.

The width \trianglerightwidth is obtained by measuring the width of $\trianglewidth$~.

I've added some \struts to the macros to avoid line height problems when you don't have descenders in your comment.


The indentation at every level is incremented by \algorithmicindent (default is 1.5em). Using

\algnewcommand{\LineCommentCont}[1]{\Statex \hskip\ALG@thistlm%
  \parbox[t]{\dimexpr\linewidth-\ALG@thistlm}{\hangindent=\algorithmicindent \hangafter=1 \strut\makebox[\algorithmicindent][l]{$\triangleright$}#1\strut}}

inserts an equivalently-spaced indentation to what you see with the other constructions:

enter image description here