4

I am trying to understand how to use fontspec to access system fonts on a Windows machine. Nothing fancy, I'm just starting.

My simple example below works in XeLaTeX but not in LuaLaTeX. Can anyone explain why?

\documentclass{article}
\usepackage{fontspec}
\setmainfont{Arial}
\pagestyle{empty}
\begin{document}

This is (plain) Arial.

This is {\setmainfont{Arial Narrow}\textbf{Bold Arial Narrow}}.

This is {\setmainfont{Arial Narrow}\textbf{\textsl{Bold Slanted Arial Narrow}}}.

This is {\setmainfont{Arial}\textbf{\textit{Bold Italic (plain) Arial}}}.

\end{document}

When LuaLaTeX is used I get this output, which is not what I want

LuaLaTeX output

When XeLaTeX is used the appearance of the text seems OK.

XeLaTeX output

Why the difference? What should I do to make my code reliable in either system?

9
  • I haven't used fontspec a whole lot, but I don't think you are supposed to keep changing the main document font. You should probably only use that command in the preamble to set the main font and then use other commands to load additional fonts if necessary. Note that it is hard for people to reproduce if you use commercial fonts not everyone has. If you can only demonstrate the issue with Arial, that's one thing. If you can reproduce it using a font supplied as part of TeX Live, say, that's far preferable.
    – cfr
    Commented Jun 29 at 1:09
  • Hmm. Works OK with a Roboto substitution. Arial specific? Or out-of-TeX-tree?
    – cfr
    Commented Jun 29 at 1:17
  • My need is to use Arial. Can you suggest another standard system font on Windows machines?
    – Blair
    Commented Jun 29 at 1:46
  • I woundered about changing the main font too. However, it would be odd wouldn't it if that were the problem in such a small example and where it works for one system and not the other. I expect that if I constructed 4 different MWEs I'd see the same issue.
    – Blair
    Commented Jun 29 at 1:48
  • I don't think it is the problem. It was more a general comment on how to set things up with fontspec to avoid issues and have things work smoothly. (Not to avoid this issue, I don't think, but just in general.)
    – cfr
    Commented Jun 29 at 1:52

3 Answers 3

6

In general, fontspec will try to guess the appropriate fonts to switch to for common changes such as regular/bold weight, upright/italic shape etc. However, it is not unusual for its guesses to be wrong. Fonts are just too variously named for any kind of guarantees here.

When things don't work as you want them to, you often need to be more directive. That is, rather than leaving fontspec to figure out which font to switch to when you ask for bold, say, you can tell it what to do in advance. Moreover, you can also be more specific about which font you mean by Arial, say, if it finds the 'wrong' one.

For instance, on my machine, requesting Roboto doesn't get me the opentype font I expected, but a truetype version which lacks the shapes/weights of the family I wanted.

The following is an example for Roboto, which doesn't demonstrate the original problem, but can easily be made to demonstrate closely related problems. The general approach should be valid for your situation, too. This also makes it more likely your document will work correctly on another system, which may have different fonts.

