10

I want to call a function in an algorithm environment.

\documentclass{article}
\usepackage{algorithm}
\usepackage{algpseudocode}

\begin{document}

\begin{algorithm}
\caption{Recursion}

\begin{algorithmic}[1]
\Procedure{Recursion}{$a$}
    \State $a\gets Recursion(a)$ \Comment{Call Recursion again}
    \State \textbf{return} $a$ 
\EndProcedure
\end{algorithmic}
\end{algorithm}

\end{document}

What do I have to do to get the "correct" style for the word "Recursion" in the line \State $r\gets Recursion(a)$?

I tried something like \State $r\gets \Procedure{Recursion}{a}$, but only received errors. The official documentation has no exmple regarding this problem.

Karlo
  • 3,257
Sebastian Schmitz
  • 2,559
  • 2
  • 19
  • 21
  • What do you mean by "correct"? Small caps? Please explain, what you are having. At the moment you are writing $R\cdot e\cdot c\cdot ... \cdot n(a)$ which happens when writing text like this in math mode. – LaRiFaRi Jul 07 '14 at 13:54

3 Answers3

8

I don't think that you can print the content of your procedure somewhere else as you have tried by \State $a\gets \Procedure{Recursion}{a}$.

You could do this by styling manually. You may use \textsc in math mode to do so. You can also add a \Comment (see in the example I provided). Instead of \textsc you may also use \Call. Sometimes either \textsc or \Call will not behave as expected.

% arara: pdflatex

\documentclass{article} \usepackage{algorithm} \usepackage{algpseudocode}

\begin{document} \begin{algorithm} \caption{Recursion} \begin{algorithmic}[1] \Procedure{Recursion}{$a$} \State $a\gets\Call{Recursion}(a)$ \Comment{Call Recursion again} \State \textbf{return} $a$ \EndProcedure \end{algorithmic} \end{algorithm} \end{document}

enter image description here

LaRiFaRi
  • 43,807
7

The intended way to do this in the algorithmicx package is with the \Call macro, ex:

\begin{algorithm}
\begin{algorithmic}
   \Function{Fix}{f}
      f(\Call{Fix}{f})
   \EndFunction
\end{algorithmic}
\end{algorithm}
Cubic
  • 213
0

This worked for me:

\documentclass{article}
\usepackage{algorithm}
\usepackage{algpseudocode}

\begin{document}

\begin{algorithm} \caption{Recursion}

\begin{algorithmic}[1] \Procedure{Recursion}{$a$} \State $a\gets$ \Call{Recursion}{$a$} \Comment{Call Recursion again} \State \textbf{return} $a$ \EndProcedure \end{algorithmic} \end{algorithm}

\end{document}

Which rendered: recursion

asherbret
  • 101