3

In my document preamble I use the following code to set my desired font and at the same time to set as default text colors different gray values for italic, bold and bold-italic text:

\usepackage{fontspec}
\usepackage{xcolor}
....
\setmainfont[{
   BoldFont=WeidemannStd-Bold.otf,
   ItalicFont=WeidemannBookItalic.otf,
   BoldItalicFont=WeidemannStd-BoldItalic.otf,
   ItalicFeatures={Colour=666666},
   BoldFeatures={Colour=6f6f6f},
   BoldItalicFeatures={Colour=777777}
  }]{WeidemannBook}

By and large, this works as expected when generating PDF via xelatex -- apart from one thing: if I want to set specific bold strings individually to a different color, say red, like this:

\textcolor{red}{\textbf{these words should be red}}

or this:

{\textbf{\textcolor{red}these words should be red}}}

it does not work as I expected, and the PDF output for both these strings is still in gray color...


How can I override the gray color which is set as default when using the fontspec package's font features?

1 Answer 1

3

A \addfontfeature command will overwrite the standard font colours and other features within the specified stretch, e.g.

{\addfontfeature{BoldItalicFeatures={Colour=red}}\bfseries\itshape these words are red}

But this might not be too practical. A \newcommand might work:

\newcommand{\mycolouredtext}[2]{{\addfontfeature{%
  ItalicFeatures={Colour=#1},%
  BoldFeatures={Colour=#1},%
  BoldItalicFeatures={Colour=#1}}%
  #2}}

Then call \mycolouredtext{green}{\bfseries\itshape This text is green} to get a green text that is italicized and boldfaced. This command will not have any influence on other, esp. following, stretches of text.

Btw, there is a syntax error in your last example of code.

{\textbf{\textcolor{red}these words should be red}}

should be

{\textbf{\textcolor{red}{these words should be red}}}

otherwise, only the first t of these will be printed in red.

3
  • Thanks for the suggestion. I just started to experiment with the \addfontfeatures command too, even before seeing your answer, after coming across it in the fontspec manual. But I didn't get it to work so far. I'll try some more, with your specific suggestions in mind. Apr 12, 2016 at 13:44
  • I also corrected the syntax error in my question. It's not in my code, it was just a typo when copying it... Apr 12, 2016 at 13:45
  • Currently I don't get it to work in my environment. But the problem may be on my side. The error I encounter is: ! Illegal parameter number in definition of \ps@plain.. I'll have to learn + master the \newcommand syntax first, before I can debug this further. Thanks for your help. Apr 12, 2016 at 14:12

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.