2

Algorithms are getting numbered automatically but unable to get same for the functions.

In the following example, Algorithms are numbered as Algorithm 1 and Algorithm 2 by themselves but Functions didn't. Please advice.

\documentclass{ucbthesis}
\usepackage{graphicx}
\usepackage{subfig}
\usepackage{epstopdf}
\usepackage[ruled,vlined,linesnumbered]{algorithm2e}
\usepackage{algpseudocode}
\usepackage{tikz,pgfplots,pgfplotstable}
\usepackage{tikz-qtree}
\usepackage{forest}
\usepackage{lettrine}
\usepackage{mathtools}
\usepackage{lscape}
\usepackage{pgf-pie}
\usepackage{caption} 
\usetikzlibrary{positioning,shapes,arrows,shadows,patterns,intersections,calc,fit}
\usepackage{dashbox}
\usepackage{mathptmx}
\usepackage{xcolor}
\usepackage{hyperref}
\begin{document}
\begin{algorithm}
    \caption{First Algorithm}
    \For{$j \in V$}{
    }
\end{algorithm}

\begin{algorithm}
    \caption{Second Algorithm}
    \For{$j \in V$}{
    }
\end{algorithm}

\begin{function}        
    \label{function1}   
    \caption{First Function ()}     
    \For{$k \in R$}{

    }       
\end{function}  

\begin{function}    
    \label{function2}
    \caption{Second Function ()}    
    \For{$k \in R$}{    

    }       
\end{function}

I am refering to function \ref{function1} and function \ref{function2}

\end{document}

enter image description here

1 Answers1

5

What you need is the package option procnumbered, which “makes the procedure and function to be numbered as algorithm”. See Page 19 of the algorithm2e manual.

Added: In order to get the cross-reference right, you need to place \label after \caption as @egreg mentioned in the comments below. See also this short explanation by @PhilMiller and this longer explanation by @anon.

\documentclass{ucbthesis}
\usepackage[ruled,vlined,linesnumbered,procnumbered]{algorithm2e}
\usepackage{newtxtext}
\usepackage{newtxmath}

\begin{document}
\begin{algorithm}
    \caption{First Algorithm}
    \For{$j \in V$}{
    }
\end{algorithm}

\begin{algorithm}
    \caption{Second Algorithm}
    \For{$j \in V$}{
    }
\end{algorithm}

\begin{function}
    \caption{First Function ()}
    \label{function1}
    \For{$k \in R$}{
    }
\end{function}

\begin{function}
    \caption{Second Function ()}
    \label{function2}
    \For{$k \in R$}{
    }
\end{function}

I am refering to Function~\ref{function1} and Function~\ref{function2}.

\end{document}

Functions numbered

In case you want your functions to be numbered independently, then you need to set up a new counter for the function environment:

\documentclass{ucbthesis}
\usepackage[ruled,vlined,linesnumbered,procnumbered]{algorithm2e}
\usepackage{newtxtext}
\usepackage{newtxmath}
% Set up a new counter for function
\newcounter{function}
\usepackage{etoolbox}
% Change the original counter to the new counter
\makeatletter
\AtBeginEnvironment{function}{%
  \let\c@algocf\c@function
}
\makeatother

\begin{document}
\begin{algorithm}
    \caption{First Algorithm}
    \For{$j \in V$}{
    }
\end{algorithm}

\begin{algorithm}
    \caption{Second Algorithm}
    \For{$j \in V$}{
    }
\end{algorithm}

\begin{function}
    \caption{First Function ()}
    \label{function1}
    \For{$k \in R$}{
    }
\end{function}

\begin{function}
    \caption{Second Function ()}
    \label{function2}
    \For{$k \in R$}{
    }
\end{function}

I am refering to Function~\ref{function1} and Function~\ref{function2}.

\end{document}

Functions numbered independently

Please note that

  • if you are using caption, please consider using subcaption (which is distributed with caption), instead of subfig.
  • if you want to use Times-like fonts, consider replacing mathptmx with the more recent newtxtext and newtxmath.

Added: Please use non-breaking space ~ between a label name and its number.

Ruixi Zhang
  • 9,553
  • Thanks for your reply. It worked well but when I am using \ref in the text in order to create a link for the function. I am getting function 1 for all the \refs. However I should be getting function 1, function 2 and function 3. – Yogesh Sharma Jul 23 '18 at 06:54
  • 1
    @YogeshSharma \label should go after \caption. – egreg Jul 23 '18 at 07:34
  • @YogeshSharma Please follow @egreg’s comment. I updated my answer to include a few explanations. Also, please use ~ between a label name and its number. – Ruixi Zhang Jul 23 '18 at 12:47