如何切换显示英文和中文内容,并且始终显示数学内容?

如何切换显示英文和中文内容,并且始终显示数学内容?

我准备起草一份文件,每一段都有英文和中文版本,并且还有一些数学公式。

例如,假设乳胶内容如下:

English paragraph 1 goes here.

Chinese paragraph 1 goest here. afdalkjalkjflajf % imagine this is Chinese.

Formula 1.

English paragraph 2 goes here.

Chinese paragraph 2 goest here. afdalkjalkjflajf % imagine this is Chinese.

我想要一种方法来设置切换:

如果我将规则设置为“仅限英语”,则 pdf 内容将是

English paragraph 1 goes here.

Formula 1.

English paragraph 2 goes here.

如果我将其设置为仅限中文,那么 pdf 就是

Chinese paragraph 1 goest here. afdalkjalkjflajf % imagine this is Chinese.

Formula 1.

Chinese paragraph 2 goest here. afdalkjalkjflajf % imagine this is Chinese.

如果我将其设置为“both”,则 pdf 将是:

English paragraph 1 goes here.

Chinese paragraph 1 goest here. afdalkjalkjflajf % imagine this is Chinese.

Formula 1.

English paragraph 2 goes here.

Chinese paragraph 2 goest here. afdalkjalkjflajf % imagine this is Chinese.

最好的方法是什么?

答案1

基本

您可以为两种语言添加宏,然后启用其中一种:

