如何更改所有算法/algpseudocode 关键字的字体样式?

如何更改所有算法/algpseudocode 关键字的字体样式?

具体来说,我想将所有关键字的字体样式更改If Return For为粗体/无衬线,但仅限于关键字。

梅威瑟:

\documentclass{article}

\usepackage{algorithm}
\usepackage[noend]{algpseudocode} 
\usepackage{caption}

\begin{document}
\begin{algorithm}
\caption{algo}
\begin{algorithmic}[1]        
    \Function{x}{z}
        \State a
        \State b
            \If{c}
                \State d
            \EndIf
        \State \Return
    \EndFunction
\end{algorithmic}
\end{algorithm}
\end{document}

答案1

algpseudocode.sty使用显式\textbf的关键字,因此您必须重新定义它们以添加\textsf(或将它们更改为所需的格式):

\documentclass{article}

\usepackage{algorithm}
\usepackage[noend]{algpseudocode} 
\usepackage{caption}

\algrenewcommand\algorithmicend{\textsf{\textbf{end}}}
\algrenewcommand\algorithmicdo{\textsf{\textbf{do}}}
\algrenewcommand\algorithmicwhile{\textsf{\textbf{while}}}
\algrenewcommand\algorithmicfor{\textsf{\textbf{for}}}
\algrenewcommand\algorithmicforall{\textsf{\textbf{for all}}}
\algrenewcommand\algorithmicloop{\textsf{\textbf{loop}}}
\algrenewcommand\algorithmicrepeat{\textsf{\textbf{repeat}}}
\algrenewcommand\algorithmicuntil{\textsf{\textbf{until}}}
\algrenewcommand\algorithmicprocedure{\textsf{\textbf{procedure}}}
\algrenewcommand\algorithmicfunction{\textsf{\textbf{function}}}
\algrenewcommand\algorithmicif{\textsf{\textbf{if}}}
\algrenewcommand\algorithmicthen{\textsf{\textbf{then}}}
\algrenewcommand\algorithmicelse{\textsf{\textbf{else}}}
\algrenewcommand\algorithmicrequire{\textsf{\textbf{Require:}}}
\algrenewcommand\algorithmicensure{\textsf{\textbf{Ensure:}}}
\algrenewcommand\algorithmicreturn{\textsf{\textbf{return}}}


\begin{document}
\begin{algorithm}
\caption{algo}
\begin{algorithmic}[1]        
    \Function{x}{z}
        \State a
        \State b
            \If{c}
                \State d
            \EndIf
        \State \Return
    \EndFunction
\end{algorithmic}
\end{algorithm}
\end{document}

在此处输入图片描述

当然,更好的选择是使用类似

\newcommand\keywordfont{\sffamily\bfseries}
\algrenewcommand\algorithmicend{{\keywordfont end}}
\algrenewcommand\algorithmicdo{{\keywordfont do}}
\algrenewcommand\algorithmicwhile{{\keywordfont while}}
\algrenewcommand\algorithmicfor{{\keywordfont for}}
\algrenewcommand\algorithmicforall{{\keywordfont for all}}
\algrenewcommand\algorithmicloop{{\keywordfont loop}}
\algrenewcommand\algorithmicrepeat{{\keywordfont repeat}}
\algrenewcommand\algorithmicuntil{{\keywordfont until}}
\algrenewcommand\algorithmicprocedure{{\keywordfont procedure}}
\algrenewcommand\algorithmicfunction{{\keywordfont function}}
\algrenewcommand\algorithmicif{{\keywordfont if}}
\algrenewcommand\algorithmicthen{{\keywordfont then}}
\algrenewcommand\algorithmicelse{{\keywordfont else}}
\algrenewcommand\algorithmicrequire{{\keywordfont Require:}}
\algrenewcommand\algorithmicensure{{\keywordfont Ensure:}}
\algrenewcommand\algorithmicreturn{{\keywordfont return}}

因此,对 的简单更改\keywordfont将传播到所有关键字。类似这样的内容可能是对软件包作者的功能请求。

相关内容