23

I have encountered something while using fontspec that I do not quite understand. What is the scope of a font feature added using the \addfontfeatures command? For instance, consider the example below using lualatex:

 \documentclass{article}
 \usepackage{fontspec,lipsum}
 \setmainfont{TeX Gyre Termes}

 \begin{document}

 1234567890

 {%
      \addfontfeatures{Numbers=OldStyle}%
      1234567890%
 }

 1234567890

 Here is some text that is long enough so that it will involve a single proper hyphenation at the end of the first line.

 {%
      \addfontfeatures{HyphenChar=None}%
      Here is some text that is long enough so that it will involve a single proper hyphenation at the end of the first line.%
 }

 Here is some text that is long enough so that it will involve a single proper hyphenation at the end of the first line.

 \end{document}

The option Numbers=OldStyle is only applied locally and does not exit the braces, but the option HyphenChar=None continues after the braces end. Is there a way to limit the scope of HyphenChar as Numbers is limited above?

1
  • 1
    Hmmmm, I guess all this needs to be added explicitly to the documentation :-) Oct 13, 2015 at 10:58

2 Answers 2

20

Good question!

Some assignments performed by TeX (and also by XeTeX) are always global. You find the list on page 277 of the TeXbook or section 10.2 in TeX by Topic. Among them is setting the \hyphenchar of a font, which is the primitive operation performed by the

HyphenChar=None

option, or any other setting of the same key.

So, no, there is no way to limit the scope of that declaration. Simply declaring a new font family using the same font but changing the hyphenchar will not work, as the value is associated to the memory location relative to the font and fonts loading the same TFM file (standard TeX) or the same OTF file (XeTeX) will share the hyphenchar.

A possible workaround is to define a new font family slightly scaling the font:

\documentclass{article}
\usepackage{fontspec}
\setmainfont{Junicode}
\newfontfamily{\mainno}[HyphenChar=None,Scale=.999]{Junicode}

\usepackage{polyglossia}
\setmainlanguage{latin}

\usepackage{lipsum}

\textwidth=5cm

\begin{document}

\hyphenpenalty=-3000

{\mainno\the\hyphenchar\font\lipsum*[2]} \the\hyphenchar\font \lipsum[3]

\end{document}

The settings are just to encourage hyphenation. The first part of the paragraph will have no hyphens and start with -1, while the second part will be hyphenated and start with 45. Of course the first part will have many overfull lines. Here's an image (second part truncated):

enter image description here

It's also possible to say

{\addfontfeatures{Scale=.999,HyphenChar=None}\the\hyphenchar\font\lipsum*[2]}
\the\hyphenchar\font \lipsum[3]
10
  • Well, with \aftergroup and some bookkeeping, fontspec could (and maybe should) take care of (re)setting the hyphenchar.
    – Robert
    Dec 15, 2012 at 2:14
  • 1
    @Robert TeX uses only one value of \hyphenchar for each font, the one current at the end of the paragraph. When TeX splits a paragraph into lines, it has forgotten about grouping structures and is just confronted with a horizontal list. And defining a new font family doesn't help either, because the hyphenchar is associated to the memory location for the font information and fonts corresponding to the same TFM file (or OTF or TTF in case of XeTeX) can't have different hyphenchars.
    – egreg
    Dec 15, 2012 at 11:04
  • after reading the surrounding material in the TeXbook it makes a lot more sense now...perhaps a note about HypenChar should be included in the fontspec documentation. The addfontfeatures command is introduced in the context of using it for local modifications, which is what prompted this question in the first place. Dec 15, 2012 at 23:26
  • 2
    @grizzilus I've added a hack, which shouldn't be too noticeable (the font is scaled at 99.9%).
    – egreg
    Dec 15, 2012 at 23:38
  • 1
    @Robert Curious; it works if you say \hyphenchar=-1, but not if you say \hyphenchar=64 (or any other number). Seems like a bug in LuaTeX.
    – egreg
    Dec 16, 2012 at 23:48
2

I just came across a similar problem, where I was not able to limit the scope of \addfontfeature{WordSpace=0.8}, which I found weird, as in some other place I had it working already, limited by a pair of parentheses. In that other place though I had also adjusted the letter space, so I came up with the solution of adding a LetterSpace=0 to my \addfontfeature, which doesn't have any effect on the letter space, but helps to keep the word space setting local. The same works also with the OP's example:

\documentclass{article}
 \usepackage{fontspec,lipsum}
 \setmainfont{TeX Gyre Termes}

 \begin{document}

 1234567890

 {%
      \addfontfeatures{Numbers=OldStyle}%
      1234567890%
 }

 1234567890

 Here is some text that is long enough so that it will involve a single proper hyphenation at the end of the first line.

 {%
      \addfontfeatures{HyphenChar=None,LetterSpace=0}%
      %\addfontfeatures{HyphenChar=None}%
      Here is some text that is long enough so that it will involve a single proper hyphenation at the end of the first line.%
 }

 Here is some text that is long enough so that it will involve a single proper hyphenation at the end of the first line.

 \end{document}

example output

You must log in to answer this question.

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