4

I am using LuaLaTeX with opentype fonts. None of the fonts are TeX fonts; they are either commercially licensed, or custom-made. No problem, everything is working great so far.

Is it possible to detect which font is being used at a particular point in a string of text? (EDIT Answer: Yes. See below). I saw this:

How can I test for the current font?

But it does not seem to work outside of TeX fonts. (EDIT Actually, yes it does.)

Here is why I wish to know: I have a custom glyph that comes in variations, in a custom font. The variations in size and weight are harmonious with surrounding fonts (Garamond, Utopia, Palatino, SourceSans, whatever). If the user changes the surrounding font, I would like the custom glyph to automatically change, using a command that detects the surrounding font and picks the correct glyph variation.

Note that the locally-used font, in a text string, may be different from the document defaults.

If this is a really big coding issue, then it would be easier for me to ignore it, and change the glyphs by hand. But if it's simple, can you point me in the right direction?

SECOND EDIT: MWE, now changed to show how it works. Thanks, egreg. I didn't catch the part about \expandafter\string earlier.

% !TeX program = LuaLaTeX
% !TEX encoding = UTF-8
\documentclass[12pt]{article}
\usepackage{fontspec}
\usepackage{polyglossia}
\setdefaultlanguage{english}
\newfontfamily{\fGaramond}{Adobe Garamond Pro}[ ]
\newcommand\fontGaramond[1]{{\fGaramond #1}}
\newfontfamily{\fMyriad}{Myriad Pro}[ ]
\newcommand\fontMyriad[1]{{\fMyriad #1}}
\setmainfont{Adobe Garamond Pro}
\begin{document}
This text is in the main font, Garamond.
Code: {\expandafter\string\the\font} is here. \fontMyriad{But this is in Myriad.
Code: {\expandafter\string\the\font} is here.}
Back to Garamond.\par
\end{document}

result

  • Have you looked into the \newfontfeature macro of the fontspec package? This macro should let you program up the custom glyph substitution in advance, i.e., in a way that doesn't require knowledge of which font is in use at the moment. (Of course, the glyph variant has to be available for all fonts that will be used, but that appears to be the case anyway.) – Mico May 01 '16 at 20:10
  • 1
    What do you mean by “it does not seem to work outside of TeX fonts”? Does your machine start crying? Can you make an example? – egreg May 01 '16 at 20:46
  • @egreg - The code in the linked post does run. But if my font is (say) Adobe Caslon Pro, then \the\font returns empty. –  May 01 '16 at 21:42
  • @miko - What you suggest is what I am already doing, successfully. But there is the possibility that the font in use, at a particular point of the document, is not the default font. I will take a second look there. –  May 01 '16 at 21:44
  • @RobtA \the\font produces the control sequence that selects the current font, not a string. – egreg May 01 '16 at 21:44
  • @egreg - Ah, I misunderstood what \the\font does. I thought it would print the string that identified the font. In any case, it did not print anything. I will make a new control string and try again. Note that I will be unable to try it until 2 days from today. –  May 01 '16 at 21:46
  • 1
    \expandafter\string\the\font – egreg May 01 '16 at 21:49
  • \fontname\font – Philipp Gesang May 02 '16 at 19:44
  • Yes, \fontname\font also works. It produces a different text string than does \expandafter\string\the\font but both are useful. –  May 04 '16 at 15:52
  • @RobtA the output of \fontname\font has been tweaked so it can be fed back into \font to define a new font from an already defined one. This behavior is Latex specific and differs from e. g. Context. – Philipp Gesang May 10 '16 at 21:04

1 Answers1

3

The instruction \the\font produces nothing “visible”, but the control sequence that chooses the current font, in the internal NFSS format.

You can produce the string representation of this control sequence by \expandafter\string\the\font. I changed the fonts to some I have.

\documentclass[12pt]{article}
\usepackage{fontspec}
\usepackage{polyglossia}

\setdefaultlanguage{english}

\setmainfont{EB Garamond}
\newfontfamily{\fMyriad}{TeX Gyre Adventor}
\DeclareTextFontCommand{\fontMyriad}{\fMyriad}

\begin{document}
This text is in the main font, Garamond.
Code: {\expandafter\string\the\font} is here. \fontMyriad{But this is in Myriad.
Code: {\expandafter\string\the\font} is here.}
Back to Garamond.\par
\end{document}

Note you don't need \fGaramond, as \normalfont and \textnormal are the equivalent of it and \fontGaramond.

You can obtain more predictable family names if you add the option NFSSFamily:

\documentclass[12pt]{article}
\usepackage{fontspec}
\usepackage{polyglossia}

\setdefaultlanguage{english}

\setmainfont{EB Garamond}[
  NFSSFamily=Garamond
]
\newfontfamily{\fMyriad}{TeX Gyre Adventor}[
  NFSSFamily=Myriad
]
\DeclareTextFontCommand{\fontMyriad}{\fMyriad}

\begin{document}
This text is in the main font, Garamond.
Code: {\expandafter\string\the\font} is here. \fontMyriad{But this is in Myriad.
Code: {\expandafter\string\the\font} is here.}
Back to Garamond.\par
\end{document}

enter image description here

Note that at any time you can inspect the values of

\f@encoding \f@family \f@series \f@shape

as suggested in Mike Renfro's answer in order to make a decision macro; for instance, under \normalfont and assuming the second example, \f@family will expand to Garamond.

egreg
  • 1,121,712
  • Thanks, egreg. The \fGaramond is actually part of some other macro that I use, not relevant to my original question. –  May 02 '16 at 00:16