3

Look at the following example, here I'm writing in two languages using babel. The name for the theorem environment (\theoremname) automatically changes with the language. Notice that the names are Théorème 1 and Theorem 2.

When I cite them, since cleveref only reads the current value of \theoremname, I get Theorem 1 and Theorem 2 in English context, and Théorème 1 et Théorème 2 in French context. However, I'd like the result to always be Théorème 1 and Theorem 2, no matter the context.

Is there any way to achieve this, without having to define separate environments for English and French?

\documentclass{article}

\usepackage{amsthm}
\usepackage{cleveref}
\newtheorem{theorem}{\theoremname}
\def\theoremname{}

\usepackage[french,english]{babel}
\addto{\captionsenglish}{\def\theoremname{Theorem}}
\addto{\captionsfrench}{\def\theoremname{Théorème}}

\crefname{theorem}{\theoremname}{\theoremname}

\begin{document}

\selectlanguage{french}
\begin{theorem}\label{thm:french}
    French.
\end{theorem}
\cref{thm:french} et \cref{thm:english}

\selectlanguage{english}
\begin{theorem}\label{thm:english}
    English.
\end{theorem}
\cref{thm:french} and \cref{thm:english}

\end{document}
1
  • 1
    I’m pretty sure you would need to define two different environments. However, you could use iflang to select which of those two environments to create, based on the current language.
    – Davislor
    Feb 8, 2021 at 15:46

2 Answers 2

1

You can define different theorem environments, sharing the same numbering, and create an environment that will select the appropriate one for the current language.

\documentclass[french,english]{article}
\usepackage{xparse}
\usepackage{babel}
\usepackage{iflang}
\usepackage{amsthm}
\usepackage{cleveref}

\addto{\captionsenglish}{\def\theoremname{Theorem}}
\addto{\captionsfrench}{\def\theoremname{Théorème}}

\newcounter{theorem}
\newtheorem{theoremEN}[theorem]{\foreignlanguage{english}{\theoremname}}
\newtheorem{theoremFR}[theorem]{\foreignlanguage{french}{\theoremname}}

\crefname{theoremEN}{Theorem}{Theorems}
\crefname{theoremFR}{Théorème}{Théorèmes}

\NewDocumentEnvironment{theorem}{O{}}%
  {\IfLanguageName{french}%
    {\begin{theoremFR}[#1]}%
    {\begin{theoremEN}[#1]}}%
  {\IfLanguageName{french}%
    {\end{theoremFR}}%
    {\end{theoremEN}}}

\begin{document}

\selectlanguage{french}
\begin{theorem}\label{thm:french}
    French.
\end{theorem}
\cref{thm:french} et \cref{thm:english}

\selectlanguage{english}
\begin{theorem}[in English]\label{thm:english}
    English.
\end{theorem}
\cref{thm:french} and \cref{thm:english}

\end{document}

Theorem and Théorème

2

LAST EDIT: A full automated way:

\documentclass{article}

\usepackage{amsthm}
\usepackage{cleveref}
\usepackage{listofitems}
\newtheorem{theorem}{\theoremname}
\def\theoremname{}

\usepackage[french,english]{babel}
\addto{\captionsenglish}{\def\theoremname{Theorem}}
\addto{\captionsfrench}{\def\theoremname{Théorème}}

\crefname{theorem}{\theoremname}{\theoremname}

\let\oldlabel\label
\def\label#1{\oldlabel{#1}\edef\lnm{\languagename}\oldlabel{\lnm:#1}}
\let\oldcref\cref
\makeatletter
\def\cref#1{\ifcsname r@\languagename:#1\endcsname\oldcref{#1}\else{\setsepchar{,}\readlist*\langlist{\bbl@loaded}\foreachitem\itm\in\langlist{{\ifcsname r@\itm:#1\endcsname\select@language{\itm}\oldcref{#1}}\fi}}\fi}
\makeatother

\begin{document}
\selectlanguage{french}
\begin{theorem}\label{thm:1}
    French.
\end{theorem}
\cref{thm:1} et \cref{thm:2}

\selectlanguage{english}
\begin{theorem}\label{thm:2}
    English.
\end{theorem}
\cref{thm:1} and \cref{thm:2}

\end{document}

Edit:

An automated way:

\documentclass{article}

\usepackage{amsthm}
\usepackage{cleveref}
\usepackage{xstring}
\newtheorem{theorem}{\theoremname}
\def\theoremname{}

\usepackage[french,english]{babel}
\addto{\captionsenglish}{\def\theoremname{Theorem}}
\addto{\captionsfrench}{\def\theoremname{Théorème}}

\crefname{theorem}{\theoremname}{\theoremname}

\let\oldlabel\label
\def\label#1{\def\mlb{#1}\iflanguage{french}{\oldlabel{#1}\oldlabel{french:#1}}{\oldlabel{#1}\oldlabel{english:#1}}}
\let\oldcref\cref
\makeatletter
\def\cref#1{\def\mlb{#1}\ifcsname r@french:#1\endcsname{\selectlanguage{french}\oldcref{#1}}\else{\selectlanguage{english}\oldcref{#1}}\fi}
\makeatother

\begin{document}

\selectlanguage{french}
\begin{theorem}\label{thm:1}
    French.
\end{theorem}
\cref{thm:1} et \cref{thm:2}

\selectlanguage{english}
\begin{theorem}\label{thm:2}
    English.
\end{theorem}
\cref{thm:1} and \cref{thm:2}

\end{document}

Old answer:

\documentclass{article}

\usepackage{amsthm}
\usepackage{cleveref}
\usepackage{xstring}
\newtheorem{theorem}{\theoremname}
\def\theoremname{}

\usepackage[french,english]{babel}
\addto{\captionsenglish}{\def\theoremname{Theorem}}
\addto{\captionsfrench}{\def\theoremname{Théorème}}

\crefname{theorem}{\theoremname}{\theoremname}

\def\mcref#1{\def\mlb{#1}\IfSubStr{#1}{engl}{{\selectlanguage{english}\cref{#1}}}{{\selectlanguage{french}\cref{#1}}}}

\begin{document}

\selectlanguage{french}
\begin{theorem}\label{thm:french}
    French.
\end{theorem}
\mcref{thm:french} et \mcref{thm:english}

\selectlanguage{english}
\begin{theorem}\label{thm:english}
    English.
\end{theorem}
\mcref{thm:french} and \mcref{thm:english}

\end{document}

enter image description here

2
  • Just add the word engl in every label of english environment and use \mcref command (I added the package xstring in your preamble)
    – koleygr
    Feb 8, 2021 at 15:52
  • The edit doesn't require the above comment
    – koleygr
    Feb 8, 2021 at 16:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .