如何排版出現在演算法環境中的函數名稱?

如何排版出現在演算法環境中的函數名稱?

我正在使用該algorithmicx包,我想在通用文字中提及一個函數。有沒有辦法做到這一點,或者我是否必須製作一種模仿演算法中使用的格式(全部大寫等寬)?

$ ... $對於數學來說,類似的事情是這樣的,但是對於演算法來說。

在下面的例子中,我想參考CalculateCovariance在文本中引用,並且希望使用與演算法中相同的樣式對其進行排版。

在此輸入影像描述

\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}

答案1

在原始碼中algpseudocode在包中,尋找用於排版函數和過程的巨集的定義:

\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}%

你可以看到一些\textproc巨集用於排版函數/過程的名稱。作為參考,該巨集定義algpseudocode如下(在 中),

\algnewcommand\textproc{\textsc}

那裡\algnewcommand只是\newcommand有一個轉折。

但是,您不應該僅用於\textsc在正文中排版函數/過程名稱;\textproc從語意的角度來看,使用是更可取的。

在此輸入影像描述

\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}

相關內容