16

I want to type an algorithm in ieee format. I have used algorithmic package. But using that package, I couldn't type a caption to the algorithm and "Algorithm " above the written algorithm. What should I do to get those things written in my algorithm ? I need the algorithm as in the given figure.enter image description here

The structure I have used is given below

 \documentclass{IEEEtran}
 \usepackage{algorithmic}
 \begin{document}
 %\begin{algorithm}
 %\caption{Algorithm for ...}
 \begin{algorithmic}[1]
 \renewcommand{\algorithmicrequire}{\textbf{Input:}}
 \renewcommand{\algorithmicensure}{\textbf{Output:}}
 \REQUIRE in
 \ENSURE  out
 \\ \textit{Initialisation} :
  \STATE first statement
 \\ \textit{LOOP Process}
  \FOR {$i = l-2$ to $0$}
  \STATE statements..
  \IF {($i \ne 0$)}
  \STATE statement..
  \ENDIF
  \ENDFOR
 \RETURN $P$ 
 \end{algorithmic} 
 %\end{algorithm}
 \end{document}
3
  • @vijita: \caption does not work outside of a floating environment ... algoritmic seems to be is a non-floating environment, but perhaps, there are package options to 'cure' this
    – user31729
    Dec 29, 2014 at 9:23
  • @ChristianHupfer IEEE format is not compatible with the floating environment. Yes the \caption{} is not working \begin{algorithm} also. But I need that. That is why I have commented those things. Which package I have to use? can you please suggest any.
    – vijita
    Dec 29, 2014 at 9:30
  • @vijita: I found the error, but Herbert was quicker in posting the same solution, see his answer and consider to accept it please, since it works ;-)
    – user31729
    Dec 29, 2014 at 9:32

2 Answers 2

18

First of all, if you want to use the algorithm environment and also want to respect the IEEE format (which doesn't allow it to float), you can use the H floating specifier to tell algorithm not to float:

\begin{algorithm}[H]

Then, it seems that you want to use the ruled style for the algorithm environment, but without lines.

This can be achieved defining a new floating style (the algorithm package loads the float package), let's say norules, which has no rules

\makeatletter
\newcommand\fs@norules{\def\@fs@cfont{\bfseries}\let\@fs@capt\floatc@ruled
  \def\@fs@pre{}%
  \def\@fs@post{}%
  \def\@fs@mid{\kern3pt}%
  \let\@fs@iftopcapt\iftrue}
\makeatother

and applying it to the algorithm environment

\floatstyle{norules}
\restylefloat{algorithm}

MWE:

\documentclass{IEEEtran}

\usepackage{algorithm}
\usepackage{algorithmic}

\makeatletter
\newcommand\fs@norules{\def\@fs@cfont{\bfseries}\let\@fs@capt\floatc@ruled
  \def\@fs@pre{}%
  \def\@fs@post{}%
  \def\@fs@mid{\kern3pt}%
  \let\@fs@iftopcapt\iftrue}
\makeatother
\floatstyle{norules}
\restylefloat{algorithm}

\begin{document}

 \begin{algorithm}[H]
 \caption{Algorithm for ...}
 \begin{algorithmic}[1]
 \renewcommand{\algorithmicrequire}{\textbf{Input:}}
 \renewcommand{\algorithmicensure}{\textbf{Output:}}
 \REQUIRE in
 \ENSURE  out
 \\ \textit{Initialisation} :
  \STATE first statement
 \\ \textit{LOOP Process}
  \FOR {$i = l-2$ to $0$}
  \STATE statements..
  \IF {($i \ne 0$)}
  \STATE statement..
  \ENDIF
  \ENDFOR
 \RETURN $P$
 \end{algorithmic}
 \end{algorithm}

\end{document} 

Output:

enter image description here

5
  • for helping me. :)
    – vijita
    Dec 29, 2014 at 18:19
  • @vijita You're welcome. Anyway it seems that Herbert's answer was what you were lookin for ;-) Dec 29, 2014 at 18:21
  • :-) I was lookin for algorithm in IEEE format. I think herbert's ans also gives algorithm in IEEE format and I 've made it as ans before ur post.
    – vijita
    Dec 29, 2014 at 18:37
  • @vijita Happy you've solved, anyway. Dec 29, 2014 at 18:39
  • :) me too.. of course with the help of u people... :)
    – vijita
    Dec 29, 2014 at 18:47
13

Load also algorithm:

\documentclass{IEEEtran}
\usepackage{algorithm,algorithmic}
\begin{document}
 \begin{algorithm}
 \caption{Algorithm for ...}
 \begin{algorithmic}[1]
 \renewcommand{\algorithmicrequire}{\textbf{Input:}}
 \renewcommand{\algorithmicensure}{\textbf{Output:}}
 \REQUIRE in
 \ENSURE  out
 \\ \textit{Initialisation} :
  \STATE first statement
 \\ \textit{LOOP Process}
  \FOR {$i = l-2$ to $0$}
  \STATE statements..
  \IF {($i \ne 0$)}
  \STATE statement..
  \ENDIF
  \ENDFOR
 \RETURN $P$ 
 \end{algorithmic} 
 \end{algorithm}

\end{document}

enter image description here

3
  • Thank you @Herbert . Can I remove that lines in between?
    – vijita
    Dec 29, 2014 at 9:39
  • 1
    After loading package algorithm write \floatstyle{plaintop} \restylefloat{algorithm}
    – user2478
    Dec 29, 2014 at 10:33
  • By default the IEEEtran centers a caption if it is shorter than a line
    – user2478
    Dec 29, 2014 at 10:45

You must log in to answer this question.

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