[Tex/LaTex] Forced indentation in algorithmicx

algorithmicxindentation

Problematic algorithm

I have an algorithm dealing with some very large parameter names, messing up the readability. What I've done so far is break the lines of the problematic statement (lines 4-5) manually into two separate statements. Without it the statement will go outside the page margins. The procedure signature wraps itself.

What I want to do is to indent the overflowing lines in both the procedure name and the long statement to the location of the arrows in the figure above.

How should I do this? I am open for suggestions to other packages for algorithms as well.

\documentclass[12pt,a4paper,english,twoside,titlepage]{report}
\usepackage{algorithm}
\usepackage{algpseudocode}
\begin{document}
    \begin{algorithm}
        \caption{This procedure calculates the \texttt{response} value}
        \begin{algorithmic}[1]
            \Procedure {CalculateResponse}{$pszMethod$, $pszDigestUri$, $pszQop$, $pszUsername$, $pszRealm$, $pszPassword$, $pszNonce$, $pszCNonce$, $pszNonceCount$}
                \State $ha1 \gets DigestCalcHA1(pszUsername, pszRealm, pszPassword)$
                \State $ha2 \gets DigestCalcHA2(pszMethod, pszDigestUri, pszQop)$
                \State $response \gets calculateResponse(ha1, ha2,pszMethod, pszDigestUri,$
                \State $pszQop,pszNonce, pszNonceCount, pszCNonce)$
        \EndProcedure
            \Statex
        \end{algorithmic}
    \end{algorithm}
\end{document}

Best Answer

One can modify the algorithmic enviornment so that continuation lines are indented.

Sample output

\documentclass[12pt,a4paper]{report}
\usepackage{algorithm}
\usepackage{algpseudocode}

\makeatletter
\newlength{\continueindent}
\setlength{\continueindent}{6em}

\renewenvironment{algorithmic}[1][0]%
   {%
   \edef\ALG@numberfreq{#1}%
   \def\@currentlabel{\theALG@line}%
   %
   \setcounter{ALG@line}{0}%
   \setcounter{ALG@rem}{0}%
   %
   \let\\\algbreak%
   %
   \expandafter\edef\csname ALG@currentblock@\theALG@nested\endcsname{0}%
   \expandafter\let\csname ALG@currentlifetime@\theALG@nested\endcsname\relax%
   %
   \begin{list}%
      {\ALG@step}%
      {%
      \rightmargin\z@%
      \itemsep\z@ \itemindent\z@ \listparindent2em%
      \partopsep\z@ \parskip\z@ \parsep\z@%
      \labelsep 0.5em \topsep 0.2em%\skip 1.2em 
      \ifthenelse{\equal{#1}{0}}%
         {\labelwidth 0.5em}%
         {\labelwidth 1.2em}%
       \leftmargin\labelwidth \addtolength{\leftmargin}{\labelsep}
      \ALG@tlm\z@%
      }%
      \parshape 2 \leftmargin \linewidth \continueindent \dimexpr\linewidth-\continueindent\relax
   \setcounter{ALG@nested}{0}%
   \ALG@beginalgorithmic%
   }%
   {% end{algorithmic}
   % check if all blocks are closed
   \ALG@closeloops%
   \expandafter\ifnum\csname ALG@currentblock@\theALG@nested\endcsname=0\relax%
   \else%
      \PackageError{algorithmicx}{Some blocks are not closed!!!}{}%
   \fi%
   \ALG@endalgorithmic%
   \end{list}%
   }%
\makeatother


\begin{document}

\begin{algorithm}
  \caption{This procedure calculates the \texttt{response} value}
  \begin{algorithmic}[1]
      \Procedure {CalculateResponse}{$pszMethod$,
        $pszDigestUri$, $pszQop$, $pszUsername$, $pszRealm$,
        $pszPassword$, $pszNonce$, $pszCNonce$, $pszNonceCount$} 
        \State $ha1 \gets DigestCalcHA1(pszUsername, pszRealm,
          pszPassword)$ 
        \State $ha2 \gets DigestCalcHA2(pszMethod, pszDigestUri,
          pszQop)$ 
        \State $response \gets calculateResponse(ha1,
          ha2,pszMethod, pszDigestUri,\break pszQop,pszNonce,
          pszNonceCount, pszCNonce)$ 
      \EndProcedure
      \Statex
  \end{algorithmic}
\end{algorithm}

\end{document}

The change to the standard definition of the environment is that a \parshape command has been added to the items. The amount of indentation is controlled by a new length \continueindent. Now you can either let latex break lines for you automatically, and so may wish to look at the question Allowing line break at ',' in inline math mode? to make this go more smoothly, or you can manually add \break commands in the formulae. I have included an example of each in the above code.

For breaks in text use \newline.

Should you need extra indentation on a single line you can use \break\hspace*{2em} and \newline\hspace*{2em}.

Also consider using @JLDiaz suggestion of \raggedright to avoid lines being stretched too much.