Algorithmic package in two different languages in same document

2,093

The algorithms bundle provides the user with the flexibility to change things to suit their needs, albeit somewhat manually. To that end, I suggest creating a language-specific environment to manage the languages you're using, say frenchalgorithm and englishalgorithm:

enter image description here

\documentclass{article}

\usepackage{algorithm,algorithmic,lipsum}
\usepackage[T1]{fontenc}

\usepackage[french,english]{babel}

\newenvironment{englishalgorithm}[1][]
  {\begin{algorithm}[#1]
     \selectlanguage{english}%
     \floatname{algorithm}{Algorithm}%
     \renewcommand{\algorithmicif}{\textbf{if}}%
     \renewcommand{\algorithmicthen}{\textbf{then}}%
     \renewcommand{\algorithmicend}{\textbf{end}}%
     % Set other language requirements
  }
  {\end{algorithm}}
\newenvironment{frenchalgorithm}[1][]
  {\begin{algorithm}[#1]
     \selectlanguage{french}%
     \floatname{algorithm}{Algorithme}%
     \renewcommand{\algorithmicif}{\textbf{si}}%
     \renewcommand{\algorithmicthen}{\textbf{alors}}%
     \renewcommand{\algorithmicend}{\textbf{fin}}%
     % Set other language requirements
  }
  {\end{algorithm}}

\begin{document}

\lipsum[1]

\begin{englishalgorithm}[H]
  \caption{An algorithm}
  \begin{algorithmic}[1]
    \STATE This is a statement
    \IF{abc}
      \STATE This is another statement
    \ENDIF
  \end{algorithmic}
\end{englishalgorithm}

\begin{frenchalgorithm}[H]
  \caption{Another algorithm}
  \begin{algorithmic}[1]
    \STATE This is a statement
    \IF{abc}
      \STATE This is another statement
    \ENDIF
  \end{algorithmic}
\end{frenchalgorithm}

\lipsum[2]

\end{document}

You'd have to define each of the translated keywords manually. The same would go if you were using the algorithmicx package.

More about the above changes is briefly discussed in sections 3.14.3 Customization and 4.4 Customization of the algorithms documentation. I don't speak French...

Share:
2,093

Related videos on Youtube

Manuel Lafond
Author by

Manuel Lafond

Updated on January 15, 2020

Comments

  • Manuel Lafond
    Manuel Lafond almost 4 years

    I need to write both in French and English in my document (for reasons).

    And I need to write algorithms in both languages, using the algorithmic package.

    Is it possible ?

    If so, how can I do that ?

    My setup is simple to include these

    \usepackage{algorithm}

    \usepackage{algorithmic}

    and I write algorithms like this:

    \begin{algorithm}

     \begin{algorithmic}
      ...
     \end{algorithmic}   
    

    \end{algorithm}

    I haven't specified anything about language in my document. So it's in English by default.

    • Werner
      Werner almost 8 years
      Yes. Why would it not be a problem? What's your current setup? You could create two different environments, one for a French algorithm, and one for an English algorithm, and set the languages accordingly. But without much more information, "Yes" is the answer here.
    • Manuel Lafond
      Manuel Lafond almost 8 years
      OK, this wasn't really intended to be a Yes/No question. Question is edited.