使用英文和法文的algorithm2e

时间:2019-01-06 04:56:47

标签: latex

我正在用两种语言(即英语和法语)写论文。我用英语写了使用algorithm2e的算法。但是,在其中一部分中,我还需要用法语编写算法。我该如何实现?我的意思是我只想在一个部分而不是整个乳胶文档中将算法中的所有关键字更改为法语。

1 个答案:

答案 0 :(得分:1)

algorithm2e提供了软件包选项frenchfrenchkw。前者除其他外,更改了与\caption中的algorithm关联的名称。后者提供了您可以使用的法语关键字。

如果要同时使用英语和法语算法,请在一个选项下加载algorithm2e程序包,然后定义该程序包提供的其他关键字。下面是一个小例子(我不会说法语):

enter image description here

\documentclass{article}

\usepackage{algorithm2e}

% French keywords:
\SetKwInput{KwRes}{R\'esultat}%
\SetKwIF{Si}{SinonSi}{Sinon}{si}{alors}{sinon si}{sinon}{fin si}%
\SetKwFor{Tq}{tant que}{faire}{fin tq}%

\begin{document}

\begin{algorithm}[H]
  \SetAlgoLined
  \KwData{this text}
  \KwResult{how to write algorithm with \LaTeX2e }
  initialization\;
  \While{not at end of this document}{
    read current\;
    \eIf{understand}{
      go to next section\;
      current section becomes this one\;
    }{
      go back to the beginning of current section\;
    }
  }
  \caption{How to write algorithms in English}
\end{algorithm}

\begin{algorithm}[H]
  \renewcommand{\algorithmcfname}{Algorithme}%
  \SetAlgoLined
  \KwData{this text}
  \KwRes{how to write algorithm with \LaTeX2e }
  initialization\;
  \Tq{not at end of this document}{
    read current\;
    \eSi{understand}{
      go to next section\;
      current section becomes this one\;
    }{
      go back to the beginning of current section\;
    }
  }
  \caption{How to write algorithms in French}
\end{algorithm}

\end{document}

这是英语算法内容的完整定义(摘自algorithm2e.sty):

\renewcommand{\listalgorithmcfname}{List of Algorithms}%
\renewcommand{\algorithmcfname}{Algorithm}%
\renewcommand{\algorithmautorefname}{algorithm}%
\renewcommand{\algorithmcflinename}{line}%
\renewcommand{\algocf@typo}{}%
\renewcommand{\@algocf@procname}{Procedure}%
\renewcommand{\@algocf@funcname}{Function}%
\renewcommand{\procedureautorefname}{procedure}%
\renewcommand{\functionautorefname}{function}%
\renewcommand{\algocf@languagechoosen}{english}%

\SetKwHangingKw{KwHData}{Data$\rightarrow$}
\SetKwInput{KwIn}{Input}%
\SetKwInput{KwOut}{Output}%
\SetKwInput{KwData}{Data}%
\SetKwInput{KwResult}{Result}%
\SetKw{KwTo}{to}
\SetKw{KwRet}{return}%
\SetKw{Return}{return}%
\SetKwBlock{Begin}{begin}{end}%
\SetKwRepeat{Repeat}{repeat}{until}%
%
\SetKwIF{If}{ElseIf}{Else}{if}{then}{else if}{else}{end if}%
\SetKwSwitch{Switch}{Case}{Other}{switch}{do}{case}{otherwise}{end case}{end switch}%
\SetKwFor{For}{for}{do}{end for}%
\SetKwFor{ForPar}{for}{do in parallel}{end forpar}
\SetKwFor{ForEach}{foreach}{do}{end foreach}%
\SetKwFor{ForAll}{forall}{do}{end forall}%
\SetKwFor{While}{while}{do}{end while}%

这是法语算法的完整内容定义(摘自algorithm2e.sty):

\renewcommand{\listalgorithmcfname}{Liste des Algorithmes}%
\renewcommand{\algorithmcfname}{Algorithme}%
\renewcommand{\algorithmautorefname}{algorithme}%
\renewcommand{\algorithmcflinename}{ligne}%
\renewcommand{\algocf@typo}{\ }%
\renewcommand{\@algocf@procname}{Proc\'edure}%
\renewcommand{\@algocf@funcname}{Fonction}%
\renewcommand{\procedureautorefname}{proc\'edure}%
\renewcommand{\functionautorefname}{fonction}%
\renewcommand{\algocf@languagechoosen}{french}%

\SetKwHangingKw{HDonnees}{Donnees$\rightarrow$}
\SetKwInput{Donnees}{Donn\'ees}%
\SetKwInput{Res}{R\'esultat}%
\SetKwInput{Entree}{Entr\'ees}%
\SetKwInput{Sortie}{Sorties}%
\SetKw{KwA}{\`a}%
\SetKw{Retour}{retourner}%
\SetKwBlock{Deb}{d\'ebut}{fin}%
\SetKwRepeat{Repeter}{r\'ep\'eter}{jusqu'\`a}%
%
\SetKwIF{Si}{SinonSi}{Sinon}{si}{alors}{sinon si}{sinon}{fin si}%
\SetKwSwitch{Suivant}{Cas}{Autre}{suivant}{faire}{cas o\`u}{autres cas}{fin cas}{fin d'alternative}%
\SetKwFor{Pour}{pour}{faire}{fin pour}%
\SetKwFor{PourPar}{pour}{faire en parall\`ele}{fin pour}%
\SetKwFor{PourCh}{pour chaque}{faire}{fin pour chaque}%
\SetKwFor{PourTous}{pour tous les}{faire}{fin pour tous}%
\SetKwFor{Tq}{tant que}{faire}{fin tq}%

如果您使用任何包含@的定义(例如,像\renewcommand{\@algocf@procname}{Proc\'edure}),则必须使用\makeatletter...\makeatother pair来包围重定义。