请看下面的例子,这里我使用 用两种语言编写babel
。定理环境 ( ) 的名称\theoremname
会随语言自动更改。请注意,名称为Théorème 1
和Theorem 2
。
当我引用它们时,由于 cleveref 仅读取 的当前值\theoremname
,因此我Theorem 1 and Theorem 2
在英语上下文中得到 ,在法语上下文中得到 。但是,无论上下文如何Théorème 1 et Théorème 2
,我希望结果始终是Théorème 1
和。Theorem 2
有什么方法可以实现这一点,而不必为英语和法语定义单独的环境?
\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
您可以定义不同的定理环境,共享相同的编号,并创建一个为当前语言选择适当编号的环境。
\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}
答案2
最后编辑:一种全自动的方式:
\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}
编辑:
自动化方式:
\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}
旧答案:
\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}