14

I have this code

\documentclass{article}
\usepackage{algorithm}
\usepackage{algpseudocode}
\begin{document}
\begin{algorithm}
   \caption{Minimal Working Example for my Problem}
\begin{algorithmic}[1]
   \While{Indentation is a mess}
      \State Examine a very long line that looks horrible because the indentation is all messed up.
   \EndWhile
\end{algorithmic}
\end{algorithm}
\end{document}​

I would want the broken text to be indented to the same column, where the statement began.

Tom
  • 141

1 Answers1

18

Wrap your long line in a top-aligned \parbox:

enter image description here

\documentclass{article}
\usepackage{algorithm}% http://ctan.org/pkg/algorithms
\usepackage{algpseudocode}% http://ctan.org/pkg/algorithmicx
\begin{document}
\begin{algorithm}
  \caption{Minimal Working Example for my Problem}
  \begin{algorithmic}[1]
    \While{Indentation is a mess}
      \State \parbox[t]{\dimexpr\linewidth-\algorithmicindent}{Examine a very long line that looks horrible 
        because the indentation is all messed up.\strut}
    \EndWhile
  \end{algorithmic}
\end{algorithm}
\end{document}​

The current indentation (\algorithmicindent) is removed from \linewidth to fit exactly within the horizontal line width. Adding a \strut at the end allows for proper vertical alignment between lines (or \States) in lines that have no descenders.

Werner
  • 603,163
  • 2
    Thanks. Works well. Is there a way to make this the default behaviour? – Tom Aug 24 '12 at 05:50
  • 1
    @Tom: I'll have to look at the source code and see. I'll get back to you if I find anything. – Werner Aug 24 '12 at 14:27
  • 1
    @Tom: I'm sure this is possible, but it would be difficult. One would have to capture the content - not easily done - and pass it to \parbox. – Werner Oct 07 '12 at 06:12
  • 1
    @Werner I realize it's been some time since you wrote this answer but implementing it is causing my text to go slightly past the right margin for some reason. Do you have any idea why this would be? – syusim Aug 06 '15 at 17:08
  • 5
    @SamuelYusim: So you're getting an "overfull \hbox warning"? You probably are deeper into the nesting than in my example, right? You could use a manual 1.5em (1st level), 3em (2nd level), 4.5em (3rd level) ... rather than \algorithmicindent. The reason is that \algorithmicindent is not a length, but a macro. If this doesn't help, could you post some code I can look at (at Pastebin). – Werner Aug 06 '15 at 17:26
  • 1
    @Werner that solved the problem perfectly! Thanks! – syusim Aug 06 '15 at 20:29
  • 2
    For the reference of others, here is a macro ready to use for you: \newcommand{\algparbox}[1]{\parbox[t]{\dimexpr\linewidth-\algorithmicindent}{#1\strut}}. – Ricardo Cruz Jan 07 '16 at 01:52
  • Here is a macro I made that works for multi-level indentation. The first parameter is the number of levels of indentation required for that line.

    \newcommand*{\LongState}[2]{\State\parbox[t]{\dimexpr\linewidth-\dimexpr\algorithmicindent*#1}{#2\strut}}

    – inavda Mar 31 '22 at 00:09