# [luatex] First question: Retrieve *family* name of a font / the three main families

Urs Liska lists at openlilylib.org
Tue Jan 23 17:06:57 CET 2018


Am 22.01.2018 um 10:53 schrieb Ulrike Fischer:
> Am Sun, 21 Jan 2018 22:33:56 +0100 schrieb Urs Liska:
>
>>> \csname TU/\rmdefault/m/n\endcsname
>>>
>>> and extract the font name "Arial" from the output:
>>>
>>>> \TU/Arial(0)/m/n=macro:
>>> ->\<->"Arial:mode=node;script=latn;language=DFLT;+tlig;" .
>>>
>> I was just about to test what you had suggested earlier (didn't have
>>
>> I have a hard time to understand your code examples. As far as I can see
>> your original code "shows" the definition of \rmdefault in certain
>> variants, and it puts in in the log output. If that's correct how would
>> I get it not to print to the console but be stored somewhere I can
>> process it further, and how would I process it in a way that LaTeX or
>> Lua's objects can tell me the "family"?
> Something like this should work:
>
> \documentclass{article}
> \usepackage{expl3}
> \usepackage{fontspec}
> \setmainfont{Arial}
> \begin{document}
>
> \ExplSyntaxOn
> \newcommand\currentromanfamily{}
> \cs_generate_variant:Nn \regex_extract_once:nnNTF {no}
>
> \tl_set:Nx \l_tmpa_tl { \tl_to_str:c {TU/\rmdefault/m/n}}
>
> \regex_extract_once:noNTF
>   { \"(.+?)\: }
>   {\l_tmpa_tl}
>   \l_tmpa_seq
>   {
>    \tl_set:Nx\currentromanfamily {\seq_item:Nn \l_tmpa_seq {2}}
>   }
>   {
>    No Match
>   }
>
> \ExplSyntaxOff
>
> \currentromanfamily
>
> \end{document}
>
>
>

Indeed this prints the name of the font specified with \setmainfont, but
I must admit I don't understand any of that code :-(

In order for this to work I would need a) either the family name (which
it isn't) or the font id (from which I can get to the family and b) all
three names in one invocation.

With the help of Will Robertson I came up with another approach that
works - almost.
WIth the following code I correctly have the three family names stored
in a Lua table:

\documentclass{article}
\usepackage{fontspec}
\setmainfont{texgyrepagella-regular.otf}
\begin{document}

\sffamily

We're in Sans
\begingroup%
\rmfamily%
\directlua{
myfonts = {}
}%
\sffamily%
\directlua{
}%
\ttfamily%
\directlua{
print("")
print('Main font: '..myfonts.rm)
print('Sans font: '..myfonts.sf)
print('Mono font: '..myfonts.tt)
}%
\endgroup%
and we should be back in Sans.

\end{document}

This iterates over the three fonts - without actually printing anything
- and treats every one as the "current" one.
Although I must admit this approach is somewhat strage - like having to
start a car's engine in order to determine its colour.

However, one problem is left which I didn't manage to solve: I need all
this from within one Lua function. But when I wrap everything in *one*
Lua chunk the font selection doesn't seem to work like other
tex.print()-ing:

\begingroup%
\rmfamily%
\directlua{
myfonts = {}
tex.print('\noexpand\\rmfamily')
tex.print('\noexpand\\sffamily')
tex.print('\noexpand\\ttfamily')
print("")
print('Main font: '..myfonts.rm)
print('Sans font: '..myfonts.sf)
print('Mono font: '..myfonts.tt)
}%
\endgroup%

While this compiles the three fonts always show the font that is active
before the function starts to execute.

So is there a way to select one of the three predefined font families
*from within Lua* so subsequent statements in the same Lua chunk see the
results?

Urs