Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I have the following code,

\documentclass[]{memoir}
\usepackage[utf8x]{inputenc}
\usepackage{amssymb}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{algorithmic}

\newcommand{\algorithmname}{Algorithm}
\newcommand{\listalgorithmname}{List of Algorithms}
\newlistof{listofalgorithms}{loa}{\listalgorithmname}
\newfloat{algorithm}{loa}{\algorithmname}
\newfixedcaption{\falgcaption}{algorithm}
\newlistentry{algorithm}{loa}{0}  

\usepackage{float}
\floatstyle{ruled}
\restylefloat{algorithm}


\begin{document}


\tableofcontents
\newpage
\listoffigures
\listoftables
\listofalgorithms

\newpage
\chapter{Example Chapter}
\section{Example Section}
\begin{algorithm}[htbp]
\caption{An example algorithm}
\label{alg1}
\begin{algorithmic}
\REQUIRE Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer lobortis luctus tincidunt. Ut commodo, erat quis tempor cursus, felis lectus. 
\ENSURE Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer lobortis luctus tincidunt. Ut commodo, erat quis tempor cursus, felis lectus. 
\STATE Statement 1: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer lobortis luctus tincidunt. Ut commodo, erat quis tempor cursus, felis lectus. \COMMENT{This is a comment}
\REPEAT
\STATE Statement 2: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer lobortis luctus tincidunt. Ut commodo, erat quis tempor cursus, felis lectus. 
\STATE Statement 3:  Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer lobortis luctus tincidunt. Ut commodo, erat quis tempor cursus, felis lectus. 
\FOR{$i=0$ to $k$}
\STATE Statement 4:  Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer lobortis luctus tincidunt. Ut commodo, erat quis tempor cursus, felis lectus. 
\STATE Statement 5: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer lobortis luctus tincidunt. Ut commodo, erat quis tempor cursus, felis lectus. 
\ENDFOR
\UNTIL{Lorem ipsum dolor sit amet}
\STATE Statement 6: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer lobortis luctus tincidunt. Ut commodo, erat quis tempor cursus, felis lectus. 
\end{algorithmic}
\end{algorithm}
\end{document}

This typeset as follows,

Noindent

As my pseudo code statements are relatively long, I would like the \STATE section (Statement 1...Statement 6) to have a slight hanging indentation as the \REQUIRE sections.

share|improve this question

1 Answer 1

up vote 3 down vote accepted

This has nothing to do with the memoir class, only the algormithmic package.

The internal command controlling the printing commands like \STATE is \ALC@it. This command is defined locally in each algorithmic environment. The \STATE is just an \item in a list, but with some adjustments. The change one wants to make to this command is to add a \parshape just after the call to \item. In this case the approrpiate \parshape is

\parshape 2 \dimexpr\ALC@tlm+\labelwidth\relax
\dimexpr\textwidth-\ALC@tlm-\labelwidth\relax 
\dimexpr\ALC@tlm+\labelwidth+\continueindent\relax
\dimexpr\textwidth-\ALC@tlm-\labelwidth-\continueindent\relax 

\ALC@tlm essentially holds the current left indentation, but needs to be corrected by \labelwidth. The command \parshape n a_1 b_1 a_2 b_2 ... means that the first line will have left margin a_1 and length b_1 etc. The nth specification applies to each of the remaining lines in the paragraph. So to flushright margins, b_k has to be \textwidth - a_k. I have introduced a new length \continueindent; in the code below it is initially set to \algorithmicindent, but you might want to choose a different value.

Ideally one would want to just patch the definition of the algorithmic environment, but unfortunately etoolbox reports that it can't do this. So the code below copies the (long) definition from algorithmic.sty and just modifies the definition of \ALC@it, see the lines after the commend %% Added three lines here %%:

Sample output

\documentclass[]{memoir}
\usepackage[utf8x]{inputenc}
\usepackage{amssymb}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{algorithmic}

\newcommand{\algorithmname}{Algorithm}
\newcommand{\listalgorithmname}{List of Algorithms}
\newlistof{listofalgorithms}{loa}{\listalgorithmname}
\newfloat{algorithm}{loa}{\algorithmname}
\newfixedcaption{\falgcaption}{algorithm}
\newlistentry{algorithm}{loa}{0}  

\usepackage{float}
\floatstyle{ruled}
\restylefloat{algorithm}

