[Tex/LaTex] How to change font style of all algorithm/algpseudocode keywords

algorithmicalgorithmsfonts

In detail I want to change the font style of all keywords like If Return For to bold/sans serif, but only the keywords.

MWE:

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

Best Answer

algpseudocode.sty uses explicit \textbf for the keywords, so you have to redefine them to add \textsf (or to change them to the desired formatting):

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

enter image description here

Of course, a better option is to use something like

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

so a simple change to \keywordfont will propagate to all keywords. Something like this could be a feature request for the package author.