You can renew the internal command \algorithmicindent
:
\algrenewcommand\algorithmicindent{2.0em}%
Change the 2.0em
to whatever you wish.
Here is a MWE extracted from the documentation and adapted:
\documentclass{article}
\usepackage{algorithmicx}
\usepackage[ruled]{algorithm}
\usepackage{algpseudocode}
\usepackage{algpascal}
\usepackage{algc}
\newcommand{\euk}{Euclid}
\algrenewcommand\algorithmicindent{2.0em}%
\begin{document}
\begin{algorithmic}[1]
\algrenewcommand{\algorithmiccomment}[1]{\hskip3em$\rightarrow$ #1}
\State $x\gets x+1$\Comment{Here is the new comment}
\end{algorithmic}
\alglanguage{pseudocode}
\begin{algorithm}[H]
\caption{\euk's algorithm}\label{euclid}
\begin{algorithmic}[1]
\Procedure{\euk}{$a,b$}\Comment{The g.c.d. of a and b}
\State $r\gets a\bmod b$
\While{$r\not=0$}\Comment{We have the answer if r is 0}
\State $a\gets b$
\State $b\gets r$
\State $r\gets a\bmod b$
\EndWhile\label{euclidendwhile}
\State \Return $b$\Comment{The gcd is b}
\EndProcedure
\end{algorithmic}
\end{algorithm}
\end{document}
One can modify the algorithmic
enviornment so that continuation lines are indented.
\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.
Best Answer
Wrap your long line in a
t
op-aligned\parbox
: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\State
s) in lines that have no descenders.