26

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}
  • Will \verb|| or \texttt{} work? tex.stackexchange.com/q/2790/32374 – darthbith Sep 29 '14 at 12:04
  • @darthbith no, those are typewriter, and the style is different from the one used in algorithmicx to typeset function names (see e.g. images in tex.stackexchange.com/questions/57060/… it is all caps and the capital letters would use a slightly larger font) – the swine Sep 29 '14 at 12:09
  • See the page 4 of listings package manual (1.2 Typesetting listings\Code snippets). you can use \lstinline$var i:integer;$ manual here – Carlos Lanziano Sep 29 '14 at 12:12
  • 1
    Are you sure its monospace? Anyways, \textsc{} will get you small capitals :-) You could even mix \texttt{\textsc{}} (I think) and get typewriter small caps if your font supports it. tex.stackexchange.com/a/46699/32374 – darthbith Sep 29 '14 at 12:13
  • 1
    Please add a minimal working example (MWE) so we know exactly what you're referring to. At the moment, whether you're referring to the style of an algorithm caption or that of something else is unclear. – jub0bs Sep 29 '14 at 12:55
28

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}
  • 2
    Great, exactly what I needed. – the swine Sep 29 '14 at 13:14
  • How to properly call a defined function is other algorithms? Is there something like \Call{Bisection(...)} ? – Sebastian Widz Mar 7 '17 at 11:44

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.