18

Before I start, I want to say that I have already read this post and this post

I am writing a paper in two languages. I want to write other language version of every paragraphs and title just below the original version.

Is the any two switch two language by using same options like

Lang == Esp

\if{Lang=Eng}
   \chapter{LITERATURE}
\elif
   \chapter{LITERATURA}

or

\if{Lang=Eng}
   {I love you.}
\elif
   {Te quiero.}

Is there any way to do that?

14

You could use an approach like the following, I use it to create various versions of a CV document.

\documentclass{minimal}

\usepackage{ifthen}
\newboolean{somevariable}
\setboolean{somevariable}{false}

\begin{document}

\ifthenelse{\boolean{somevariable}}{Text if somevariable is true.}{Text if somevariable is false.}

\end{document}
  • Works smoothly. This is what i need it. Thank you!!! – i2_ Nov 27 '18 at 11:43
  • 1
    Then please tag my answer as the solution. It helps other to find it more easily. – Uwe Ziegenhagen Nov 27 '18 at 15:41
15

There's always docstrip, which lets you distinguish one code from another using special comments (called guards) and also have code which is common to both.

In my opinion, the advantage of this method over the conditionals is that it's fairly simple to add more languages avoiding the hassle of nesting conditionals.

For instance, you can have a source file (let's call it source.dtx, but any other name will do):

\documentclass{book}
\begin{document}

%<english>\chapter{LITERATURE}
%<spanish>\chapter{LITERATURA}

%<*english>
\emph{The Tau Manifesto} is dedicated to one of the most important numbers in
mathematics, perhaps \emph{the} most important: the \emph{circle constant} relating
the circumference of a circle to its linear dimension. For millennia, the circle has
been considered the most perfect of shapes, and the circle constant captures the
geometry of the circle in a single number. Of course, the traditional choice for
the circle constant is $\pi$--but, as mathematician Bob Palais notes in his
delightful article ``$\pi$ Is Wrong!'', $\pi$ \emph{is wrong}. It's time to set
things right.
%</english>

%<*spanish>
\emph{El Manifiesto de Tau} está dedicado a uno de los números más importantes en
matemáticas, quizás \emph{el} más importante: la \emph{constante de círculo} que relaciona
la circunferencia de un círculo con su dimensión lineal. Durante milenios, el círculo ha
sido considerado la forma más perfecta, y la constante del círculo captura la
geometría del círculo en un solo número. Por supuesto, la elección tradicional para
la constante de círculo es $\pi$--pero, como señala el matemático Bob Palais en su
artículo encantador ``$\pi$ Is Wrong!'', $\Pi$ \emph{es incorrecto}. Es hora de arreglar las cosas.
%</spanish>

\end{document}

(sample text taken from here and Google-Translated to Spanish)

then you only need a so-called “installation file”:

\input docstrip % Loads the docstrip program
% Optional switches
\askforoverwritefalse % Allows it to overwrite older files without asking
\keepsilent % Reduces verbosity
\nopreamble % Removes preamble
\nopostamble % removes postamble
% Separating sources:
\generate{%
  %
  \file{english.tex}{%
    \from{source.dtx}{english}%
  }%
  %
  \file{spanish.tex}{%
    \from{source.dtx}{spanish}%
  }%
  %
}
% Exiting
\endbatchfile

which will generate an english.tex file from the parts that are marked as <english> in the source.dtx and another one called spanish.tex from the parts marked with <spanish> in the source.dtx. The parts of the source.dtx which aren't marked go to both output files.

To execute this you only need to run TeX on the installation file:

> pdftex installation.ins

The generated english.tex will look like this:

\documentclass{book}
\begin{document}

\chapter{LITERATURE}

\emph{The Tau Manifesto} is dedicated to one of the most important numbers in
mathematics, perhaps \emph{the} most important: the \emph{circle constant} relating
the circumference of a circle to its linear dimension. For millennia, the circle has
been considered the most perfect of shapes, and the circle constant captures the
geometry of the circle in a single number. Of course, the traditional choice for
the circle constant is $\pi$--but, as mathematician Bob Palais notes in his
delightful article ``$\pi$ Is Wrong!'', $\pi$ \emph{is wrong}. It's time to set
things right.


\end{document}
  • This solution also works and as you said we definitely work on cleaner source tex. But in this case i do not want to create new files. Thank you anyway! – i2_ Nov 27 '18 at 12:26
  • 2
    @i2_ I'm afraid there's no way to use docstrip without generating other source files. In fact, this is the purpose of docstrip: to generate usable TeX code from one (or more) source files. But, if you can live with the multiple files (why, not; LaTeX creates a bunch of them anyway :-), since this operates “outside” the TeX run of your document, you don't have to worry about verbatim content being messed up, and you can do some other tricks which aren't possible when you need to keep braces and groups balanced. Pros and cons of each method :) – Phelype Oleinik Nov 27 '18 at 12:48
12

You can use the iflang package:

\documentclass{article}
\usepackage[english,ngerman]{babel}
\usepackage{iflang}
\begin{document}

\IfLanguageName{english}{This is english}{This is not english}

\IfLanguageName{ngerman}{Das ist deutsch}{Das ist nicht  deutsch}

\selectlanguage{english}

\IfLanguageName{english}{This is  english}{This not english}

\IfLanguageName{ngerman}{Das ist deutsch}{Das ist nicht deutsch}

\end{document}

enter image description here

7

The comment package makes it easy to define arbitrary throw-away environments:

\documentclass{article}
\usepackage{comment}


% define comment sections that are either included or excluded 
\excludecomment{ENG}
\includecomment{GER}

\begin{document}

\begin{ENG}
  In the beginning was the Word, and the Word was with God, and the Word was God.
\end{ENG}

\begin{GER}
    Am Anfang war das Wort und das Wort war bei Gott und Gott war das Wort.
\end{GER}

\end{document}

If one defines both environments with \includecomment, both will become part of the output.

Technically, this comes close to the docstrip approach suggested by Phelype Oleinik as the excluded sections are actually gobbled (lexically thrown away), that is, not compiled by the tex compiler.

5

You can do it quite smoothly using ConTeXt, another TeX engine. It has a mode option that you can pass as an option. When compiling with mode=thismode, everything enclosed in the proper \startmode[thismode] \stopmode pair will be interpreted.

Here is a MWE

\startmode[english]
\language[en]
\stopmode
\startmode[espanol]
\language[es]
\stopmode

\starttext
\startmode[english]
I love you
\stopmode
\startmode[espanol]
Te quiero.
\stopmode
\stoptext

Paste this MWE in mwe.tex and compare the output of context --mode=english mwe.tex and context --mode=espanol mwe.tex. If you want to have the two texts next to another, context --mode=english,espanol mwe.tex will do it.

  • If you think my post was useful, please upvote it, this is the usual way to thank here. You can upvote as many answers as you want (including the one you accepted). – sztruks Nov 27 '18 at 12:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.