0

IEEEtran suggests do not use the algorithm floating environment. So I have been trying to mimic the original look of algorithm without success.

Here is my algorithm with algorithm floating environment:

enter image description here

\begin{algorithm}[!t]
 \caption{my algorithm}
 \label{alg: alg}
\begin{algorithmic}[1]
 \renewcommand{\algorithmicrequire}{\textbf{Input:}}
 \renewcommand{\algorithmicensure}{\textbf{Output:}}
 \REQUIRE input
 \ENSURE  output \\
\STATE $i\gets 10$
\IF {$i\geq 5$} 
  \STATE $i\gets i-1$
\ELSE
  \IF {$i\leq 3$}
    \STATE $i\gets i+2$
  \ENDIF
\ENDIF 
\end{algorithmic}
 \end{algorithm}

However, if I use figure enviroment, the algorithm looks so ugly.

enter image description here

\begin{figure}[!t]
 \caption{my algorithm}
 \label{alg: alg}
\begin{algorithmic}[1]
 \renewcommand{\algorithmicrequire}{\textbf{Input:}}
 \renewcommand{\algorithmicensure}{\textbf{Output:}}
 \REQUIRE input
 \ENSURE  output \\
\STATE $i\gets 10$
\IF {$i\geq 5$} 
  \STATE $i\gets i-1$
\ELSE
  \IF {$i\leq 3$}
    \STATE $i\gets i+2$
  \ENDIF
\ENDIF 
\end{algorithmic}
 \end{figure}

How can I generate the algorithm like the first one without violiting IEEEtran rules?

Here is the full code:

\documentclass[journal]{IEEEtran}
\usepackage{amsmath,amsfonts}
\usepackage{algorithmic}
\usepackage{algorithm}
\usepackage{array}
\usepackage[]{subfig}
\usepackage{stfloats}
\usepackage{graphicx}
\usepackage{mathtools}
\usepackage{newtxtext, newtxmath}
\usepackage{lipsum}




\begin{document}

\lipsum[1]
\begin{algorithm}[!t]
 \caption{my algorithm}
 \label{alg: alg}
\begin{algorithmic}[1]
 \renewcommand{\algorithmicrequire}{\textbf{Input:}}
 \renewcommand{\algorithmicensure}{\textbf{Output:}}
 \REQUIRE input
 \ENSURE  output \\
\STATE $i\gets 10$
\IF {$i\geq 5$} 
  \STATE $i\gets i-1$
\ELSE
  \IF {$i\leq 3$}
    \STATE $i\gets i+2$
  \ENDIF
\ENDIF 
\end{algorithmic}
 \end{algorithm}
 
\lipsum[2]
\begin{figure}[!t]
 \caption{my algorithm}
 \label{alg: alg}
\begin{algorithmic}[1]
 \renewcommand{\algorithmicrequire}{\textbf{Input:}}
 \renewcommand{\algorithmicensure}{\textbf{Output:}}
 \REQUIRE input
 \ENSURE  output \\
\STATE $i\gets 10$
\IF {$i\geq 5$} 
  \STATE $i\gets i-1$
\ELSE
  \IF {$i\leq 3$}
    \STATE $i\gets i+2$
  \ENDIF
\ENDIF 
\end{algorithmic}
 \end{figure}
\end{document}

1 Answer 1

1

algorithm package was not allowed in the past. This is stated as follows in the file bare_adv.tex.

Do NOT use the algorithm floating environment provided by algorithm.sty (by the same authors) or algorithm2e.sty (by Christophe Fiorio) as the IEEE does not use dedicated algorithm float types and packages that provide these will not provide correct IEEE style captions.

Now IEEE allows algorithm package. You can choose any journal template in IEEE-Template Selector. For example, IEEE Internet of Things Journal template, we open the bare_jrnl_new_sample4.tex in it. You can see the following code.

