14

I am having problems displaying algorithms. The wrapping is really ugly and I can't, for the life of me, find a fix that doesn't involve manually tweaking the hell out of everything, to the point that I might as well scrap the crappy algorithm packages and write everything out in TeX.

Is there any way to properly indent the following

\documentclass{article}
\usepackage{algorithm}
\usepackage{algpseudocode}
\begin{document}
\begin{algorithm}
   \caption{My pseudo code.}
\begin{algorithmic}[1]
   \While{this line is sooooooooooo long and boring and too much for algorithmic to handle}
      \State look at this state, this state is just too long for algorithmic to display it properly 
      \While{again this line is sooooooooooo long and boring and too much for algorithmic too handle}
         \State look at this state again, this state is just too long for algorithmic to handle, I'm just going to switch to Word
      \EndWhile
   \EndWhile
\end{algorithmic}
\end{algorithm}
\end{document}​

Ugly algorithm

so that the output is similar to

Pretty algorithm

1

2 Answers 2

9

algorithmicx obviously wasn't intended to manage paragraph-style text as part of pseudo code. You're going to have to do some of your own legwork in order for it to replicate your requirements:

enter image description here

\documentclass{article}
\usepackage{algorithm,algpseudocode}
\makeatletter
\newcommand{\algmargin}{\the\ALG@thistlm}
\makeatother
\newlength{\whilewidth}
\settowidth{\whilewidth}{\algorithmicwhile\ }
\algdef{SE}[parWHILE]{parWhile}{EndparWhile}[1]
  {\parbox[t]{\dimexpr\linewidth-\algmargin}{%
     \hangindent\whilewidth\strut\algorithmicwhile\ #1\ \algorithmicdo\strut}}{\algorithmicend\ \algorithmicwhile}%
\algnewcommand{\parState}[1]{\State%
  \parbox[t]{\dimexpr\linewidth-\algmargin}{\strut #1\strut}}

\begin{document}
\begin{algorithm}
  \caption{My pseudo code.}
  \begin{algorithmic}[1]
    \While{this line is sooooooooooo long and boring and too much for algorithmic to handle}
      \State look at this state, this state is just too long for algorithmic to display it properly 
      \While{again this line is sooooooooooo long and boring and too much for algorithmic too handle}
        \State look at this state again, this state is just too long for algorithmic to handle, I'm just going to switch to Word
      \EndWhile
    \EndWhile
    \State
    \parWhile{this line is sooooooooooo long and boring and too much for algorithmic to handle}
      \parState{%
        look at this state, this state is just too long for algorithmic to display it properly}
      \parWhile{again this line is sooooooooooo long and boring and too much for algorithmic too handle}
        \parState{%
          look at this state again, this state is just too long for algorithmic to handle, I'm just going to switch to Word}
      \EndparWhile
    \EndparWhile
  \end{algorithmic}
\end{algorithm}
\end{document}​

In the above code, \parWhile and \parState define the paragraph-style pseudo-code macros that you're after.

1
  • Thank you thank you thank you! These macros do exactly what I want. Sep 8, 2014 at 17:17
0

Use \hspace*{5mm} depending upon the space you need (change 5mm for your needs). It is easy and no need to write macros. It works fine with me.

1
  • 1
    Welcome to TeX.SE! Can you please add an compiling MWE showing your solution for a fast proof?
    – Mensch
    Oct 27, 2017 at 1:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.