我正在使用该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}