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.

4
  • Is there only one paragraph in the parState or can they be more? Commented Jul 30, 2017 at 20:42
  • For my needs it is sufficient to assume there is only one paragraph. Thank you!
    – efie
    Commented Jul 30, 2017 at 21:08
  • I have not tried this package (yet) but it is designed to provide a solution to this problem: ctan.org/pkg/algxpar
    – Gus
    Commented Jan 12, 2022 at 5:46
  • As I commented elsewhere, tex.stackexchange.com/a/86351/1362 appears to provide a general solution, not tied to \State.
    – Gus
    Commented Jan 12, 2022 at 6:25

1 Answer 1

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

4
  • 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
    Commented Jul 31, 2017 at 10:35
  • 1
    Try \hangindent=\algorithmicindent instead of \algmargin. Commented Jul 31, 2017 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
    Commented Jan 12, 2022 at 5:59
  • It appears there is a solution here: tex.stackexchange.com/questions/78776/…
    – Gus
    Commented Jan 12, 2022 at 6:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .