3

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

5

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

| improve this answer | |
  • Thanks. but I actually wanted the continuation indent to be same as the other indents in the picture. I mean the indent of the content inside while. I think, around two times trianglerightwidth. May be you know that value? – ozi Apr 4 '14 at 18:43
  • @ozi: I've added an updated that uses the default \algorithmicindent for your \hangindent, as well as the width of the \triangleright marker. I assume this is what you were after? – Werner Apr 4 '14 at 19:06
  • More than what I want :). I used this: \algnewcommand{\LineCommentCont}[1]{\Statex \hskip\ALG@thistlm% \parbox[t]{\dimexpr\linewidth-\ALG@thistlm}{\hangindent=\algorithmicindent \hangafter=1 $\triangleright${} #1\strut}}. – ozi Apr 4 '14 at 19:40
  • I noticed a problem: if the comment is not after 'mark points' but right after if result = , it is indented one less then necessary. – ozi Apr 5 '14 at 18:31
  • @ozi Since I personally don't mind about numbering line comments, I replaced Statex by State, and removed \hskip\ALG@thistlm as well, which gives me the correct behaviour for both "just after a \If" and "After an existing \State". There is the code I use: \algnewcommand{\LineCommentCont}[1]{\State \parbox[t]{\dimexpr\linewidth-\ALG@thistlm}{\hangindent=\trianglerightwidth \hangafter=1 \strut$\triangleright$ #1\strut}} – Boris Dalstein Jun 8 '14 at 23:20
0

What about simply

\algnewcommand{\LineCommentCont}[1]{\State \phantom{$\triangleright$} #1}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.