# How to change the font style (not the size but the style) of algorithimc package

I would like to change the font of the algorithm package. It's currently using serif and I would like to try either sans serif or typewritter font. I have tried to use the following code (which I adapted from this link) by simply using the \ttdefault at line 5 but it does not work. How can I achieve my aim?

\documentclass{article}
\usepackage{algpseudocode,algorithm}

\makeatletter
\algrenewcommand\ALG@beginalgorithmic{\ttdefault}
\makeatother

\begin{document}
\begin{algorithm}
\caption{Euclid’s algorithm}\label{euclid}
\begin{algorithmic}[1]
\Procedure{Euclid}{$a,b$}\Comment{The g.c.d. of $a$ and $b$}
\State $r \gets a \bmod b$
\While{$r \not= 0$}\Comment{We have the answer if $r$ is 0}
\State $a \gets b$
\State $b \gets r$
\State $r \gets a \bmod b$
\EndWhile\label{euclidendwhile}
\State \textbf{return} $b$\Comment{The gcd is $b$}
\EndProcedure
\end{algorithmic}
\end{algorithm}
\end{document}

• There are several packages for algorithms, and I believe they all use bold for keywords. There are very few typewriter fonts available with a matching bold, so you would most likely lose that. Jul 27, 2019 at 19:33
• Hello Barbara the aim is to convert the font family witouth loosing the bold for the keyword. Look at the answer of Zarko, he provided the right solution to my problem. Jul 27, 2019 at 19:54
• While \ttfamily as suggested by @Zarko is certainly a good solution in most ways, I've just checked the cmtt fonts available in TeX Live. Someone has provided cmbtt fonts in several sizes, but only from Metafont source, so they will be bitmap fonts, an won't scale well. I'd be very happy to be shown to be wrong. (I once had to provide a bold version of cmtt, and the only way I could do it well was to create a lighter version of the original cmtt and then a bolder version; that was in the days when bitmap fonts were still the standard. Jul 27, 2019 at 20:45
• OK I see. In fact in my case I am using xelatex with UbuntuMono as a typewritter font and it works very fine (no scaling issues). Jul 27, 2019 at 21:18
• UbuntuMono sounds like it's designed for such things, I'm glad it works. (Since no specific font was indicated in the question, I just assumed CM.) Jul 27, 2019 at 21:55

If I'm not wrong, you should just use \usepackage{algpseudocode} because it seems to re-implement algorithms and it's the one that defines things like \State, etc.

There doesn't appear to be a single, simple way to change the font (maybe you should consider a different code package?).

The keywords in the code are defined in the package like this:

\algnewcommand\algorithmicfunction{\textbf{function}}


and you can change that to

\algrenewcommand\algorithmicfunction{\texttt{function}}


You can make similar changes to some of the other keywords. This won't change the other text though, like "Euclid" and the text in the comments. You can change some of this by wrapping algorithm in a group with a font declaration.

\documentclass{article}
\usepackage{algpseudocode}
\algrenewcommand\algorithmicprocedure{\texttt{procedure}}
\begin{document}
{\tt
\begin{algorithmic}
\Procedure{Euclid}{a,b}
\State $ABC$ ABC \Comment{this is a test}
\EndProcedure
\end{algorithmic}
}
\end{document}


But this still won't change the font for the math part. To change that font, either don't put it in math, or look at a package like mathastext that redefines the math fonts.

• instead TeX \tt you should use LaTeX command \ttfamily :-) Jul 27, 2019 at 19:40
• Hello Zarko you are right if I replace \ttdefault by \ttfamily it works like a charm. I would use as a accepted answer. Could you please put your comment as answer? Jul 27, 2019 at 19:51
• Hello Ethan, thanks a lot for all the explanations I understand more the background of the package now. However, I think the solution of Zarko is more simple. Jul 27, 2019 at 19:52