\makeatletter
\newlength{\continueindent}
\setlength{\continueindent}{\algorithmicindent}
\renewenvironment{algorithmic}[1][0]{
\setcounter{ALC@depth}{\@listdepth}%
\let\@listdepth\c@ALC@depth%
\let\@item\ALC@item%
  \newcommand{\ALC@lno}{%
\ifthenelse{\equal{\arabic{ALC@rem}}{0}}
{{\ALC@linenosize \arabic{ALC@line}\ALC@linenodelimiter}}{}%
}
\let\@listii\@listi
\let\@listiii\@listi
\let\@listiv\@listi
\let\@listv\@listi
\let\@listvi\@listi
\let\@listvii\@listi
  \newenvironment{ALC@g}{
    \begin{list}{\ALC@lno}{ \itemsep\z@ \itemindent\z@
    \listparindent\z@ \rightmargin\z@
    \topsep\z@ \partopsep\z@ \parskip\z@\parsep\z@
    \leftmargin \algorithmicindent%1em
    \addtolength{\ALC@tlm}{\leftmargin}
    }
  }
  {\end{list}}
  \newcommand{\ALC@it}{%
    \stepcounter{ALC@rem}%
    \ifthenelse{\equal{\arabic{ALC@rem}}{#1}}{\setcounter{ALC@rem}{0}}{}%
    \stepcounter{ALC@line}%
    \refstepcounter{ALC@unique}%
    \item\def\@currentlabel{\theALC@line}%
    %% Added four lines here %%
    \parshape 2 \dimexpr\ALC@tlm+\labelwidth\relax
    \dimexpr\textwidth-\ALC@tlm-\labelwidth\relax 
    \dimexpr\ALC@tlm+\labelwidth+\continueindent\relax
    \dimexpr\textwidth-\ALC@tlm-\labelwidth-\continueindent\relax
    %% End of added material
  }
  \newcommand{\ALC@com}[1]{\ifthenelse{\equal{##1}{default}}%
{}{\ \algorithmiccomment{##1}}}
  \newcommand{\REQUIRE}{\item[\algorithmicrequire]}
  \newcommand{\ENSURE}{\item[\algorithmicensure]}
  \newcommand{\PRINT}{\ALC@it\algorithmicprint{} \ }
  \newcommand{\RETURN}{\ALC@it\algorithmicreturn{} \ }
  \newcommand{\TRUE}{\algorithmictrue{}}
  \newcommand{\FALSE}{\algorithmicfalse{}}
  \newcommand{\AND}{\algorithmicand{} }
  \newcommand{\OR}{\algorithmicor{} }
  \newcommand{\XOR}{\algorithmicxor{} }
  \newcommand{\NOT}{\algorithmicnot{} }
  \newcommand{\TO}{\algorithmicto{} }
  \newcommand{\STATE}{\ALC@it}
  \newcommand{\STMT}{\ALC@it}
  \newcommand{\COMMENT}[1]{\algorithmiccomment{##1}}
  \newenvironment{ALC@inputs}{\begin{ALC@g}}{\end{ALC@g}}
  \newenvironment{ALC@outputs}{\begin{ALC@g}}{\end{ALC@g}}
  \newenvironment{ALC@globals}{\begin{ALC@g}}{\end{ALC@g}}
  \newenvironment{ALC@body}{\begin{ALC@g}}{\end{ALC@g}}
  \newenvironment{ALC@if}{\begin{ALC@g}}{\end{ALC@g}}
  \newenvironment{ALC@for}{\begin{ALC@g}}{\end{ALC@g}}
  \newenvironment{ALC@whl}{\begin{ALC@g}}{\end{ALC@g}}
  \newenvironment{ALC@loop}{\begin{ALC@g}}{\end{ALC@g}}
  \newenvironment{ALC@rpt}{\begin{ALC@g}}{\end{ALC@g}}
  \renewcommand{\\}{\@centercr}
  \newcommand{\INPUTS}[1][default]{\ALC@it\algorithmicinputs\ \ALC@com{##1}\begin{ALC@inputs}}
  \newcommand{\ENDINPUTS}{\end{ALC@inputs}}
  \newcommand{\OUTPUTS}[1][default]{\ALC@it\algorithmicoutputs\ \ALC@com{##1}\begin{ALC@outputs}}
  \newcommand{\ENDOUTPUTS}{\end{ALC@outputs}}
  \newcommand{\GLOBALS}{\ALC@it\algorithmicglobals\ }
  \newcommand{\BODY}[1][default]{\ALC@it\algorithmicbody\ \ALC@com{##1}\begin{ALC@body}}
  \newcommand{\ENDBODY}{\end{ALC@body}}
  \newcommand{\IF}[2][default]{\ALC@it\algorithmicif\ ##2\ \algorithmicthen%
\ALC@com{##1}\begin{ALC@if}}
  \newcommand{\ELSE}[1][default]{\end{ALC@if}\ALC@it\algorithmicelse%
\ALC@com{##1}\begin{ALC@if}}
  \newcommand{\ELSIF}[2][default]%
{\end{ALC@if}\ALC@it\algorithmicelsif\ ##2\ \algorithmicthen%
\ALC@com{##1}\begin{ALC@if}}
  \newcommand{\FOR}[2][default]{\ALC@it\algorithmicfor\ ##2\ \algorithmicdo%
\ALC@com{##1}\begin{ALC@for}}
  \newcommand{\FORALL}[2][default]{\ALC@it\algorithmicforall\ ##2\ %
\algorithmicdo%
\ALC@com{##1}\begin{ALC@for}}
  \newcommand{\WHILE}[2][default]{\ALC@it\algorithmicwhile\ ##2\ %
\algorithmicdo%
\ALC@com{##1}\begin{ALC@whl}}
  \newcommand{\LOOP}[1][default]{\ALC@it\algorithmicloop%
\ALC@com{##1}\begin{ALC@loop}}
  \newcommand{\REPEAT}[1][default]{\ALC@it\algorithmicrepeat%
\ALC@com{##1}\begin{ALC@rpt}}
  \newcommand{\UNTIL}[1]{\end{ALC@rpt}\ALC@it\algorithmicuntil\ ##1}
  \ifthenelse{\boolean{ALC@noend}}{
    \newcommand{\ENDIF}{\end{ALC@if}}
    \newcommand{\ENDFOR}{\end{ALC@for}}
    \newcommand{\ENDWHILE}{\end{ALC@whl}}
    \newcommand{\ENDLOOP}{\end{ALC@loop}}
  }{
    \newcommand{\ENDIF}{\end{ALC@if}\ALC@it\algorithmicendif}
    \newcommand{\ENDFOR}{\end{ALC@for}\ALC@it\algorithmicendfor}
    \newcommand{\ENDWHILE}{\end{ALC@whl}\ALC@it\algorithmicendwhile}
    \newcommand{\ENDLOOP}{\end{ALC@loop}\ALC@it\algorithmicendloop}
  }
  \renewcommand{\@toodeep}{}
  \begin{list}{\ALC@lno}{\setcounter{ALC@rem}{0}\setcounter{ALC@line}{0}%
    \itemsep\z@ \itemindent\z@ \listparindent\z@%
    \partopsep\z@ \parskip\z@ \parsep\z@%
    \labelsep 0.5em \topsep 0.2em%
\ifthenelse{\equal{#1}{0}}
  {\labelwidth 0.5em }
  {\labelwidth  1.2em }
\leftmargin\labelwidth \addtolength{\leftmargin}{\labelsep}
    \ALC@tlm\labelsep
  }
}
{\end{list}}
\makeatother

\begin{document}

\chapter{Example Chapter}
\section{Example Section}
\begin{algorithm}[htbp]
\caption{An example algorithm}
\label{alg1}
\begin{algorithmic}
\REQUIRE Lorem ipsum dolor sit amet, consectetur adipiscing
elit. Integer lobortis luctus tincidunt. Ut commodo, erat quis tempor
cursus, felis lectus.  
\ENSURE Lorem ipsum dolor sit amet, consectetur adipiscing
elit. Integer lobortis luctus tincidunt. Ut commodo, erat quis tempor
cursus, felis lectus.  
\STATE Statement 1: Lorem ipsum dolor sit amet, consectetur adipiscing 
elit. Integer lobortis luctus tincidunt. Ut commodo, erat quis tempor
cursus, felis lectus. \COMMENT{This is a comment}
\REPEAT
\STATE Statement 2: Lorem ipsum dolor sit amet, consectetur adipiscing
elit. Integer lobortis luctus tincidunt. Ut commodo, erat quis tempor
cursus, felis lectus.  
\STATE Statement 3:  Lorem ipsum dolor sit amet, consectetur
adipiscing elit. Integer lobortis luctus tincidunt. Ut commodo, erat
quis tempor cursus, felis lectus.  
\FOR{$i=0$ to $k$}
\STATE Statement 4:  Lorem ipsum dolor sit amet, consectetur
adipiscing elit. Integer lobortis luctus tincidunt. Ut commodo, erat
quis tempor cursus, felis lectus.  
\STATE Statement 5: Lorem ipsum dolor sit amet, consectetur adipiscing
elit. Integer lobortis luctus tincidunt. Ut commodo, erat quis tempor
cursus, felis lectus.  
\ENDFOR
\UNTIL{Lorem ipsum dolor sit amet}
\STATE Statement 6: Lorem ipsum dolor sit amet, consectetur adipiscing
elit. Integer lobortis luctus tincidunt. Ut commodo, erat quis tempor
cursus, felis lectus.  
\end{algorithmic}
\end{algorithm}
\end{document}
share|improve this answer
    
thank you so much!!!! ;-D –  entropy Feb 23 '13 at 22:12

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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