1

I have some pseudocode for an algorithm, which appears okay (with some minor errors in compilation). But the biggest problem is that the numbering of the steps does not match the number of steps in the algorithm. I have an MWE below.

\documentclass[conference]{IEEEtran}
\usepackage{cite}
\usepackage{amsmath,amssymb,amsfonts}
\usepackage{graphicx}
\usepackage{textcomp}
\usepackage{xcolor}
\usepackage{algorithm}
\usepackage{algorithmic}
\usepackage{tabulary}
\usepackage{algpseudocode}
\usepackage{booktabs}
\usepackage{cleveref}
\usepackage{enumitem}
\usepackage{subcaption}
\usepackage{amsmath,amssymb,amsthm,bbm,color,psfrag,amsfonts}
\algrenewcommand\algorithmicrequire{\textbf{Input:}}
\algrenewcommand\algorithmicensure{\textbf{Output:}}
\usepackage{url}
\newtheorem{theorem}{Theorem}
\newtheorem{lemma}{Lemma}
\newtheorem{asu}{Assumption}
\newtheorem{remark}{Remark}
\theoremstyle{definition}
\newtheorem{definition}{Definition}[section]
\crefname{lemma}{Lemma}{Lemmas}
\def\BibTeX{{\rm B\kern-.05em{\sc i\kern-.025em b}\kern-.08em
    T\kern-.1667em\lower.7ex\hbox{E}\kern-.125emX}}
\algrenewcommand\algorithmicrequire{\textbf{Input:}}
\algrenewcommand\algorithmicensure{\textbf{Output:}}
\begin{document}
 \begin{algorithm}[H]
 \caption{My Algorithm}\label{alg:greedy}
 \begin{algorithmic}[1]
 \renewcommand{\algorithmicrequire}{\textbf{Input:}}
 \renewcommand{\algorithmicensure}{\textbf{Output:}}
 \Require $M^0=4$.
 \Ensure $V,\text{found}$  
  \WHILE{$P^l \neq \emptyset$}
  \STATE{state}
    \For{$i \in 1....|N|$} 
        \State{Compute }
    \EndFor \label{nio}
    \State Get $\theta$ \%. \label{tre}
    \State $G \gets$ Find vals$) \label{find_vals}
    \State $T,Y^{m+1} \gets $ \Call{Otherfunc}{$I$} \label{p}
 \ENDWHILE
 \end{algorithmic}
 \end{algorithm}
\end{document}

You'll notice that the number "2" appears many times. Any help regarding how to fix the numbering would be appreciated. Any ideas?

P.S. You may notice that this is the IEEE template, in case that helps.

2
  • Do you need all thos algo-packages? I.e. why don't they interact unintendedly?
    – MS-SPO
    Commented Apr 22 at 18:47
  • apart from multiple conflicting packages, why define \renewcommand{\algorithmicrequire}{\textbf{Input:}} three times, or specify amsfonts twice, and specify both xcolor and color. loading psfrag stops you using pdflatex. If you just load packages that you use, such conflicts are far less likely Commented Apr 22 at 18:54

1 Answer 1

1

You should use either algorithmic or algpseudocode, not both. You're mixing the syntax of both packages and you obviously get errors.

Here's a correct version, where packages and definitions are not duplicate.

I'd point out that

  1. caption (and so subcaption) cannot be used with IEEEtran. If you need subfloats, load \usepackage[caption=false]{subfig} and check the syntax of this package;

  2. textcomp is no longer necessary

  3. psfrag is not compatible with pdflatex

\documentclass[conference]{IEEEtran}
\usepackage{cite}
\usepackage{amsmath,amssymb,amsthm}
\usepackage{graphicx}
%\usepackage{textcomp}% no longer necessary
\usepackage{xcolor}
\usepackage{algorithm}
%\usepackage{algorithmic}
\usepackage{algpseudocode}
\usepackage{booktabs}
\usepackage{enumitem}
%\usepackage{subcaption}% not with IEEEtran
\usepackage{url}
\usepackage{cleveref}

\algrenewcommand\algorithmicrequire{\textbf{Input:}}
\algrenewcommand\algorithmicensure{\textbf{Output:}}

\newtheorem{theorem}{Theorem}
\newtheorem{lemma}{Lemma}
\newtheorem{asu}{Assumption}
\newtheorem{remark}{Remark}
\theoremstyle{definition}
\newtheorem{definition}{Definition}[section]
\crefname{lemma}{Lemma}{Lemmas}


\begin{document}

\begin{algorithm}[H]
\caption{My Algorithm}\label{alg:greedy}
\begin{algorithmic}[1]
\Require $M^0=4$.
\Ensure $V,\text{found}$  
\While{$P^l \neq \emptyset$}
  \State state
  \For{$i \in 1\dots|N|$} 
    \State Compute
  \EndFor \label{nio}
  \State Get $\theta$ \%. \label{tre}
  \State $G \gets$ Find vals \label{find_vals}
  \State $T,Y^{m+1} \gets $ \Call{Otherfunc}{$I$} \label{p}
\EndWhile
\end{algorithmic}
\end{algorithm}

\end{document}

enter image description here

I'd also recommend to load \usepackage{newtx} (and to remove amssymb that's already covered by the former). Here's the output you get, with math in Times-like fonts. Here the first few lines to see how the package is called (after amsthm).

\documentclass[conference]{IEEEtran}
\usepackage{cite}
\usepackage{amsmath,amsthm}
\usepackage{newtx}

enter image description here

2
  • I'm confused why you removed \usepackage{algorithmic}, and yet \begin{algorithmic}[1] appears in your code. Can you tell me why/how that works? Commented Apr 23 at 7:20
  • @underdog987 algorithmic and algpseudocode both have the algorithmic environment; they’re alternative packages with different styles (and syntax).
    – egreg
    Commented Apr 23 at 7:23

You must log in to answer this question.

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