\documentclass[lettersize,journal]{IEEEtran}
\usepackage{amsmath,amsfonts}
\usepackage{algorithmic}
\usepackage{algorithm}
...
\begin{algorithm}[H]
\caption{Weighted Tanimoto ELM.}\label{alg:alg1}
\begin{algorithmic}
\STATE
\STATE {\textsc{TRAIN}}$(\mathbf{X} \mathbf{T})$
\STATE \hspace{0.5cm}$ \textbf{select randomly } W \subset \mathbf{X}  $
\STATE \hspace{0.5cm}$ N_\mathbf{t} \gets | \{ i : \mathbf{t}_i = \mathbf{t} \} | $ \textbf{ for } $ \mathbf{t}= -1,+1 $
\STATE \hspace{0.5cm}$ B_i \gets \sqrt{ \textsc{max}(N_{-1},N_{+1}) / N_{\mathbf{t}_i} } $ \textbf{ for } $ i = 1,...,N $
\STATE \hspace{0.5cm}$ \hat{\mathbf{H}} \gets  B \cdot (\mathbf{X}^T\textbf{W})/( \mathbb{1}\mathbf{X} + \mathbb{1}\textbf{W} - \mathbf{X}^T\textbf{W} ) $
\STATE \hspace{0.5cm}$ \beta \gets \left ( I/C + \hat{\mathbf{H}}^T\hat{\mathbf{H}} \right )^{-1}(\hat{\mathbf{H}}^T B\cdot \mathbf{T})  $
\STATE \hspace{0.5cm}\textbf{return}  $\textbf{W},  \beta $
\STATE
\STATE {\textsc{PREDICT}}$(\mathbf{X} )$
\STATE \hspace{0.5cm}$ \mathbf{H} \gets  (\mathbf{X}^T\textbf{W} )/( \mathbb{1}\mathbf{X}  + \mathbb{1}\textbf{W}- \mathbf{X}^T\textbf{W}  ) $
\STATE \hspace{0.5cm}\textbf{return}  $\textsc{sign}( \mathbf{H} \beta )$
\end{algorithmic}
\label{alg1}
\end{algorithm}
...
\end{document}

So, there is an example for you. I suggest you use algpseudocodex package. And I adjusted some styles according to what my editor modified. I hope it will be helpful to you.

\documentclass{IEEEtran}
\usepackage[commentColor=black]{algpseudocodex}
\tikzset{algpxIndentLine/.style={draw=black}}
\algrenewcommand{\alglinenumber}[1]{\bfseries\footnotesize #1}
\algrenewcommand{\textproc}{}
\algrenewcommand{\algorithmicrequire}{\textbf{Input:}}
\algrenewcommand{\algorithmicensure}{\textbf{Output:}}
\usepackage{algorithm}
\floatplacement{algorithm}{tbp}
\makeatletter
\newcommand{\algorithmname}{\ALG@name}
\renewcommand{\floatc@ruled}[2]{{\@fs@cfont #1:} #2\par}
\makeatother
\begin{document}
\begin{algorithm}
\caption{Algorithm}
\label{alg}
\begin{algorithmic}[1]
\Require input
\Ensure  output
\State $i\gets 10$
\If {$i\geq 5$}
    \State $i\gets i-1$
\ElsIf{$i\leq 3$}
    \State $i\gets i+2$
\EndIf
\end{algorithmic}
\end{algorithm}
\end{document}

enter image description here

Default style

\documentclass{IEEEtran}
\usepackage{algpseudocodex}
\algrenewcommand{\algorithmicrequire}{\textbf{Input:}}
\algrenewcommand{\algorithmicensure}{\textbf{Output:}}
\usepackage{algorithm}
\begin{document}
\begin{algorithm}
\caption{Algorithm}
\label{alg}
\begin{algorithmic}[1]
\Require input
\Ensure  output
\State $i\gets 10$
\If {$i\geq 5$}
    \State $i\gets i-1$
\ElsIf{$i\leq 3$}
    \State $i\gets i+2$
\EndIf
\end{algorithmic}
\end{algorithm}
\end{document}

enter image description here

3
  • Thanks, it looks nicer. You said that "my editor modified", did it do it automatically or you have to manually edit it?
    – emnha
    Sep 12, 2022 at 5:46
  • @emnha I started with the default style. When my paper was published, I found that the publisher helped me modify some styles. I adjusted the code by myself in the later period according to the published styles as shown in example abobe. You can compare the default appearance of algpseudocodex and algorithm packages with the example I gave.
    – Clara
    Sep 12, 2022 at 5:57
  • @emnha I have added my answer of defaut style of algpseudocodex and algorithm packages.
    – Clara
    Sep 12, 2022 at 6:21

You must log in to answer this question.

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