[Tex/LaTex] Indentation of continued lines in algorithmicx

algorithmicxindentation

Trying to get the indentation right within the algorithmic environment I run into trouble, and all the solutions posted so far have not been able to solve my problem as I'd like to see it solved.

I want continued lines to have an indentation w.r.t the initial line (new to this forum and yet unable to post images).

However, the best I get with what I've found on this forum is either a fixed indentation for all continued lines (i.e., no difference if the initial line was indented or not), or the same indentation as the initial line (see MWE below).

Anybody has an answer to this?

\documentclass[10pt, A4]{report}
\usepackage{algorithm}
\usepackage{algpseudocode}

\begin{document}

\begin{algorithmic}\raggedright

\State Define N\_crf as the number of points on the circumference of the hyperstreamline, which will define its cross-sectional area at each r

\For {n = 1 : number of tracts}

\State \parbox[t]{\dimexpr\linewidth-\algorithmicindent\relax}{define N\_crf points creating a unit circle around r(n,i), in the plane perpendicular to t(n,i).}\strut

\EndFor

\end{algorithmic}

\end{document}

Best Answer

Set the paragraph \hangindent:

enter image description here

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

\begin{document}

\begin{algorithmic}
  %\raggedright
  \State Define \texttt{N\_crf} as the number of points on the circumference 
    of the hyperstreamline, which will define its cross-sectional area at each~$r$
  \For {$n = 1$ : number of tracts}
  \State \parbox[t]{\dimexpr\linewidth-\algorithmicindent\relax}{%
    \setlength{\hangindent}{\algorithmicindent}%
      define N\_crf points creating a unit circle around $r(n,i)$, in the plane 
      perpendicular to $t(n,i)$.}\strut
  \EndFor
\end{algorithmic}

\end{document}

You could define yourself a \hangparbox:

\newcommand{\hangparbox}[3][t]{\parbox[#1]{\dimexpr#2}{%
  \setlength{\hangindent}{\algorithmicindent}#3}}

Another crude method to obtain this would be to insert and remove an \algorithmicindent before/after the \parbox:

\State \hspace*{\algorithmicindent}\parbox[t]{\dimexpr\linewidth-\algorithmicindent}{%
  \hspace*{-\algorithmicindent}
  %...
  }

This may come in handy when dealing with deeply-nested parts of your algorithm.