# how to toggle displaying English and Chinese content, and always showing math content?

I'm about to draft a document that each paragraph will have English and Chinese version, and there are also some math formulas.

For example, suppose the latex content is something like this:

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.


I'd like to have a way to set the toggling:

If I set the rule as "English only", the pdf content would be

English paragraph 1 goes here.

Formula 1.

English paragraph 2 goes here.


if I set it to be Chinese only, then the pdf is

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

Formula 1.

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


if I set it to be "both", the pdf will be:

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.


What's the best way to do so?

• I would do separate Chinese and English versions, but put all the equations into macros in a third file to be \input by the other two. – John Kormylo Jun 13 '15 at 13:09
• how to do the \input? – athos Jun 16 '15 at 10:52

## Basics

You can add macros for both of the languages, and just enable one of them:

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


Now you can keep your source file nice and clean with both versions together:

\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.


If you make a change to the actual content, you see both versions at once and can apply the change to both languages.

## Switching the languages

In order to simplify switching between the languages, you can add a boolean, or (without a package) a definition:

\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

If you declare the commands before \usepackage{babel} you could also add

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


before \usepackage{babel}.

You could also feed \en and \ch with babel's \iflanguage, if you use babel to switch the languages, but this probably does not allow to have both versions.

Using pgfkeys I wrote some macros to do something more general than this. As that question has been closed I will reproduce it here.

Using my code you can type your different language sections using the environment SelectEnvironment, tagging it as either Chinese or English:

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

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


Then you can selectively print choose one or both languages by putting one of the following lines at the top of your document:

\SelectCommentsToPrint{all}      % both languages


You can also use \SelectCommentsToPrint anywhere in the document to add to the list of comments being printed. There is "unset" feature to turn off printing of one language, say:

\SelectCommentsToPrint{unset/Englsh}   % stop printing English


Here is a 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
\foreach \key in {#1}{\pgfkeys{/SelectiveComment,set/\key} }
}
\newcommand\MakeSelection[1]{
\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}

\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}


The MWE produces:

If I change the \SelectCommentsToPrint{all} to \SelectCommentsToPrint{Chinese} then the output is:

There is also a \Select command for printing shorter sections of text. It is used in the same way:

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


With both \Select and the SelectEnvironment you can also specify that the text should be printed in both languanges:

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


Finally, I should note that with this code you can use arbitrary keys: they don't have to be Chinese and English

Here is the file formulas.tex containing macro definition for all the math. One can either name them individually, or use \autodef and \autoform to name them automatically (in order of appearance).

\newcommand{\formI}{$$x=a$$}

\newcommand{\formII}{$$y=b$$}

% 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{$$z=c$$}

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


One would then write English, Chinese, or multiple other language versions as follows:

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


The disadvantage of this approach is having to edit multiple files simultaneously. Also, it might be easier to debug if you complete the first version together with its formulas before moving all the math to another file.