2

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

  • 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
4

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.

  • @warner thanks, but i get this error LaTeX Error: Something's wrong--perhaps a missing \item. – user14372 Nov 29 '13 at 23:00
  • @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
  • @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}% 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 1 '13 at 9:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.