%% for the english version:
\newcommand{\en}[1]{#1} %% English: display content inside \en{<content>}
\newcommand{\ch}[1]{}   %% English: discard content inside \ch{<content>}

现在,你可以将两个版本放在一起,使源文件保持干净整洁:

\en{English paragraph 1 goes here.}%
\ch{Chinese paragraph 1 goes here. 中文} % this is Chinese.

Formula 1.

\en{English paragraph 2 goes here.}%
\ch{Chinese paragraph 2 goes here. 中文} % this is Chinese.

如果您对实际内容进行了更改,您可以同时看到两个版本,并可以将更改应用于两种语言。

切换语言

为了简化语言之间的切换,您可以添加布尔值,或者(不带包)定义:

\def\showChinese{} %% comment this to hide Chinese content
\def\showEnglish{} %% comment this to hide English content

\ifdefined\showChinese
    \newcommand{\ch}[1]{#1}
\else
    \newcommand{\ch}[1]{}
\fi
\ifdefined\showEnglish
    \newcommand{\en}[1]{#1}
\else
    \newcommand{\en}[1]{}
\fi

巴别塔 (Babel)

如果你之前声明过命令,\usepackage{babel}那么也可以添加

\ch{\PassOptionsToPackage{chinese}{babel}}
\en{\PassOptionsToPackage{english}{babel}}

\usepackage{babel}

如果您使用 babel 来切换语言,您也可以使用's来提供\en和,但这可能不允许同时拥有两个版本。\chbabel\iflanguage

答案2

使用鍵盤我写了一些做一些比这更普遍的事情。由于这个问题已经关闭,我将在这里重现它。

使用我的代码,您可以使用环境输入不同的语言部分SelectEnvironment,并将其标记为中文或英文:

\begin{SelectEnvironment}[English]
  English paragraph 1 goes here.
\end{SelectEnvironment}

\begin{SelectEnvironment}[Chinese]
  Chinese paragraph 1 goes here.  % this is pretend Chinese.
\end{SelectEnvironment}

然后,您可以通过在文档顶部放置以下其中一行来有选择地打印一种或两种语言:

\SelectCommentsToPrint{all}      % both languages
\SelectCommentsToPrint{Englsh}   % English only
\SelectCommentsToPrint{Chinese}  % Chinese only

您还可以\SelectCommentsToPrint在文档的任何地方使用来添加到要打印的评论列表中。有“取消设置”功能可以关闭一种语言的打印,例如:

\SelectCommentsToPrint{unset/Englsh}   % stop printing English

以下是 MWE:

\documentclass{article}
\usepackage{pgf,pgffor}
\usepackage{environ}
\usepackage{amsmath}
\usepackage{xcolor}% only needed for highlighting selections in MWE

% https://tex.stackexchange.com/questions/15204/is-there-a-way-to-set-a-global-key-value-using-pgfkeys
\makeatletter
\newcommand{\pgfkeysgsetvalue}[2]{\pgfkeys@temptoks{#2}\expandafter\xdef\csname pgfk@#1\endcsname{\the\pgfkeys@temptoks}}
\makeatother

\pgfkeys{/SelectiveComment/.is family,/SelectiveComment,
  set/.unknown/.code={
      \pgfkeysgsetvalue{/SelectiveComment/\pgfkeyscurrentname}{1}
  },
  set/unset/.unknown/.code={
      \pgfkeysgsetvalue{/SelectiveComment/\pgfkeyscurrentname}{0}
  }
}

\newif\ifPrintComment% print comment if true
\newcommand\SelectCommentsToPrint[1]{%
   \foreach \key in {#1}{\pgfkeys{/SelectiveComment,set/\key} }
}
\newcommand\MakeSelection[1]{
  \PrintCommentfalse% comments off by default
  \foreach \key in {#1,all} {
    \pgfkeysifdefined{/SelectiveComment/\key}%
        {\pgfkeysgetvalue{/SelectiveComment/\key}{\temp}
         \ifnum\temp=1\global\PrintCommenttrue\fi% print if key=1
        }{}
  }
}

\newcommand\Select[2][]{\MakeSelection{#1}\ifPrintComment#2\fi}
\NewEnviron{SelectEnvironment}[1][]{\MakeSelection{#1}\ifPrintComment\BODY\fi }

\begin{document}

  \SelectCommentsToPrint{all}

  \begin{SelectEnvironment}[English]
  English paragraph 1 goes here.
  \end{SelectEnvironment}

  \begin{SelectEnvironment}[Chinese]
  Chinese paragraph 1 goes here.  % this is pretend Chinese.
  \end{SelectEnvironment}

  \[ \text{Formula 1.}\]

  \begin{SelectEnvironment}[English]
  English paragraph 1 goes here.
  \end{SelectEnvironment}

  \begin{SelectEnvironment}[Chinese]
  Chinese paragraph 1 goes here. % this is pretend Chinese.
  \end{SelectEnvironment}

\end{document}

MWE 生产:

在此处输入图片描述

如果我将其更改\SelectCommentsToPrint{all}\SelectCommentsToPrint{Chinese},则输出为:

在此处输入图片描述

还有一个\Select命令用于打印较短的文本部分。它的使用方式相同:

\Select[Chinese]{Some more words pretending to be Chinese}

使用\SelectSelectEnvironment您还可以指定用两种语言打印文本:

\Select[Chinese,English]{Will be printed when either Chinese or English is selected.}

最后,我应该指出,使用此代码,您可以使用任意键:它们不必ChineseEnglish

答案3

formulas.tex这是包含所有数学宏定义的文件。可以单独命名它们,也可以使用\autodef\autoform自动命名它们(按出现顺序)。

\newcommand{\formI}{\begin{equation}
x=a
\end{equation}}

\newcommand{\formII}{\begin{equation}
y=b
\end{equation}}

% alternatively one can use a counter and \csname
\newcounter{formula}

\newcommand{\autodef}[1]% #! = definition
{\stepcounter{formula}%
 \expandafter\def\csname autoform\Roman{formula}\endcsname{#1}}

\autodef{\begin{equation}
z=c
\end{equation}}

\setcounter{formula}{0}%ready to reuse

\newcommand{\autoform}% expand previous \autodef
{\stepcounter{formula}\csname autoform\Roman{formula}\endcsname}

然后可以编写英文、中文或其他多种语言的版本,如下所示:

\documentclass{article}
\usepackage{mathtools}
\input{formulas}
\begin{document}
English version
\formI
more text
\formII
and even more text
\autoform
etc.
\end{document}

这种方法的缺点是必须同时编辑多个文件。此外,如果您在将所有数学运算移至另一个文件之前完成第一个版本及其公式,则调试可能会更容易。

示例输出

相关内容