3

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:

enter image description here

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.

efie
  • 409
  • 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
  • I have not tried this package (yet) but it is designed to provide a solution to this problem: https://ctan.org/pkg/algxpar – Gus Jan 12 '22 at 05:46
  • As I commented elsewhere, https://tex.stackexchange.com/a/86351/1362 appears to provide a general solution, not tied to \State. – Gus Jan 12 '22 at 06:25

1 Answers1

2

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}

enter image description here

Ulrike Fischer
  • 327,261
  • 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
  • 1
    Try \hangindent=\algorithmicindent instead of \algmargin. – Ulrike Fischer Jul 31 '17 at 12:52
  • Is there a solution to wrap many arguments to a procedure resulting in wrapping? e.g. \Procedure{MyProcedure}{longarg1, longarg2, longarg3, longarg4, longarg5} – Gus Jan 12 '22 at 05:59
  • It appears there is a solution here: https://tex.stackexchange.com/questions/78776/forced-indentation-in-algorithmicx – Gus Jan 12 '22 at 06:08