1

I have algorithms that I wrote in algorithm2e. But I want the name of the function written in typewriter. As you see in the picture, this does not work, I get this strange font: enter image description here

Here is my MWE:

\documentclass{article}
\usepackage[german, linesnumbered, boxruled]{algorithm2e}
\usepackage{algorithmic}


\newcommand{\pythonsyntax}{
    \SetStartEndCondition{ }{}{}%
    \SetKwProg{Fn}{Funktion}{:}{End}
    \SetKwFunction{Range}{range}%%
    \SetKw{KwTo}{in}\SetKwFor{For}{for}{\string:}{}%
    \SetKwIF{If}{ElseIf}{Else}{if}{:}{elif}{else:}{endif}%
    \SetKwFor{While}{while}{:}{endwhile}%
    \SetKwFor{For}{for}{:}{endfor}
    \SetKw{or}{or}
    \newcommand{\forcond}{$i$ \KwTo\Range{$n$}}
    \AlgoDontDisplayBlockMarkers
    \SetKw{in}{in}
}

\newcommand{\basic}{    
    \DontPrintSemicolon
    \SetAlgoLined
    \SetKwInput{algoinput}{Eingabe}
    \SetKwInput{algooutput}{Ausgabe}
    \SetKw{return}{return}
    \SetKw{and}{and}
    \SetKw{continue}{continue}
    \SetKw{in}{in}
    \SetKw{to}{to}
    \newcommand{\algotrue}{\return\texttt{True}}
    \newcommand{\algofalse}{\return\texttt{False}}
}

\newcommand{\eingabe}[1]{\algoinput{#1}}
\newcommand{\ausgabe}[1]{\algooutput{#1}}
\newcommand{\funktion}[2]{\Fn{#1}{#2}}

\begin{document}
    \begin{algorithm}[H]
        \caption{This is an MWE} \label{algo: findjacobikandidat}
        \basic
        \pythonsyntax
        \eingabe{Some variables \texttt{a} and \texttt{b}}
        \ausgabe{Some other variables}
        \funktion{\texttt{wrongfont(h, k, p)}}{
            here is some command
        }
    \end{algorithm}
\end{document}

1 Answer 1

1

By default, algorithm2e uses \ProgSty for that part, which is defined to use \emph.

It also provides \SetProgSty to modify the output (with a peculiar syntax), but does the wrong thing, because it turns out that \SetProgSty changes \ArgSty.

You can fix it and do the required setting.

Note that algorithmic and algorithm2e aren't compatible. Use either, but not both.

\documentclass{article}
\usepackage[german, linesnumbered, boxruled]{algorithm2e}
%\usepackage{algorithmic}

%%% fix a bug in algorithm2e
\renewcommand{\SetProgSty}[1]{%
  \renewcommand{\ProgSty}[1]{% <--- algorithm2e.sty has \ArgSty
    \textnormal{\csname#1\endcsname{##1}}\unskip
  }%
}

\newcommand{\pythonsyntax}{%
    \SetStartEndCondition{ }{}{}%
    \SetKwProg{Fn}{Funktion}{:}{End}%
    \SetKwFunction{Range}{range}%%
    \SetKw{KwTo}{in}\SetKwFor{For}{for}{\string:}{}%
    \SetKwIF{If}{ElseIf}{Else}{if}{:}{elif}{else:}{endif}%
    \SetKwFor{While}{while}{:}{endwhile}%
    \SetKwFor{For}{for}{:}{endfor}%
    \SetKw{or}{or}%
    \newcommand{\forcond}{$i$ \KwTo\Range{$n$}}%
    \AlgoDontDisplayBlockMarkers
    \SetKw{in}{in}%
    % additions (maybe you also need to uncomment the last two
    \SetProgSty{normalfont}%
    %\SetFuncArgSty{normalfont}%
    %\SetArgSty{normalfont}%
}

\newcommand{\basic}{%
    \DontPrintSemicolon
    \SetAlgoLined
    \SetKwInput{algoinput}{Eingabe}%
    \SetKwInput{algooutput}{Ausgabe}%
    \SetKw{return}{return}%
    \SetKw{and}{and}%
    \SetKw{continue}{continue}%
    \SetKw{in}{in}%
    \SetKw{to}{to}%
    \newcommand{\algotrue}{\return\texttt{True}}%
    \newcommand{\algofalse}{\return\texttt{False}}%
}

\newcommand{\eingabe}[1]{\algoinput{#1}}
\newcommand{\ausgabe}[1]{\algooutput{#1}}
\newcommand{\funktion}[2]{\Fn{#1}{#2}}

\begin{document}

\begin{algorithm}[H]
  \caption{This is an MWE} \label{algo: findjacobikandidat}

  \basic
  \pythonsyntax
  \eingabe{Some variables \texttt{a} and \texttt{b}}
  \ausgabe{Some other variables}
  \funktion{\texttt{wrongfont(h, k, p)}}{
    here is some command
  }
\end{algorithm}

\end{document}

enter image description here

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.