5

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}

1 Answer 1

5

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.

3
  • Ok, so there is no concise command to define all of them at once? Commented Oct 24, 2015 at 14:09
  • @fmetz Unfortunately no. Commented Oct 24, 2015 at 14:09
  • @fmetz I added a more convenient approach to my answer. As I suggedted at the bottom, perhaps this could be a feature request for the package author/maintainer. Commented Oct 24, 2015 at 14:14

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.