How to wrap lines correctly inside algorithmic + more indentation for children lines

Please refer to this answer. I used its parState command to create this MWE:

\documentclass{article}
\usepackage{algorithm,algpseudocode}
\makeatletter
\newcommand{\algmargin}{\the\ALG@thistlm}
\makeatother
\algnewcommand{\parState}[1]{\State%
\parbox[t]{\dimexpr\linewidth-\algmargin}{\strut #1\strut}}

\begin{document}
\begin{algorithm}
\caption{My pseudo code.}
\begin{algorithmic}[1]
\If{$true$}
\parState{%
Look at this state again, this state is just too long for algorithmic to handle, I'm just going to switch to Word. Look at this state again, this state is just too long for algorithmic to handle, I'm just going to switch to Word. Look at this state again, this state is just too long for algorithmic to handle, I'm just going to switch to Word.}
\EndIf
\end{algorithmic}
\end{algorithm}
\end{document}


which yields this:

How can the last 4 lines of line 2 be even more intended than the first line of line 2? I'd like them to be intended by the amount of space which is used to intend line 2 with respect to the line 1.

• Is there only one paragraph in the parState or can they be more? – Ulrike Fischer Jul 30 '17 at 20:42
• For my needs it is sufficient to assume there is only one paragraph. Thank you! – efie Jul 30 '17 at 21:08

If there is only one paragraph involved you can use \hangindent/\hangafter:

\documentclass{article}
\usepackage{algorithm,algpseudocode}
\makeatletter
\newcommand{\algmargin}{\the\ALG@thistlm}
\makeatother
\algnewcommand{\parState}[1]{\State%
\parbox[t]{\dimexpr\linewidth-\algmargin}{\strut\hangindent=\algorithmicindent \hangafter=1 #1\strut}}

\begin{document}
\begin{algorithm}
\caption{My pseudo code.}
\begin{algorithmic}[1]
\If{$true$}
\parState{%
Look at this state again, this state is just too long for algorithmic to handle, I'm just going to switch to Word. Look at this state again, this state is just too long for algorithmic to handle, I'm just going to switch to Word. Look at this state again, this state is just too long for algorithmic to handle, I'm just going to switch to Word.}
\EndIf
\end{algorithmic}
\end{algorithm}
\end{document}


• Thank you! However, if I add another if statement inside this if statement the indentation for the inner one is twice as large as the one for the outer one. Could this be solved as well? – efie Jul 31 '17 at 10:35
• Try \hangindent=\algorithmicindent instead of \algmargin. – Ulrike Fischer Jul 31 '17 at 12:52