![关于宏 \CreateTheorem(*) 的帮助](https://linux22.com/image/429942/%E5%85%B3%E4%BA%8E%E5%AE%8F%20%5CCreateTheorem(*)%20%E7%9A%84%E5%B8%AE%E5%8A%A9.png)
我正在尝试定义一个宏\CreateTheorem(*)
,目的是能够像以前一样轻松地定义类似定理的环境,但可以在cref
多语言文档中正确跟踪。例如,
\CreateTheorem{theorem}<section>
\CreateTheorem{definition}[theorem]
应该像往常一样创建theorem
和definition
环境。结果将是这样的(这是由这个问题的最后一个文档生成的,它手动完成了 的工作\CreateTheorem
):
目前我有(下面是一个完整的,但尚未工作的 MWE):
\documentclass{article}
\usepackage[french,english]{babel}
\usepackage{xcolor}
\definecolor{paper}{RGB}{255,255,255}
\usepackage{mathtools}
\usepackage{amsthm}
\makeatletter
\newtheoremstyle{simple}%
{}{}%
{\normalfont}{}%
{\normalfont}{}%
{0pt}%
{\thmname{\MakeUppercase{#1}}\thmnumber{ #2}\hspace{.4em}\textcolor{gray!55!paper}{$|$}\hspace{.4em}\color{gray}\thmnote{\ensuremath{(\text{#3})}~~}\pushQED{\qed}}
\def\@endtheorem{\popQED\endtrivlist\@endpefalse }
\makeatother
\renewcommand{\qedsymbol}{\makebox[1em]{\color{gray!55!paper}\rule[-0.1em]{.95em}{.95em}}}
\PassOptionsToPackage{hidelinks,linktoc=all}{hyperref}
\RequirePackage{hyperref}
\PassOptionsToPackage{nameinlink}{cleveref}
\RequirePackage{cleveref}
\crefdefaultlabelformat{#2#1#3~\aftergroup\ignorespaces}
\theoremstyle{simple}
\newcommand\englishABBR{EN}
\newcommand\frenchABBR{FR}
%% macro for creating theorems
\RequirePackage{xstring}
\NewDocumentCommand{\CreateTheorem}{smod<>}{%
\IfBooleanTF{#1}
{% Stared version
\IfEndWith{#2}{*}
{% \CreateTheorem*{theorem*}
\StrGobbleRight{#2}{1}[\temp]%
\newtheorem*{\temp EN*}{\csname\temp nameEN\endcsname}
\newtheorem*{\temp FR*}{\csname\temp nameFR\endcsname}
\NewDocumentEnvironment{\temp*}{O{}}{\begin{\temp \csname\languagename ABBR\endcsname*}[##1]}{\end{\temp \csname\languagename ABBR\endcsname*}}
}
{% \CreateTheorem*{theorem}
\newtheorem*{#2EN}{\csname#2nameEN\endcsname}
\newtheorem*{#2FR}{\csname#2nameFR\endcsname}
\NewDocumentEnvironment{#2}{O{}}{\begin{#2\csname\languagename ABBR\endcsname}[##1]}{\end{#2\csname\languagename ABBR\endcsname}}
}%
}
{% Non-stared version
\IfEndWith{#2}{*}
{% \CreateTheorem{theorem*}[...]<...>
\StrGobbleRight{#2}{1}[\temp]%
\newtheorem{\temp EN*}[#3]{\csname\temp nameEN\endcsname}[#4]
\newtheorem{\temp FR*}[#3]{\csname\temp nameFR\endcsname}[#4]
\crefname{\temp EN*}{\protect\MakeUppercase{\csname\temp nameEN\endcsname}}{\protect\MakeUppercase{\csname\temp nameEN\endcsname}}
\crefname{\temp FR*}{\protect\MakeUppercase{\csname\temp nameFR\endcsname}}{\protect\MakeUppercase{\csname\temp nameFR\endcsname}}
\NewDocumentEnvironment{\temp*}{O{}}{\begin{\temp \csname\languagename ABBR\endcsname*}[##1]}{\end{\temp \csname\languagename ABBR\endcsname*}}
}
{% \CreateTheorem{theorem}[...]<...>
\newtheorem{#2EN}[#3]{\csname#2nameEN\endcsname}[#4]
\newtheorem{#2FR}[#3]{\csname#2nameFR\endcsname}[#4]
\crefname{#2EN}{\protect\MakeUppercase{\csname#2nameEN\endcsname}}{\protect\MakeUppercase{\csname#2nameEN\endcsname}}
\crefname{#2FR}{\protect\MakeUppercase{\csname#2nameFR\endcsname}}{\protect\MakeUppercase{\csname#2nameFR\endcsname}}
\NewDocumentEnvironment{#2}{O{}}{\begin{#2\csname\languagename ABBR\endcsname}[##1]}{\end{#2\csname\languagename ABBR\endcsname}}
}%
}%
}
\def\theoremnameEN{Theorem}
\def\theoremnameFR{Théorème}
\def\definitionnameEN{Definition}
\def\definitionnameFR{Définition}
\def\remarknameEN{Remark}
\def\remarknameFR{Remarque}
\def\notenameEN{Note}
\def\notenameFR{Note}
\CreateTheorem{theorem}<section>
\CreateTheorem{definition}[theorem]
\CreateTheorem*{theorem*}
\CreateTheorem*{definition*}
\CreateTheorem*{remark}
\CreateTheorem{note*} % This is strange, only for test
\begin{document}
\begin{theorem}
\end{theorem}
\begin{definition}
\end{definition}
\end{document}
有两个问题:
- 正如@Skillmon 向我指出的那样,不能使用
\newtheorem
两个计数器作为可选参数。(使用 \newtheorem) 因此[#3]
和[#4]
应该只有当它们非空时才出现。 - 这个
theorem*
案例使用了\temp
字符串,它不是静态的,可能会改变。类似于这个问题应该被考虑。
有人可以帮我建立这个宏吗?
此宏旨在简化在多语言文档中定义类定理环境的过程。下面是一份手动定义类定理环境的文档。作为比较,\CreateTheorem
完成后,它应该能够产生相同的结果,但过程要简单得多。
\documentclass{article}
\usepackage[french,english]{babel}
\usepackage{xcolor}
\definecolor{paper}{RGB}{255,255,255}
\usepackage{mathtools}
\usepackage{amsthm}
\makeatletter
\newtheoremstyle{simple}%
{}{}%
{\normalfont}{}%
{\normalfont}{}%
{0pt}%
{\thmname{\MakeUppercase{#1}}\thmnumber{ #2}\hspace{.4em}\textcolor{gray!55!paper}{$|$}\hspace{.4em}\color{gray}\thmnote{\ensuremath{(\text{#3})}~~}\pushQED{\qed}}
\def\@endtheorem{\popQED\endtrivlist\@endpefalse }
\makeatother
\renewcommand{\qedsymbol}{\makebox[1em]{\color{gray!55!paper}\rule[-0.1em]{.95em}{.95em}}}
\PassOptionsToPackage{hidelinks,linktoc=all}{hyperref}
\RequirePackage{hyperref}
\PassOptionsToPackage{nameinlink}{cleveref}
\RequirePackage{cleveref}
\crefdefaultlabelformat{#2#1#3~\aftergroup\ignorespaces}
\theoremstyle{simple}
\newcounter{theorem}
\newcounter{conjecture}
\newcounter{example}
\newcounter{problem}
\newcounter{remark}
\numberwithin{theorem}{section}
\numberwithin{conjecture}{section}
\numberwithin{example}{section}
\numberwithin{problem}{section}
\numberwithin{remark}{section}
\newcommand\englishABBR{EN}
\newcommand\frenchABBR{FR}
%% English theorems
\newtheorem{theoremEN}[theorem]{\theoremnameEN}
\newtheorem{lemmaEN}[theorem]{\lemmanameEN}
\newtheorem{propositionEN}[theorem]{\propositionnameEN}
\newtheorem{corollaryEN}[theorem]{\corollarynameEN}
\newtheorem{factEN}[theorem]{\factnameEN}
\newtheorem{conjectureEN}[conjecture]{\conjecturenameEN}
\newtheorem*{theoremEN*}{\theoremnameEN}
\newtheorem*{lemmaEN*}{\lemmanameEN}
\newtheorem*{propositionEN*}{\propositionnameEN}
\newtheorem*{corollaryEN*}{\corollarynameEN}
\newtheorem*{factEN*}{\factnameEN}
\newtheorem*{conjectureEN*}{\conjecturenameEN}
\newtheorem{definitionEN}[theorem]{\definitionnameEN}
\newtheorem{exampleEN}[example]{\examplenameEN}
\newtheorem{problemEN}[problem]{\problemnameEN}
\newtheorem*{definitionEN*}{\definitionnameEN}
\newtheorem*{exampleEN*}{\examplenameEN}
\newtheorem*{problemEN*}{\problemnameEN}
\newtheorem{remarkEN}[remark]{\remarknameEN}
\newtheorem*{remarkEN*}{\remarknameEN}
\def\theoremnameEN{Theorem}
\def\lemmanameEN{Lemma}
\def\propositionnameEN{Proposition}
\def\corollarynameEN{Corollary}
\def\factnameEN{Fact}
\def\conjecturenameEN{Conjecture}
\def\definitionnameEN{Definition}
\def\examplenameEN{Example}
\def\problemnameEN{Problem}
\def\remarknameEN{Remark}
%% French theorems
\newtheorem{theoremFR}[theorem]{\theoremnameFR}
\newtheorem{lemmaFR}[theorem]{\lemmanameFR}
\newtheorem{propositionFR}[theorem]{\propositionnameFR}
\newtheorem{corollaryFR}[theorem]{\corollarynameFR}
\newtheorem{factFR}[theorem]{\factnameFR}
\newtheorem{conjectureFR}[conjecture]{\conjecturenameFR}
\newtheorem*{theoremFR*}{\theoremnameFR}
\newtheorem*{lemmaFR*}{\lemmanameFR}
\newtheorem*{propositionFR*}{\propositionnameFR}
\newtheorem*{corollaryFR*}{\corollarynameFR}
\newtheorem*{factFR*}{\factnameFR}
\newtheorem*{conjectureFR*}{\conjecturenameFR}
\newtheorem{definitionFR}[theorem]{\definitionnameFR}
\newtheorem{exampleFR}[example]{\examplenameFR}
\newtheorem{problemFR}[problem]{\problemnameFR}
\newtheorem*{definitionFR*}{\definitionnameFR}
\newtheorem*{exampleFR*}{\examplenameFR}
\newtheorem*{problemFR*}{\problemnameFR}
\newtheorem{remarkFR}[remark]{\remarknameFR}
\newtheorem*{remarkFR*}{\remarknameFR}
\def\theoremnameFR{Théorème}
\def\lemmanameFR{Lemme}
\def\propositionnameFR{Proposition}
\def\corollarynameFR{Corollaire}
\def\factnameFR{Fait}
\def\conjecturenameFR{Conjecture}
\def\definitionnameFR{Définition}
\def\examplenameFR{Exemple}
\def\problemnameFR{Problème}
\def\remarknameFR{Remarque}
%% cleveref configuration
\crefname{theoremEN}{\protect\MakeUppercase{\theoremnameEN}}{\protect\MakeUppercase{\theoremnameEN}}
\crefname{lemmaEN}{\protect\MakeUppercase{\lemmanameEN}}{\protect\MakeUppercase{\lemmanameEN}}
\crefname{propositionEN}{\protect\MakeUppercase{\propositionnameEN}}{\protect\MakeUppercase{\propositionnameEN}}
\crefname{corollaryEN}{\protect\MakeUppercase{\corollarynameEN}}{\protect\MakeUppercase{\corollarynameEN}}
\crefname{factEN}{\protect\MakeUppercase{\factnameEN}}{\protect\MakeUppercase{\factnameEN}}
\crefname{conjectureEN}{\protect\MakeUppercase{\conjecturenameEN}}{\protect\MakeUppercase{\conjecturenameEN}}
\crefname{definitionEN}{\protect\MakeUppercase{\definitionnameEN}}{\protect\MakeUppercase{\definitionnameEN}}
\crefname{exampleEN}{\protect\MakeUppercase{\examplenameEN}}{\protect\MakeUppercase{\examplenameEN}}
\crefname{problemEN}{\protect\MakeUppercase{\problemnameEN}}{\protect\MakeUppercase{\problemnameEN}}
\crefname{remarkEN}{\protect\MakeUppercase{\remarknameEN}}{\protect\MakeUppercase{\remarknameEN}}
\crefname{theoremFR}{\protect\MakeUppercase{\theoremnameFR}}{\protect\MakeUppercase{\theoremnameFR}}
\crefname{lemmaFR}{\protect\MakeUppercase{\lemmanameFR}}{\protect\MakeUppercase{\lemmanameFR}}
\crefname{propositionFR}{\protect\MakeUppercase{\propositionnameFR}}{\protect\MakeUppercase{\propositionnameFR}}
\crefname{corollaryFR}{\protect\MakeUppercase{\corollarynameFR}}{\protect\MakeUppercase{\corollarynameFR}}
\crefname{factFR}{\protect\MakeUppercase{\factnameFR}}{\protect\MakeUppercase{\factnameFR}}
\crefname{conjectureFR}{\protect\MakeUppercase{\conjecturenameFR}}{\protect\MakeUppercase{\conjecturenameFR}}
\crefname{definitionFR}{\protect\MakeUppercase{\definitionnameFR}}{\protect\MakeUppercase{\definitionnameFR}}
\crefname{exampleFR}{\protect\MakeUppercase{\examplenameFR}}{\protect\MakeUppercase{\examplenameFR}}
\crefname{problemFR}{\protect\MakeUppercase{\problemnameFR}}{\protect\MakeUppercase{\problemnameFR}}
\crefname{remarkFR}{\protect\MakeUppercase{\remarknameFR}}{\protect\MakeUppercase{\remarknameFR}}
%% Theorem environments
\NewDocumentEnvironment{theorem}{O{}}{\begin{theorem\csname\languagename ABBR\endcsname}[#1]}{\end{theorem\csname\languagename ABBR\endcsname}}
\NewDocumentEnvironment{theorem*}{O{}}{\begin{theorem\csname\languagename ABBR\endcsname*}[#1]}{\end{theorem\csname\languagename ABBR\endcsname*}}
\NewDocumentEnvironment{lemma}{O{}}{\begin{lemma\csname\languagename ABBR\endcsname}[#1]}{\end{lemma\csname\languagename ABBR\endcsname}}
\NewDocumentEnvironment{lemma*}{O{}}{\begin{lemma\csname\languagename ABBR\endcsname*}[#1]}{\end{lemma\csname\languagename ABBR\endcsname*}}
\NewDocumentEnvironment{proposition}{O{}}{\begin{proposition\csname\languagename ABBR\endcsname}[#1]}{\end{proposition\csname\languagename ABBR\endcsname}}
\NewDocumentEnvironment{proposition*}{O{}}{\begin{proposition\csname\languagename ABBR\endcsname*}[#1]}{\end{proposition\csname\languagename ABBR\endcsname*}}
\NewDocumentEnvironment{corollary}{O{}}{\begin{corollary\csname\languagename ABBR\endcsname}[#1]}{\end{corollary\csname\languagename ABBR\endcsname}}
\NewDocumentEnvironment{corollary*}{O{}}{\begin{corollary\csname\languagename ABBR\endcsname*}[#1]}{\end{corollary\csname\languagename ABBR\endcsname*}}
\NewDocumentEnvironment{fact}{O{}}{\begin{fact\csname\languagename ABBR\endcsname}[#1]}{\end{fact\csname\languagename ABBR\endcsname}}
\NewDocumentEnvironment{fact*}{O{}}{\begin{fact\csname\languagename ABBR\endcsname*}[#1]}{\end{fact\csname\languagename ABBR\endcsname*}}
\NewDocumentEnvironment{conjecture}{O{}}{\begin{conjecture\csname\languagename ABBR\endcsname}[#1]}{\end{conjecture\csname\languagename ABBR\endcsname}}
\NewDocumentEnvironment{conjecture*}{O{}}{\begin{conjecture\csname\languagename ABBR\endcsname*}[#1]}{\end{conjecture\csname\languagename ABBR\endcsname*}}
\NewDocumentEnvironment{definition}{O{}}{\begin{definition\csname\languagename ABBR\endcsname}[#1]}{\end{definition\csname\languagename ABBR\endcsname}}
\NewDocumentEnvironment{definition*}{O{}}{\begin{definition\csname\languagename ABBR\endcsname*}[#1]}{\end{definition\csname\languagename ABBR\endcsname*}}
\NewDocumentEnvironment{example}{O{}}{\begin{example\csname\languagename ABBR\endcsname}[#1]}{\end{example\csname\languagename ABBR\endcsname}}
\NewDocumentEnvironment{example*}{O{}}{\begin{example\csname\languagename ABBR\endcsname*}[#1]}{\end{example\csname\languagename ABBR\endcsname*}}
\NewDocumentEnvironment{problem}{O{}}{\begin{problem\csname\languagename ABBR\endcsname}[#1]}{\end{problem\csname\languagename ABBR\endcsname}}
\NewDocumentEnvironment{problem*}{O{}}{\begin{problem\csname\languagename ABBR\endcsname*}[#1]}{\end{problem\csname\languagename ABBR\endcsname*}}
\NewDocumentEnvironment{remark}{O{}}{\begin{remark\csname\languagename ABBR\endcsname}[#1]}{\end{remark\csname\languagename ABBR\endcsname}}
\NewDocumentEnvironment{remark*}{O{}}{\begin{remark\csname\languagename ABBR\endcsname*}[#1]}{\end{remark\csname\languagename ABBR\endcsname*}}
\def\sometext{Here's some text without a meaning. This text ...}
\begin{document}
In English:
\begin{theorem}\label{thm:1}
\sometext
\end{theorem}
\begin{theorem*}
\sometext
\end{theorem*}
\begin{definition}\label{def:1}
\sometext
\end{definition}
\selectlanguage{french}
En français:
\begin{theorem}\label{thm:2}
\sometext
\end{theorem}
\begin{theorem*}
\sometext
\end{theorem*}
\begin{definition}\label{def:2}
\sometext
\end{definition}
\selectlanguage{english}
See \cref{thm:1,thm:2,def:1,def:2}. % reference should be in their original language
\end{document}
答案1
如果你希望 cleveref 工作,我建议定义一个计数器⟨name of environment⟩
并使用包别名用于定义别名计数器 ⟨name of environment⟩EN
和⟨name of environment⟩FR
。 (下面的例子考虑了从中
分离尾随。) 然后你可以通过 分别定义新的定理 ,但你会遇到问题*
⟨name of environment⟩
\newtheorem{⟨name of environment⟩EN}[⟨name of environment⟩EN]{⟨header⟩}
\newtheorem{⟨name of environment⟩FR}[⟨name of environment⟩FR]{⟨header⟩}
- cleveref 或 hyperref 都没有
\autoref
选择正确的\..name
-macro - 也不适用
\newtheorem
于未使用尾随EN
/指定要使用的计数器的 -directivesFR
。
\documentclass{article}
\usepackage[french,english]{babel}
\usepackage[T1]{fontenc} % <-- needed for French language !!!
\usepackage{xcolor}
\definecolor{paper}{RGB}{255,255,255}
\usepackage{mathtools}
\usepackage{amsthm}
\makeatletter
\newtheoremstyle{simple}%
{}{}%
{\normalfont}{}%
{\normalfont}{}%
{0pt}%
{\thmname{\MakeUppercase{#1}}\thmnumber{ #2}\hspace{.4em}\textcolor{gray!55!paper}{$|$}\hspace{.4em}\color{gray}\thmnote{\ensuremath{(\text{#3})}~~}\pushQED{\qed}}
\def\@endtheorem{\popQED\endtrivlist\@endpefalse }
\makeatother
\renewcommand{\qedsymbol}{\makebox[1em]{\color{gray!55!paper}\rule[-0.1em]{.95em}{.95em}}}
\RequirePackage{aliascnt}
\PassOptionsToPackage{hidelinks,linktoc=all}{hyperref}
\RequirePackage{hyperref}
\PassOptionsToPackage{nameinlink}{cleveref}
\RequirePackage{cleveref}
\crefdefaultlabelformat{#2#1#3~\aftergroup\ignorespaces}
\theoremstyle{simple}
\newcommand\englishABBR{EN}
\newcommand\frenchABBR{FR}
%% macro for creating theorems
\RequirePackage{xstring}
\makeatletter
\newcommand\PassFirstToSecond[2]{#2{#1}}%
\NewDocumentCommand{\CreateTheorem}{sm}{%
\begingroup
\protected@edef\temp{#2}%
\expandafter\IfEndWith\expandafter{\temp}{*}{%
\expandafter\StrGobbleRight\expandafter{\temp}{1}[\temp]%
\PassFirstToSecond{*}%
}{%
\PassFirstToSecond{}%
}%
{\expandafter\PassFirstToSecond\expandafter{\temp}{\endgroup\InnerCreateTheorem{#1}}}%
}%
\NewDocumentCommand{\InnerCreateTheorem}{mmmod<>}{%
% #1 = Tokens denoting an xparse-boolean value;
% value "true" -> the call was \CreateTheorem*{...}...
% value "false" -> the call was \CreateTheorem{...}...
% This indicates whether the "starred" variant or the
% "unstarred" variant of \CreateTheorem was called.
% #2 = Name of environment to define. One trailing
% star is removed from the environment-name provided
% to \CreateTheorem if that environment-name has
% trailing stars.
% #3 = If the environment-name provided to \CreateTheorem has
% trailing stars: A star (*).
% If the environment-name provided to \CreateTheorem has
% no trailing stars: Empty/no tokens at all.
% #4 = numbered like=name of counter like which the environment
% shall be numbered.
% #5 = numbered within=name of counter which is superordinate
% to the environment's counter and whose stepping resets
% the environment's counter.
\IfBooleanTF{#1}{%
\IfValueTF{#4}{\@firstoftwo}{\IfValueTF{#5}{\@firstoftwo}{\@secondoftwo}}%
}{\IfValueTF{#4}{\IfValueTF{#5}{\@firstoftwo}{\@secondoftwo}}{\@secondoftwo}}%
{%
\GenericError{}%
{\string\CreateTheorem\space syntax error\on@line}%
{You cannot call the starred variant with optional arguments.\MessageBreak
You cannot call the unstarred variant with several optional arguments.}%
{%
Allowed usage:\MessageBreak\MessageBreak
\CreateTheorem*{(name of environment)}\MessageBreak
\CreateTheorem{(name of environment)}[(numbered like)]\MessageBreak
\CreateTheorem{(name of environment)}<(numbered within)>\MessageBreak
\CreateTheorem{(name of environment)}\MessageBreak
Captions come from macros \string\(name of environment)nameEN\MessageBreak
respective \string\(name of environment)nameFR.\MessageBreak
These macros must be defined separately.%
}%
}{%
\IfBooleanTF{#1}{%
\expandafter\PassFirstToSecond\expandafter{\csname#2nameEN\endcsname}{\newtheorem*{#2EN#3}}%
\expandafter\PassFirstToSecond\expandafter{\csname#2nameFR\endcsname}{\newtheorem*{#2FR#3}}%
}{%
\IfValueTF{#5}{%
\newcounter{#2#3}[{#5}]%
\expandafter\renewcommand\expandafter*\csname the#2#3\expandafter\endcsname\expandafter{\csname the#5\endcsname.\arabic{#2#3}}%
}{%
\IfValueTF{#4}{\newaliascnt{#2#3}{#4}}{\newcounter{#2#3}}%
}%
%-------------------------------------------------------------------------------------------------------------
\CreateTheoremNumberedLikeAliasCounter{#2}{EN}{#3}%
\CreateTheoremNumberedLikeAliasCounter{#2}{FR}{#3}%
%-------------------------------------------------------------------------------------------------------------
}%
\NewDocumentEnvironment{#2#3}{}{\csname#2\csname\languagename ABBR\endcsname#3\endcsname}%
{\csname end#2\csname\languagename ABBR\endcsname#3\endcsname}%
}%
}%
\NewDocumentCommand{\CreateTheoremNumberedLikeAliasCounter}{mmm}{%
\newaliascnt{#1#2#3}{#1#3}%
\newtheorem{#1#2#3}[{#1#2#3}]{\csname#1name#2\endcsname}%
\aliascntresetthe{#1#2#3}%
\expandafter\PassFirstToSecond\expandafter{\expandafter\protect\expandafter\MakeUppercase\expandafter{\csname#1name#2\endcsname}}%
{%
\expandafter\PassFirstToSecond\expandafter{\expandafter\protect\expandafter\MakeUppercase\expandafter{\csname#1name#2\endcsname}}%
{\crefname{#1#2#3}}%
}%
}%
\makeatother
\def\theoremnameEN{Theorem}
\def\theoremnameFR{Théorème}
\def\definitionnameEN{Definition}
\def\definitionnameFR{Définition}
\def\remarknameEN{Remark}
\def\remarknameFR{Remarque}
\def\notenameEN{Note}
\def\notenameFR{Note}
\CreateTheorem{theorem}<section>
\CreateTheorem{definition}[theorem]
\CreateTheorem*{theorem*}
\CreateTheorem*{definition*}
\CreateTheorem*{remark}
\CreateTheorem{note*} % This is strange, only for test
\newcommand*\sometext{Here's some text without a meaning. This text ...}
\begin{document}
In English:
\begin{theorem}\label{thm:1}%
\sometext
\end{theorem}
\begin{theorem*}
\sometext
\end{theorem*}
\begin{definition}\label{def:1}%
\sometext
\end{definition}
\selectlanguage{french}
En français:
\begin{theorem}\label{thm:2}%
\sometext
\end{theorem}
\begin{theorem*}
\sometext
\end{theorem*}
\begin{definition}\label{def:2}%
\sometext
\end{definition}
\selectlanguage{english}
See \cref{thm:1,thm:2,def:1,def:2}. % reference should be in their original language
\end{document}
答案2
这不是对您的问题的直接回答,但我认为您的方法不是最好的方法。
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[french,english]{babel}
\usepackage{xcolor}
\usepackage{amsthm}
\usepackage[hidelinks,linktoc=all]{hyperref}
\usepackage[nameinlink]{cleveref}
\definecolor{paper}{RGB}{255,255,255}
\newtheoremstyle{simple}%
{}
{}
{\normalfont}
{}
{\normalfont}
{}
{0pt}
{%
\thmname{#1}%
\thmnumber{ #2}%
\hspace{.4em}%
\textcolor{gray!55!paper}{$|$}%
\hspace{.4em}\color{gray}%
\thmnote{(#3)~~}\pushQED{\qed}%
}
\makeatletter
\def\@endtheorem{\popQED\endtrivlist\@endpefalse}
\makeatother
\theoremstyle{simple}
\ExplSyntaxOn
\NewDocumentCommand{\CreateTheorem}{smm}
{
\IfBooleanTF{#1}
{
\jinwen_theorem_nonumber:nn { #2 } { #3 }
}
{
\jinwen_theorem_number:nn { #2 } { #3 }
}
}
\keys_define:nn { jinwen/theorem }
{
within .tl_set:N = \l__jinwen_theorem_within_tl,
like .tl_set:N = \l__jinwen_theorem_like_tl,
name .tl_set:N = \l__jinwen_theorem_name_tl,
unknown .code:n =
\prop_gput:cVn { g_jinwen_theorem_ \l__jinwen_theorem_main_tl _prop }
\l_keys_key_str { #1 },
}
\tl_new:N \l__jinwen_theorem_main_tl
\str_new:N \l_jinwen_language_curr_str
\cs_new_protected:Nn \jinwen_theorem_nonumber:nn
{
\tl_set:Nn \l__jinwen_theorem_main_tl { #1 }
\prop_new:c { g_jinwen_theorem_#1_prop }
\keys_set:nn { jinwen/theorem }
{
#2, % set the given options
}
\newtheorem*{#1}{ \__jinwen_theorem_printname:n { #1 } }
}
\cs_generate_variant:Nn \prop_item:Nn { cV }
\cs_new_protected:Nn \jinwen_theorem_number:nn
{
\tl_set:Nn \l__jinwen_theorem_main_tl { #1 }
\prop_new:c { g_jinwen_theorem_#1_prop }
\keys_set:nn { jinwen/theorem }
{
within = , % initialize to empty
like =, % initialize to empty
#2, % set the given options
}
\jinwen_theorem_new:nVV { #1 } \l__jinwen_theorem_within_tl \l__jinwen_theorem_like_tl
}
\cs_new_protected:Nn \jinwen_theorem_new:nnn
{
\tl_if_blank:nTF { #2 }
{% no within
\tl_if_blank:nTF { #3 }
{% no like
\newtheorem{#1}{ \__jinwen_theorem_printname:n { #1 } }
}
{% numbered like
\newtheorem{#1}[#3]{ \__jinwen_theorem_printname:n { #1 } }
}
}
{% within
\newtheorem{#1}{ \__jinwen_theorem_printname:n { #1 } }[#2]
}
\crefname{#1}{ \protect\__jinwen_theorem_printname:n { #1 } }
{ \protect\__jinwen_theorem_printname:n { #1 } }
}
\cs_generate_variant:Nn \jinwen_theorem_new:nnn { nVV }
\cs_new:Nn \__jinwen_theorem_printname:n
{
\text_uppercase:n { \prop_item:cV { g_jinwen_theorem_#1_prop } \l_jinwen_language_curr_str }
}
\addto\captionsenglish{\tl_set:Nn \l_jinwen_language_curr_str { EN } }
\addto\captionsfrench{\tl_set:Nn \l_jinwen_language_curr_str { FR } }
\ExplSyntaxOff
\CreateTheorem{theorem}{within=section,EN=Theorem,FR=Théorème}
\CreateTheorem{lemma}{EN=Lemma,FR=Lemme}
\CreateTheorem*{foo}{EN=abc,FR=xyz}
\CreateTheorem{theorem*}{like=theorem,EN=Easy Theorem,FR=Théorème facile}
\CreateTheorem*{foo*}{EN=foo,FR=xyz}
\begin{document}
\section{English}
\begin{lemma}\label{l}
This is a lemma.
\end{lemma}
\cref{l}
\begin{theorem}\label{a}
This is in English.
\end{theorem}
\cref{a}
\begin{theorem*}[Very easy]\label{b}
This is in English.
\end{theorem*}
\cref{b}
\begin{foo}
This is in English and is named abc.
\end{foo}
\begin{foo*}
This is in English and is named abc.
\end{foo*}
\selectlanguage{french}
\section{Français}
\begin{lemma}\label{m}
Ceci est un lemme.
\end{lemma}
\cref{m}
\begin{theorem}\label{c}
Ceci est en français.
\end{theorem}
\cref{c}
\begin{theorem*}[Très facile]\label{d}
Ceci est en français.
\end{theorem*}
\cref{d}
\begin{foo}
Ceci est en français et est nommé xyz.
\end{foo}
\begin{foo*}
Ceci est en français et est nommé xyz.
\end{foo*}
\end{document}
当然,大写字母太多了,但这是你的文档,不是我的。
答案3
下面\temp
通过使用两个辅助宏来解决您的扩展问题,一个用于带星号的情况,另一个用于不带星号的情况。不带星号的情况中的宏确保只转发正确的可选参数,如果同时使用两个宏,则会引发错误。
您不需要\edef
,此答案仅\temp
使用 扩展您的宏\expandafter
。 的一般规则\expandafter
是尝试在 扩展之前扩展,因此将执行以下操作(这列出了四个步骤,但您实际上无法访问这些步骤,TeX 在扩展第一个步骤时将执行所有这些步骤):\expandafter<tokA><tokB>
<tokB>
<tokA>
\expandafter\foo\expandafter{\stuff}
\expandafter
\expandafter
推迟\foo
:% to be reinserted \expandafter\foo % next things TeX will look at \expandafter{\stuff}
第二个
\expandafter
推迟{
:% to be reinserted \expandafter\foo\expandafter{% % next things TeX will look at \stuff}
\stuff
得到扩展,并且延迟\expandafter{
被放回(\expandafter
被删除,<stuff>
是的扩展\stuff
)% to be reinserted \expandafter\foo % next things TeX will look at {<stuff>}
最后
\expandafter\foo
重新插入并被\expandafter
移除% next things TeX will look at \foo{<stuff>}
现在\foo
可以做它的事情,它所看到的只是扩展\stuff
(扩展只是一次,这在您的用例中是可以的)。
所有这些都应用于您的代码以创建新的定理环境:
\documentclass{article}
\usepackage[french,english]{babel}
\usepackage{xcolor}
\definecolor{paper}{RGB}{255,255,255}
\usepackage{mathtools}
\usepackage{amsthm}
\makeatletter
\newtheoremstyle{simple}%
{}{}%
{\normalfont}{}%
{\normalfont}{}%
{0pt}%
{\thmname{\MakeUppercase{#1}}\thmnumber{ #2}\hspace{.4em}\textcolor{gray!55!paper}{$|$}\hspace{.4em}\color{gray}\thmnote{\ensuremath{(\text{#3})}~~}\pushQED{\qed}}
\def\@endtheorem{\popQED\endtrivlist\@endpefalse }
\makeatother
\renewcommand{\qedsymbol}{\makebox[1em]{\color{gray!55!paper}\rule[-0.1em]{.95em}{.95em}}}
\PassOptionsToPackage{hidelinks,linktoc=all}{hyperref}
\RequirePackage{hyperref}
\PassOptionsToPackage{nameinlink}{cleveref}
\RequirePackage{cleveref}
\crefdefaultlabelformat{#2#1#3~\aftergroup\ignorespaces}
\theoremstyle{simple}
\newcommand\englishABBR{EN}
\newcommand\frenchABBR{FR}
%% macro for creating theorems
\RequirePackage{xstring}
\makeatletter
\newcommand\jinwen@createenvironment@starred[2]
{%
% #1: name
% #2: star or empty
\newtheorem*{#1EN#2}{\csname#1nameEN\endcsname}%
\newtheorem*{#1FR#2}{\csname#1nameFR\endcsname}%
\NewDocumentEnvironment{#1#2}{O{}}
{\begin{#1\csname\languagename ABBR\endcsname#2}[{##1}]}
{\end{#1\csname\languagename ABBR\endcsname#2}}%
}
\newcommand\jinwen@createenvironment@nostar[4]
{%
% #1: name
% #2: star or empty
% #3: first optional argument to \newtheorem
% #4: second optional argument to \newtheorem
\IfNoValueTF{#3}
{%
\IfNoValueTF{#4}
{%
\newtheorem{#1EN#2}{\csname #1nameEN\endcsname}%
\newtheorem{#1FR#2}{\csname #1nameFR\endcsname}%
}%
{%
\newtheorem{#1EN#2}{\csname #1nameEN\endcsname}[{#4}]%
\newtheorem{#1FR#2}{\csname #1nameFR\endcsname}[{#4}]%
}%
}%
{%
\IfNoValueTF{#4}
{%
\newtheorem{#1EN#2}[{#3}]{\csname #1nameEN\endcsname}%
\newtheorem{#1FR#2}[{#3}]{\csname #1nameFR\endcsname}%
}%
{%
\PackageError{jinwen}
{%
You can't use both optional arguments,\MessageBreak
they are mutually exclusive!\@gobble%
}
{}%
}%
}%
\crefname{#1EN#2}
{\protect\MakeUppercase{\csname #1nameEN\endcsname}}
{\protect\MakeUppercase{\csname #1nameEN\endcsname}}%
\crefname{#1FR#2}
{\protect\MakeUppercase{\csname #1nameFR\endcsname}}
{\protect\MakeUppercase{\csname #1nameFR\endcsname}}%
\NewDocumentEnvironment{#1#2}{O{}}
{\begin{#1\csname\languagename ABBR\endcsname#2}[{##1}]}
{\end{#1\csname\languagename ABBR\endcsname#2}}%
}
\NewDocumentCommand{\CreateTheorem}{smod<>}
{%
\IfBooleanTF{#1}
{% Stared version
\IfEndWith{#2}{*}
{% \CreateTheorem*{theorem*}
\StrGobbleRight{#2}{1}[\temp]%
\expandafter\jinwen@createenvironment@starred\expandafter{\temp}{*}%
}
{% \CreateTheorem*{theorem}
\jinwen@createenvironment@starred{#2}{}%
}%
}
{% Non-stared version
\IfEndWith{#2}{*}
{% \CreateTheorem{theorem*}[...]<...>
\StrGobbleRight{#2}{1}[\temp]%
\expandafter\jinwen@createenvironment@nostar\expandafter
{\temp}{*}{#3}{#4}%
}
{% \CreateTheorem{theorem}[...]<...>
\jinwen@createenvironment@nostar{#2}{}{#3}{#4}%
}%
}%
}
\makeatother
\def\theoremnameEN{Theorem}
\def\theoremnameFR{Théorème}
\def\definitionnameEN{Definition}
\def\definitionnameFR{Définition}
\def\remarknameEN{Remark}
\def\remarknameFR{Remarque}
\def\notenameEN{Note}
\def\notenameFR{Note}
\newcounter{theorem}
\CreateTheorem{theorem}<section>
\CreateTheorem{definition}[theorem]
\CreateTheorem*{theorem*}
\CreateTheorem*{definition*}
\CreateTheorem*{remark}
\CreateTheorem{note*} % This is strange, only for test
\begin{document}
\begin{theorem}
\end{theorem}
\begin{definition}
\end{definition}
\end{document}
答案4
如果我理解你想要什么:
\createtheorem *{definition*}[theorem] ->
\newtheorem *{definitionEN*}[theorem]{\definitionnameEN}
\newtheorem *{definitionFR*}[theorem]{\definitionnameFR}
\createtheorem *{definition}[theorem] ->
\newtheorem *{definitionEN}[theorem]{\definitionnameEN}
\newtheorem *{definitionFR}[theorem]{\definitionnameFR}
\createtheorem {definition}[theorem] ->
\newtheorem {definitionEN}[theorem]{\definitionnameEN}
\newtheorem {definitionFR}[theorem]{\definitionnameFR}
\createtheorem {definition} ->
\newtheorem {definitionEN}{\definitionnameEN}
\newtheorem {definitionFR}{\definitionnameFR}
\createtheorem*{definition} ->
\newtheorem*{definitionEN}{\definitionnameEN}
\newtheorem*{definitionFR}{\definitionnameFR}
\createtheorem*{definition*} ->
\newtheorem*{definitionEN*}{\definitionnameEN}
\newtheorem*{definitionFR*}{\definitionnameFR}
我不明白这些星号和参数到底是什么[..]
意思(因为我不使用 LaTeX),但这并不重要。如果你想做这样的宏\createtheroem
,你可以使用以下代码:
\def\createtheorem#1#{\def\astA{#1}\createthmA}
\def\createthmA#1{\def\themname{#1}\futurelet\next\createthmB}
\def\createthmB{\ifx[\next\afterfi{\createthmC}\else\afterfi{\createthmC[]}\fi}
\def\createthmC[#1]{\def\astB{}\def\themopt{#1}%
\expandafter\createthmD\themname*\end % \themname + *->\astB
\createthmE{EN}\createthmE{FR}%
}
\def\createthmD#1*#2\end{\ifx^#2^\else\def\themname{#1}\def\astB{*}\fi}
\def\createthmE#1{%
\edef\act{\noexpand\newtheorem\astA{\themname#1\astB}%
\ifx\themopt\empty \else[\themopt]\fi
{\expandafter\noexpand\csname\themname name#1\endcsname}}\act
}
\def\afterfi#1#2\fi{\fi#1}