[Tex/LaTex] Enable bold in `algpseudocode` only for specific keywords

boldfontstypewriter

I am new in latex. I want to keep the font type in my algorithmic block but only set the keywords such as for and if to bold. I am not sure if setting the ttdefault inside a algorithm block is the best approach. Any help would be appreciated.

\documentclass{article}

\usepackage[ruled]{algorithm}
\usepackage[]{algorithmicx}
\usepackage[noend]{algpseudocode}

% make keywords below bold instead
\newcommand\keywordfont{\ttfamily\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}}

\begin{document}

\begin{algorithm}[H]

% this may not be the best approach
\renewcommand{\ttdefault}{cmtt}
\keywordfont{\ttfamily}

    \begin{algorithmic}
        \caption{Gaussian sampler using Taylor series}
        \State sigma $\gets 8 / \sqrt{2  \pi}$ 
        \State variance $\gets$ sigma$^2$
        \State \Comment{returns pdf(x) = standard Gaussian pdf}
        \Function{pdf}{x}
            \Return $e^{(-x^2 / 2)} / \sqrt{2  \pi}$
        \EndFunction
        \State \Comment{returns CDF(z) = standard Gaussian CDF using Taylor approximation}
        \Function{cdf}{x}
            \If{x $<$ -variance}
                \Return 0.0
            \ElsIf{x $>$ variance}
                \Return 1.0
            \EndIf
        \EndFunction
    \end{algorithmic}
\end{algorithm}

\end{document}

enter image description here

Best Answer

The problem is that computer modern has no boldface typewriter font. So you have to choose a font family like lmodern.

\usepackage{lmodern}

To increase the contrast between boldface and normal, you can choose the lightweight series.

 \ttfamily\fontseries{l}\selectfont

As noted by Werner in the comments, after switching the font it is not necessary to modify the keywords, as they are typeset in boldface anyway (see the code below).

enter image description here

\documentclass{article}

\usepackage[ruled]{algorithm}
\usepackage[]{algorithmicx}
\usepackage[noend]{algpseudocode}
\let\oldalgorithmic\algorithmic 
\renewcommand\algorithmic{\ttfamily\fontseries{l}\selectfont\oldalgorithmic}
\usepackage{lmodern}
\begin{document}

\begin{algorithm}[H]
    \caption{Gaussian sampler using Taylor series}
    \begin{algorithmic}
        \State sigma $\gets 8 / \sqrt{2  \pi}$ 
        \State variance $\gets$ sigma$^2$
        \State \Comment{returns pdf(x) = standard Gaussian pdf}
        \Function{pdf}{x}
            \Return $e^{(-x^2 / 2)} / \sqrt{2  \pi}$
        \EndFunction
        \State \Comment{returns CDF(z) = standard Gaussian CDF using Taylor approximation}
        \Function{cdf}{x}
            \If{x $<$ -variance}
                \Return 0.0
            \ElsIf{x $>$ variance}
                \Return 1.0
            \EndIf
        \EndFunction
    \end{algorithmic}
\end{algorithm}

\end{document}