当我用不同语言书写时字体颜色会发生变化

当我用不同语言书写时字体颜色会发生变化

我想用不同的语言写一些单词。我写了一个单词及其翻译。但是,我希望翻译以不同的颜色显示。我知道要更改语言我可以使用\foreignlanguage{english}{Sample text},要更改颜色我可以使用\color{red}{Sample text}

我想知道我是否可以创建一个命令,或者是否有更短的输入方式

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

我尝试创建这样的命令

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

但我收到了错误。

答案1

是的。babel和都polyglossia允许您为每种语言指定不同的字体集,并且您可以向它们传递不同的fontspec选项。这可以包括Color=选项。

此 MWE 需要 2020 或更高版本的 LuaLaTeX。

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

字体示例

在 XeLaTeX 中使用polyglossia、 或,您必须使用或babel等命令标记您的语言。该命令可在 中同时启用 和。\texthindi\begin{hindi}\babeltags{hindi=hindi}babel

在 中polyglossia,您可以像这样声明字体:

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

您还可以定义\hebrewfontsf\japanesefonttt等等。

另一个选项(也适用于 PDFTeX 等传统引擎)是将包装器本身声明为与语言一起设置颜色,例如

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

答案2

你可以做这样的事情:

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

如果您愿意,您可以定义一个 根据语言选择颜色的命令。\foreignlanguagecolored{⟨language⟩}{⟨text⟩}

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

在此处输入图片描述

相关内容