Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question
add comment

1 Answer

up vote 12 down vote accepted

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]
share|improve this answer
    
Well, with \aftergroup and some bookkeeping, fontspec could (and maybe should) take care of (re)setting the hyphenchar. –  Robert Dec 15 '12 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 '12 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. –  grizzilus Dec 15 '12 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 '12 at 23:38
    
That works really well, quite clever. –  grizzilus Dec 16 '12 at 20:28
show 5 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.