# How to detect the font currently in use with LuaLaTeX

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}
\newcommand\fontGaramond[1]{{\fGaramond #1}}
\begin{document}
This text is in the main font, Garamond.
Code: {\expandafter\string\the\font} is here.}
Back to Garamond.\par
\end{document}


• 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 1 '16 at 20:10
• 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 1 '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. – user103221 May 1 '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. – user103221 May 1 '16 at 21:44
• \expandafter\string\the\font – egreg May 1 '16 at 21:49

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}

\begin{document}
This text is in the main font, Garamond.
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
]
]

\begin{document}
This text is in the main font, Garamond.

\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.