algorithm2e 未设置 \texttt

algorithm2e 未设置 \texttt

我有在 algorithm2e 中编写的算法。但我想要用打字机写的函数名称。正如您在图片中看到的那样,这不起作用,我得到了这种奇怪的字体: 在此处输入图片描述

这是我的 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

默认情况下,algorithm2e用于\ProgSty定义为使用的该部分\emph

它还提供了\SetProgSty修改输出的功能(使用特殊的语法),但却做了错误的事情,因为事实证明\SetProgSty发生了变化\ArgSty

您可以修复它并进行所需的设置。

请注意algorithmicalgorithm2e不兼容。请使用其中之一,但不能同时使用两者。

\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}

在此处输入图片描述

相关内容