5

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.

  • 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 7 '14 at 13:54
4

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}$.

The easiest way would be to do the styling manually. This could look like this (\textsc can be used in math and text mode. E.g. if you want to style your \Comment as well):

% arara: pdflatex

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

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

enter image description here

  • The answer by @Cubic shows how to do it "correctly" – Jochem Kuijpers Aug 30 '18 at 9:56
4

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}

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.