[Tex/LaTex] Reset the default font and style for algoritm2e

algorithm2efonts

I'm using a (required) template with a sans serif font, however I would like to maintain the default font for algorithms.

I'm using pseudocode with variables with more than one letter, so I defined a data type in algorithm2e with that variable name, because I was told that in math mode latex understands multiple variables without spaces as a multiplication of several single letter variables and not as the long name of a variable.

However, as I have to use sans serif font, my variables use that font also, and it doesn't look good, as it's different from single varibles.

I tried using italic an Computer Modern font…

My code:

\documentclass[10pt,a4paper,twoside]{report}

\usepackage[linesnumbered,ruled,vlined,algochapter]{algorithm2e}
\DontPrintSemicolon
\SetAlFnt{crm}
\SetDataSty{it}

\SetKwData{Result}{result}

% Use Arial font as default
\renewcommand{\rmdefault}{phv}
\renewcommand{\sfdefault}{phv}

\begin{document}

\begin{algorithm}
  \KwIn{ \(x\) and \(y\)  }
  \KwOut{ \Result }

  \( \Result \leftarrow x + y \) \;

  \Return{ \Result  }

  \caption{ This is a caption }
\end{algorithm}

In here I talk about the \Result of \(x + y \).
\end{document}

How it looks:

Result

How I would like it to look:

Expected result

("result" seem the same font as "x" and "y", but the caption also changed…)

Best Answer

Seems I nailed it, after finding this about typesetting in algorithm2e and this about the default math mode font.

\newcommand{\myargfont}{\itshape\fontfamily{lmr}\selectfont}
\SetDataSty{myargfont}

I'm not completely sure about the font (as the link talks about Latin Modern Math and not Latin Modern Roman, as I'm using here), so if some one wants to correct me, please do!

Complete code:

\documentclass[10pt,a4paper,twoside]{report}

\usepackage[linesnumbered,ruled,vlined]{algorithm2e}
\DontPrintSemicolon
\newcommand{\myargfont}{\itshape\fontfamily{lmr}\selectfont}
\SetDataSty{myargfont}
\SetKwData{Result}{result}    
% Use Arial font as default
%
\renewcommand{\rmdefault}{phv}
\renewcommand{\sfdefault}{phv}

\begin{document}

\begin{algorithm}
  \KwIn{\(x\) and \(y\)}
  \KwOut{\Result}
  \(\Result\leftarrow x + y\) \;
  \Return{\Result}
  \caption{ This is a caption }
\end{algorithm}

In here I talk about the \Result of \(x + y\).
\end{document}