9

Can you suggest me what is wrong with using return command in algorithms. when I add

\RETURN $C

to my LaTeX document I get

return C =0,

what I need is just return C.

\begin{algorithm}                      % enter the algorithm environment
  \caption{ lloyd's kmeans}      
  \label{alg1}                          
  \begin{algorithmic}[1]                    % enter the algorithmienvironment
    \RETURN{$C$}
  \end{algorithmic}
\end{algorithm}

I get two extra characters =0. I wonder where its coming from. if it helps,there is "Missing number, treated as zero",and Missing = inserted for \ifnum. in the error log generated

user14372
  • 193
  • yes, i also checked it and it doesn't reproduce the problem when i run it alone. However, i still get the problem when I add it to my doc. – user14372 Nov 29 '13 at 20:50
  • if it helps,there is "Missing number, treated as zero", in the error log generated – user14372 Nov 29 '13 at 21:01
  • 1
    Do you define \RETURN somewhere else in your document? For example, what does \show\RETURN put in your .log when you place it just before this algorithm in your code? Also, what version of the packages are you using? You can find this out by following Which package version am I using? – Werner Nov 29 '13 at 21:03
  • @Werner yes, the problem still exists even after removing \return. i get =0 at end of my algo. – user14372 Nov 29 '13 at 21:10

3 Answers3

8

You're probably combining the use of algorithmic and algorithmicx package. The latter can be used as a full replacement of the former, so you should not load them together (the error is reproduced when they are loaded in sequence \usepackage{algorithmic,algcompatible}). I would suggest using it in the following way:

enter image description here

\documentclass{article}

\usepackage{algorithm}% http://ctan.org/pkg/algorithms
\usepackage{algcompatible}% http://ctan.org/pkg/algorithmicx

% Define keyword and construction of \RETURN
\algnewcommand\algorithmicreturn{\textbf{return}}
\algnewcommand\RETURN{\State \algorithmicreturn}%
\begin{document}
\begin{algorithm}
  \caption{My algorithm}\label{alg1}                          
  \begin{algorithmic}[1]
    \RETURN{} $C$
  \end{algorithmic}
\end{algorithm}
\end{document}

This requires you to define the \RETURN keyword to function properly. There may be others as well that you need to define to work properly. However, they can be done in the same way.


Based on the code you posted, the following might be a way of defining your algorithm structures:

enter image description here

\documentclass{article}
\usepackage{amsmath}% http://ctan.org/pkg/amsmath
\usepackage{algorithm,algcompatible}% http://ctan.org/pkg/{algorithms,algorithmicx}
\algnewcommand\algorithmicreturn{\textbf{return}}
\algnewcommand\RETURN{\algorithmicreturn}
\algnewcommand\algorithmicprocedure{\textbf{procedure}}
\algnewcommand\PROCEDURE{\item[\algorithmicprocedure]}%
\algnewcommand\algorithmicendprocedure{\textbf{end procedure}}
\algnewcommand\ENDPROCEDURE{\item[\algorithmicendprocedure]}%
\algnewcommand{\algvar}[1]{{\text{\ttfamily\detokenize{#1}}}}
\algnewcommand{\algarg}[1]{{\text{\ttfamily\itshape\detokenize{#1}}}}
\algnewcommand{\algproc}[1]{{\text{\ttfamily\detokenize{#1}}}}
\algnewcommand{\algassign}{\leftarrow}
\begin{document}
\begin{algorithm}% enter the algorithm environment
\caption{procedure DOWNLOAD}% give the algorithm a caption
\label{SaS:DOWNLOAD}
\begin{algorithmic}[1]% enter the algorithmic environment
\raggedright
\PROCEDURE \algvar{user_request}(\algarg{follower_id}, \algarg{friend_id}, \algarg{content_id}, \algarg{home_server}, \algarg{friend_surrogate})
  \REQUIRE \algarg{user_id}, \algarg{home_server}
  \ENSURE URL of requested content
  \STATE $\algvar{content} \algassign \algproc{request_redirect}(\algarg{content_id})$
  \IF{$\algvar{content} \neq \algvar{server}$}
    \STATE \algproc{schedule_pull}(\algarg{content_id}, \algvar{server})
  \ENDIF
 \STATE \RETURN{} \algvar{content_host}
\ENDPROCEDURE
\end{algorithmic}
\end{algorithm}
\end{document}

In an attempt to be consistent, define macros that would handle formatting of like items.

Werner
  • 603,163
  • @warner thanks, but i get this error LaTeX Error: Something's wrong--perhaps a missing \item. – user14372 Nov 29 '13 at 23:00
  • 1
    @user14372: Can you post your entire algorithm somewhere (like Pastebin? That way I could copy-and-paste it in my editor and try to replicate your problem (and help). – Werner Nov 29 '13 at 23:02
  • http://pastebin.com/embed_js.php?i=WhWiMDDW – user14372 Nov 29 '13 at 23:07
  • @user14372: I don't receive an error like that, although you may want to have \REQUIRE $k \geq 0$ \STATE select k points as initial centroids. Are you only loading algorithm and algcompatible (not also algorithmic)? – Werner Nov 29 '13 at 23:16
  • I am working on existing document, and it loads the following \usepackage[english]{babel} % o italian \usepackage[latin1]{inputenc} \usepackage{algorithm} \usepackage{algorithmic} %\usepackage{algcompatible}% http://ctan.org/pkg/algorithmicx \usepackage{algpseudocode} %\usepackage{inputenc} \usepackage{graphicx} %%\usepackage[a-1b]{pdfx} %%\usepackage{topfront} %\usepackage{amsfonts} %\usepackage{latexsym} \usepackage{amssymb} \usepackage{amsthm} \usepackage{amsmath} \usepackage{float} \usepackage{multirow} \usepackage{hyperref} – user14372 Dec 01 '13 at 09:32
  • @user14372: Remove the loading of algorithmic. Also, do you use algpseudocode? If not, remove that as well. – Werner Dec 01 '13 at 14:57
  • thanks for the follow up. i use algpseudocode to use procedure \Procedure{download}{$time, server$} . can you tell me a similar one, or help me define one for same goal. – user14372 Dec 01 '13 at 17:07
  • @user14372: Look at the definitions within algpseudocode.sty and copy them into your document preamble. algcompatible.sty does not define \Procedure. You may also wish to ask a follow-up question... – Werner Dec 01 '13 at 18:21
  • i Still get that annoying zero, please look at here http://pastebin.com/embed_js.php?i=qDr1x9mZ – user14372 Dec 01 '13 at 19:18
  • @user14372: I've added some code to my answer. – Werner Dec 02 '13 at 04:03
2

I had the same problem in my code. I was using the algpseudocode package.

Original: Appends 0 at the end of return

\usepackage{algorithmic}               
\usepackage{algorithm}   
\usepackage{algpseudocode}  %This introduces extra zero at the end of algorithm 

Solution: Does not append 0 at the end of return after removing the package algpseudocode:

\usepackage{algorithmic}            
\usepackage{algorithm}
Mensch
  • 65,388
Raju
  • 29
  • 1
    Welcome to TeX.SE! Please add an compilable tex code to your answer and an screenshot of your result too for a fast proof! – Mensch Aug 28 '19 at 10:45
  • The algorithm and algorithmic (and algorithm2e, and...) packages serve more or less the same purpose, but give different look-and-feel, and will probably clash in internal implementation details, giving grief. For consistent look of your document(s), pick one (and live with it's quirks). – vonbrand Feb 16 '20 at 00:48
  • 2
    terrible answer this doesn't work. – Charlie Parker Dec 03 '21 at 23:05
1

I just removed that error by placing the packages in the following order:

\usepackage{algpseudocode}
%\usepackage{algorithmic} not required
\usepackage{algorithm}
  • This is a very old question which already has received an accepted answer, and what you're suggesting was already said above. – Miyase Jun 12 '22 at 20:46