1

I want to write in a different language some words. I write a word and its translation. However, I want the translation to be displayed in a different color. I know that for changing languages I can use \foreignlanguage{english}{Sample text} and to change color I can do \color{red}{Sample text} .

I wonder if I can create a command or there is something for having a shorter way to type

\foreignlanguage{english}{\color{red}{Sample text}}

I tried creating a command like this

\newcommand{\eng}{\foreignlanguage{english}{\color{red}}}[1]

but I am getting errors.

3
  • \newcommand{\eng}[1]{\foreignlanguage{english}{\color{red}#1}} Oct 21, 2020 at 17:30
  • I am getting Undefined control sequence. \eng when I try \eng{Sample text}
    – user227279
    Oct 21, 2020 at 17:42
  • 3
    well it works for me, so you are doing some wrong. Show a small but complete example, so that one doesn't have to guess. Oct 21, 2020 at 17:45

2 Answers 2

5

Yes. Both babel and polyglossia allow you to specify a different set of fonts for each language, and you can pass them different fontspec options. This can include the Color= option.

This MWE requires a version of LuaLaTeX from 2020 or later.

\documentclass{article}
\tracinglostchars=2
\usepackage[english,bidi=basic,layout=sectioning]{babel}
\usepackage[svgnames]{xcolor}
\usepackage{fontspec}

\babelprovide[import, onchar=ids fonts]{hebrew}
\babelprovide[import, onchar=ids fonts]{hindi}
\babelprovide[import, onchar=ids fonts]{japanese}

\defaultfontfeatures{ Scale=MatchUppercase, Ligatures=TeX, Renderer=HarfBuzz }
\babelfont{rm}
          [Scale=1.0, Ligatures={Common,Discretionary}]{New Computer Modern Book}
\babelfont[hebrew]{rm}
          [Color=DarkGreen ,
           UprightFont=*-Medium ,
           BoldFont=*-Bold ,
           ItalicFont=*-MediumOblique ,
           BoldItalicFont=*-BoldOblique ,
           Extension=.ttf]{FrankRuehlCLM}
\babelfont[hindi]{rm}
          [Color=DarkBlue]{Shobhika}
\babelfont[japanese]{rm}
          [Color=DarkRed]{Harano Aji Mincho}

\begin{document}
English עִבְרִית हिन्दी 日本語
\end{document}

Font sample

With polyglossia, or babel in XeLaTeX, you must tag your languages with a command such as \texthindi or \begin{hindi}. The \babeltags{hindi=hindi} command enables both in babel.

In polyglossia, you would declare fonts like so:

\newfontfamily\hindifont[Color=DarkBlue, Script=Devanagari, Language=Hindi]{Shobhika}

You can also define \hebrewfontsf, \japanesefonttt, and so on.

Another option, which also works in a legacy engine such as PDFTeX, is to declare the wrappers themselves as setting the color along with the language, for example

\usepackage[ngerman, english]{babel}
\usepackage[svgnames]{xcolor}

\newcommand\textgerman[1]{%
  \textcolor{Magenta}{\foreignlanguage{ngerman}{#1}}}

\newenvironment{german}%
  {\begingroup\color{Magenta}\begin{otherlanguage}{ngerman}}%
  {\end{otherlanguage}\endgroup}

\newcommand\german{\color{Magenta}\selectlanguage{ngerman}}
1

You can do something like this:

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[english, german, french]{babel}
\usepackage[dvipsnames]{xcolor}

\newcommand{\eng}[1]{\foreignlanguage{english}{\color{red}#1}}

\begin{document}

Brix trueddel di bat in die Gehoelzenen

\eng{This is english}

Brix trueddel di bat in die Gehoelzenen

\end{document}

If you prefer you can define a command \foreignlanguagecolored{⟨language⟩}{⟨text⟩} which selects the color depending on the language.

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[english,spanish,swedish,portuguese,german,french,italian]{babel}
\usepackage[dvipsnames]{xcolor}

\makeatletter
\@ifdefinable\GobbleToExclam{\long\def\GobbleToExclam#1!{}}%
\@ifdefinable\mycolorselect{%
   \long\def\mycolorselect#1%
   !english!spanish!swedish!portuguese!german!french!%
   #2#3!!!!{\color#2}%
}%
\newcommand\mycolorfork[1]{%
  \ifcat$\detokenize\expandafter{\GobbleToExclam#1!}$%
  \expandafter\@firstoftwo\else\expandafter\@secondoftwo\fi{%
    \mycolorselect
    !#1!spanish!swedish!portuguese!german!french!{{red}}% Case: #1 = english
    !english!#1!swedish!portuguese!german!french!{[HTML]{1ABC9C}}% Case: #1 = spanish
    !english!spanish!#1!portuguese!german!french!{[rgb]{1 0 0.99995}}% Case: #1 = swedish
    !english!spanish!swedish!#1!german!french!{[named]{CornflowerBlue}}% Case: #1 = portuguese
    !english!spanish!swedish!portuguese!#1!french!{{green}}% Case: #1 = german
    !english!spanish!swedish!portuguese!german!#1!{[named]{BurntOrange}}% Case: #1 = french
    !english!spanish!swedish!portuguese!german!french!{{black}}% Case: #1 is empty or something else without ! 
    !!!!%
  }{{black}}% Case: #1 is something else with ! 
}%
\newcommand\foreignlanguagecolored[2]{%
  \foreignlanguage{#1}{\mycolorfork{#1}#2}%
}%
\makeatother

\begin{document}

Brix trueddel di bat in die Gehoelzenen

\foreignlanguagecolored{english}{Hello!}

Brix trueddel di bat in die Gehoelzenen

\foreignlanguagecolored{spanish}{Hola!}

Brix trueddel di bat in die Gehoelzenen

\foreignlanguagecolored{swedish}{Hallå!}

Brix trueddel di bat in die Gehoelzenen

\foreignlanguagecolored{portuguese}{Olá!}

Brix trueddel di bat in die Gehoelzenen

\foreignlanguagecolored{german}{Hallo!}

Brix trueddel di bat in die Gehoelzenen

\foreignlanguagecolored{french}{Salut!}

Brix trueddel di bat in die Gehoelzenen

% italian is not in the fork, thus default black
\foreignlanguagecolored{italian}{Ciao!}

Brix trueddel di bat in die Gehoelzenen

\end{document}

enter image description here

Your Answer

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