3

This is my algorithm:

 \usepackage{algorithm}
 \usepackage{algorithmic}
 \begin{document}
 \begin{algorithm}[H]
 \caption{My algo}
 \begin{algorithmic}[1]
 \STATE instruction 1
 \STATE instruction 2
 \end{algorithmic}
 \end{algorithm}
 \end{document}

How can I specify which instruction have applied this option: [1] ? enter image description here

I'd like to delete the number before "Début" and before "Fin", I'd like also add spacing before "ch" and "po"

1
  • The option [1] says number everyline. Changing it to [2] places line numbers on lines 2,4,6,... etc. Commented Oct 6, 2012 at 13:51

1 Answer 1

4

The optional argument to algorithmic ([1] in this case), provides the line numbered at every line. A numbering modulo 2 would be obtained using [2], and so forth.

Here's a slight variation of Is it possible to have connecting loop lines (like algorithm2e) in algorithmic? - using algpseudocode (from algorithmicx) - to insert Début and Fin as keywords. Others still need to be translated to French:

enter image description here

\documentclass{article}
\usepackage{algorithm}% http://ctan.org/pkg/algorithm
\usepackage{algpseudocode}% http://ctan.org/pkg/algorithmicx

\makeatletter
\renewcommand{\ALG@name}{Algorithme}% Algorithm in French
\algnewcommand{\Debut}{\Statex \textbf{D\'ebut}\pushindent}
\algnewcommand{\Fin}{\Statex \textbf{Fin}\popindent}

% This is the vertical rule that is inserted
\def\therule{\makebox[\algorithmicindent][l]{\hspace*{.5em}\vrule height .75\baselineskip depth .25\baselineskip}}%

\newtoks\therules% Contains rules
\therules={}% Start with empty token list
\def\appendto#1#2{\expandafter#1\expandafter{\the#1#2}}% Append to token list
\def\gobblefirst#1{% Remove (first) from token list
  #1\expandafter\expandafter\expandafter{\expandafter\@gobble\the#1}}%
\def\LState{\State\unskip\the\therules}% New line-state
\def\pushindent{\appendto\therules\therule}%
\def\popindent{\gobblefirst\therules}%
\def\printindent{\unskip\the\therules}%
\def\printandpush{\printindent\pushindent}%
\def\popandprint{\popindent\printindent}%

%      ***      DECLARED LOOPS      ***
% (from algpseudocode.sty)
\algdef{SE}[WHILE]{While}{EndWhile}[1]
  {\printandpush\textbf{Tant que} #1}
  {\popandprint\textbf{Fin Tant que}}%
\algdef{SE}[FOR]{For}{EndFor}[1]
  {\printandpush\algorithmicfor\ #1\ \algorithmicdo}
  {\popandprint\algorithmicend\ \algorithmicfor}%
\algdef{S}[FOR]{ForAll}[1]
  {\printindent\algorithmicforall\ #1\ \algorithmicdo}%
\algdef{SE}[LOOP]{Loop}{EndLoop}
  {\printandpush\algorithmicloop}
  {\popandprint\algorithmicend\ \algorithmicloop}%
\algdef{SE}[REPEAT]{Repeat}{Until}
  {\printandpush\algorithmicrepeat}[1]
  {\popandprint\algorithmicuntil\ #1}%
\algdef{SE}[IF]{If}{EndIf}[1]
  {\printandpush\algorithmicif\ #1\ \algorithmicthen}
  {\popandprint\algorithmicend\ \algorithmicif}%
\algdef{C}[IF]{IF}{ElsIf}[1]
  {\popandprint\pushindent\algorithmicelse\ \algorithmicif\ #1\ \algorithmicthen}%
\algdef{Ce}[ELSE]{IF}{Else}{EndIf}
  {\popandprint\pushindent\algorithmicelse}%
\algdef{SE}[PROCEDURE]{Procedure}{EndProcedure}[2]
   {\printandpush\algorithmicprocedure\ \textproc{#1}\ifthenelse{\equal{#2}{}}{}{(#2)}}%
   {\popandprint\algorithmicend\ \algorithmicprocedure}%
\algdef{SE}[FUNCTION]{Function}{EndFunction}[2]
   {\printandpush\algorithmicfunction\ \textproc{#1}\ifthenelse{\equal{#2}{}}{}{(#2)}}%
   {\popandprint\algorithmicend\ \algorithmicfunction}%
\makeatother

\begin{document}

\begin{algorithm}
  \caption{My algorithme}
  \begin{algorithmic}[1]
    \Debut
      \LState $r\gets a\bmod b$
      \While{$r\not=0$}\Comment{We have the answer if r is 0}
        \LState $a\gets b$
        \LState $b\gets r$
        \LState $r\gets a\bmod b$
      \EndWhile\label{euclidendwhile}
      \LState \textbf{Retour} $b$\Comment{The gcd is b}
    \Fin
  \end{algorithmic}
\end{algorithm}
\end{document}

\State provides a "regular line" (with number), while \Statex provides an unnumbered line. As such, I've defined both Début and Fin to be of \Statex.


Adding the macro

\def\LStatex{\Statex\unskip\the\therules}% New line-state

allows you to print an unnumbered line. Now you can use it as follows:

enter image description here

\begin{algorithm}
  \caption{My algorithme}
  \begin{algorithmic}[1]
    \Debut
      \LState $r\gets a\bmod b$
      \While{$r\not=0$}\Comment{We have the answer if r is 0}
        \LState $a\gets b$
        \LState Here is a very long expression
        \LStatex \expandafter\hskip\algorithmicindent\relax that has to go to a new line.
        \LState $r\gets a\bmod b$
      \EndWhile\label{euclidendwhile}
      \LState \textbf{Retour} $b$\Comment{The gcd is b}
    \Fin
  \end{algorithmic}
\end{algorithm}
7
  • Thank you for help,i'd like also add spacing before "ch" and "po" have you an idea please?
    – researcher
    Commented Oct 6, 2012 at 15:08
  • @researcher: Are you referring to vertical or horizontal spacing?
    – Werner
    Commented Oct 6, 2012 at 16:50
  • horizontal spacing
    – researcher
    Commented Oct 6, 2012 at 16:55
  • @researcher: I assume it's for just the single line? Alternatively, one can increase the entire algorithm indent...
    – Werner
    Commented Oct 6, 2012 at 17:05
  • in my algorithme i have a long instruction. "ch" is its end , how can i add spacing before "ch"
    – researcher
    Commented Oct 6, 2012 at 17:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .