[Tex/LaTex] How to typeset function names as they appear in algorithmic environments

algorithmicxalgorithmsinline()

I'm using the algorithmicx package and I'd like to mention a function in common text. Is there a way to do that, or do I have to make a format that mimics the one used in the algorithm (all caps monospace)?

Something like $ ... $ does for math, but for algorithms.

In the example below, I want to refer to CalculateCovariance in the text and I want it to be typeset using the same style as in the algorithm.

enter image description here

\documentclass{article}

\usepackage{algpseudocode}
\usepackage{algorithm}
\usepackage{amsmath}
\usepackage{amssymb}

\begin{document}
\begin{algorithm}[t]
\begin{algorithmic}[1]
\Function{IncrementalGN}{${\boldsymbol \theta}, {\bf v}, {\bf r}, {\bf z}_u, \Sigma_u, tol, it_{max}$}
\State $(\hat{\boldsymbol \theta}, \hat{\bf r}) = \Call{Update}{{\boldsymbol \theta}, {\bf v}, {\bf r}, {\bf z}_u, \Sigma_u}$
\State $(\hat\Lambda, \hat{\boldsymbol \eta}, A_u) = \Call{LinearSystem}{\hat{\boldsymbol \theta}\;, \hat{\bf r}}$

\State $changedLP = \textsc{false}$
\For{$it = 0$ \textbf{to} $it_{max}$}
    \State $ {\boldsymbol \delta} = \Call{Solve}{\hat\Lambda, \hat{\boldsymbol \eta}} $
    \If{$norm({\boldsymbol \delta}) < tol$}
        \State ${\bf break}$
    \EndIf
    \State $\hat{\boldsymbol \theta}\;\leftarrow \hat{\boldsymbol \theta} \oplus {\boldsymbol \delta}$
    \State $(\hat\Lambda, \hat{\boldsymbol \eta}) = \Call{LinearSystem}{\hat{\boldsymbol \theta}, \hat{\bf r}}$
    \State $changedLP = \textsc{true}$ % we have just optimized, L needs to be rebuilt    
\EndFor
\Statex \Comment a simple incremental Gauss-Newton solver

\State $ordering = \Call{AMD}{\hat\Lambda}$
\State $\hat R = \Call{Chol}{\hat\Lambda, ordering}$ %%legit\Comment the $\hat R$ factor may be reused, if available in the solver

\If{$changedLP$}
    \State $\hat\Sigma = \Call{CalculateCovariance}{\hat R, ordering}$
\Else
    \State $\hat\Sigma = \Call{UpdateCov}{\Sigma, \hat R, ordering, A_u, {\bf v}}$ % UpdateCovariance was too long
\EndIf
\EndFunction
\end{algorithmic}
\caption{\label{alg:seeifrelin} Covariance Recovery Algorithm Selection}
\end{algorithm}

\end{document}

Best Answer

In the source code of the algpseudocode package, look for the definitions of the macros used for typesetting functions and procedures:

\algdef{SE}[PROCEDURE]{Procedure}{EndProcedure}%
   [2]{\algorithmicprocedure\ \textproc{#1}\ifthenelse{\equal{#2}{}}{}{(#2)}}%
   {\algorithmicend\ \algorithmicprocedure}%
\algdef{SE}[FUNCTION]{Function}{EndFunction}%
   [2]{\algorithmicfunction\ \textproc{#1}\ifthenelse{\equal{#2}{}}{}{(#2)}}%
   {\algorithmicend\ \algorithmicfunction}%

You can see that some \textproc macro is used to typeset the name of the function/procedure. For information, that macro is defined (in algpseudocode) as follows,

\algnewcommand\textproc{\textsc}

where \algnewcommand is simply \newcommand with a twist.

However, you shouldn't just use \textsc to typeset function/procedure names in the main text; using \textproc is preferable, from a semantic point of view.

enter image description here

\documentclass{article}

\usepackage{algpseudocode}
\usepackage{algorithm}

\begin{document}
\begin{algorithm}
  \caption{AIP}\label{AIPal}
  \begin{algorithmic}[1]
    \Function{Bisection}{$f,a,b,\epsilon$}
    \State foo
    \State bar
    \EndFunction
  \end{algorithmic}
\end{algorithm}

The \textproc{Bisection} algorithm shows blah blah blah
\end{document}