\setmainfont{Roboto}
[

Since I don't want the .ttf fonts, which LuaTeX seems to prefer, I specify use of the .otf. This is only harmful if you want your document to use either e.g. to support users with one or the other, but not both, formats installed.

  Extension = .otf,

The standard, default font is going to be Roboto-Regular.otf (which is part of TeX Live, but that's not relevant here).

  UprightFont = *-Regular,

I want Roboto-Bold.otf, Roboto-Italic.otf and Roboto-BoldItalic.otf for the obvious weights/shapes.

  BoldFont = *-Bold,
  ItalicFont = *-Italic,
  BoldItalicFont = *-BoldItalic,

I'm also going to tell it to use these when oblique is requested, since this didn't seem to work by default once I got more prescriptive generally.

  SlantedFont = *-Italic,
  BoldSlantedFont = *-BoldItalic,

There are different methods available at this point and different people will prefer different options for different purposes. The following is probably not the most popular of the available methods. I like it because it corresponds very closely to LaTeX standard font selection commands. (In fact, if I wasn't answering a question here, I might well do the previous lines like this, too.)

The syntax is

FontFace = {<series>}{<shape>}{<font>}

where <series> combines both weight and width. The default series is m (medium). Condensed is c and bold condensed is bc.

  FontFace = {c}{n}{*Condensed-Regular},

n is upright. So this says that if condensed, regular is requested while the main font is active, fontspec should load <main font>Condensed-Regular i.e. RobotoCondensed-Regular.otf.

  FontFace = {bc}{n}{*Condensed-Bold},

Bold concensed (bc) upright (n).

Now the italic (it).

  FontFace = {c}{it}{*Condensed-Italic},
  FontFace = {bc}{it}{*Condensed-BoldItalic},

Finally, oblique (sl).

  FontFace = {c}{sl}{*Condensed-Italic},
  FontFace = {bc}{sl}{*Condensed-BoldItalic},
  ]

To make things easy, I like to have LaTeX-like font selection commands even for non-standard switches, such as changing from medium width to condensed, so I'd say something like the following.

\makeatletter
\DeclareRobustCommand \cdwidth{%
  \not@math@alphabet\cdwidth\relax
  \fontseries{c}\selectfont}

This sets up \cdwidth to do something similar to \bfseries, but rather than switching both weight and width, this will only change the width. So, if we are currently using regular, we'll get condensed regular; if we're using bold, we'll get condensed bold.

\DeclareTextFontCommand \textcd{\cdwidth}

We might as well have a standard \text<two-letter-suffix>{} macro, too. So \textcd{} will work like \textbf{}, \textit{} etc.

Then we can write

This is (plain) Roboto.

{\cdwidth

This is Roboto Condensed \textbf{Bold Roboto Condensed}.

This is Roboto Condensed \textbf{\textsl{Bold Slanted Roboto Condensed}}.}

This is Roboto \textbf{\textit{Bold Italic (plain) Roboto}}.

to produce

Roboto, Condensed, Bold etc.

\documentclass{article}
\usepackage{fontspec}
\setmainfont{Roboto}
[
  Extension = .otf,
  UprightFont = *-Regular,
  BoldFont = *-Bold,
  ItalicFont = *-Italic,
  BoldItalicFont = *-BoldItalic,
  SlantedFont = *-Italic,
  BoldSlantedFont = *-BoldItalic,
  FontFace = {c}{n}{*Condensed-Regular},
  FontFace = {bc}{n}{*Condensed-Bold},
  FontFace = {c}{it}{*Condensed-Italic},
  FontFace = {bc}{it}{*Condensed-BoldItalic},
  FontFace = {c}{sl}{*Condensed-Italic},
  FontFace = {bc}{sl}{*Condensed-BoldItalic},
  ]
\makeatletter
\DeclareRobustCommand \cdwidth{%
  \not@math@alphabet\cdwidth\relax
  \fontseries{c}\selectfont}
\DeclareTextFontCommand \textcd{\cdwidth}
\makeatother
\pagestyle{empty}
\begin{document}

This is (plain) Roboto.

{\cdwidth

This is Roboto Condensed \textbf{Bold Roboto Condensed}.

This is Roboto Condensed \textbf{\textsl{Bold Slanted Roboto Condensed}}.}

This is Roboto \textbf{\textit{Bold Italic (plain) Roboto}}.

\end{document}
12
  • If this doesn't help, I can delete it, but it should at least enable trying some things with Arial which can be incorporated into the question (and it is rather too long for a comment).
    – cfr
    Commented Jun 29 at 2:21
  • 1
    Very nicely explained, thank you. By specifying the file names (i.e., arialn, arialnb, arialbni, and arialni) and the extension .ttf I can get LuaLaTeX to run without warnings. But this seems like hard work, because XeLaTeX didn't need the extra detail (and it took me a while to find the actual file names because Windows tries to show only the generic font names (i.e., Arial and Arial Narrow). Perhaps fontspec can cope but LuaLaTeX is not giving it the same information as XeLaTeX provides.
    – Blair
    Commented Jun 29 at 3:29
  • 1
    @Blair, in a terminal on windows go to windows/fonts then dir arial* you will see all the arial fonts available. On my windows I see also ARIALUNI.ttf In windows applications this is Arial Unicode MS! which is internal name.!
    – yannisl
    Commented Jun 29 at 5:14
  • 1
    @yannisl True, but it also exists on my system, which is what fontconfig (and so luaotfload) prefers, left to its own devices. If that was also otf, I'd have to specify path, too, but fortunately that's not relevant here.
    – cfr
    Commented Jun 29 at 5:46
  • 1
    To find the correpondence between the 'font name' and 'file names' I have found the luafindfont command line tool useful. Type in > luafindfont Arial and it reports a list of Filename, Symbolic name and Path. Not sure exactly what Symbolic name is here in relation to what one would perhaps give to \setmainfont
    – Blair
    Commented Jun 29 at 6:22
2

There's a difference in whether the system defaults to faking bold or slanted fonts when a designed bold is not available.

enter image description here

Produced with luatex with

\documentclass{article}
\usepackage{fontspec}
\setmainfont[AutoFakeBold=1.2,AutoFakeSlant]{Arial}
\pagestyle{empty}
\begin{document}

This is (plain) Arial.

This is {\setmainfont[AutoFakeBold=1.2,AutoFakeSlant]{Arial Narrow}\textbf{Bold Arial Narrow}}.

This is {\setmainfont[AutoFakeBold=1.2,AutoFakeSlant]{Arial Narrow}\textbf{\textsl{Bold Slanted Arial Narrow}}}.

This is {\setmainfont[AutoFakeBold=1.2,AutoFakeSlant]{Arial}\textbf{\textit{Bold Italic (plain) Arial}}}.

\end{document}

The markup here is incorrect, \setmainfont should only be used for setting the main font, not locally within a group (for which you could use \fontspec or better declare as suitable command with \newfontfamily) but the issue would be the same even if you changed the markup.

0

After taking in the comments and answers I think the following is what I needed to know.

Although I did not say so initially, I want to be able to access various members of the Arial system font on Windows for headings etc., but the main font in my document is not Arial.

The following seems to do the trick.

\documentclass{article}
\usepackage{fontspec}

\setmainfont{Times New Roman}
\newfontfamily\arial{Arial}[
   Extension = .ttf,
   BoldFont = arialnb, 
   BoldItalicFont = arialnbi,
   UprightFont = arialn 
] 

\pagestyle{empty}
\begin{document}
This is (plain) {\arial Arial Narrow}.

This is {\arial\bfseries {Bold Arial Narrow}}.

This is {\arial\bfseries\itshape Bold Italic Arial Narrow}.
\end{document}

I get no warnings in either LuaLaTeX or XeLaTeX now.

Along the way I found the luafindfont command-line tool useful. Typing > luafindfont Arial presented me with the files names and path that I used above.

One thing I still don't understand is the command \IfFontExists. I expected \IfFontExists to help me detect if the font was missing. But it seems that is not what it does. I found that \IfFontExistsTF{Arial Narrow}{T}{F} returned true in LuaLaTeX although the font was not found, which generated the warnings mentioned earlier.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .