首页 > 解决方案 > 在英语和法语中使用 algorithm2e

问题描述

我正在用两种语言写我的论文,即英语和法语。我用英文写了算法 algorithm2e。但是,在其中一个部分中,我还需要用法语编写算法。我怎样才能做到这一点?我的意思是我只想在一个部分而不是整个乳胶文档中将算法中的所有关键字更改为法语。

标签: latex

解决方案


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

如果您想同时使用英语和法语算法,则algorithm2e在一个选项下加载包,并定义包提供的附加关键字。下面是一个小例子(我不会说法语):

在此处输入图像描述

\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包围重新定义。


推荐阅读