5

I am using LuaTeX with TeXlive 2016, Ubuntu. Using fontspec to create a new font family. MWE:

% !TeX program = LuaLaTeX
% !TeX encoding = UTF-8
\documentclass{minimal}
\usepackage{fontspec}
\newfontfamily\MyPrivate{Latin Modern Roman} % or some other font
\begin{document}
Your private font is % \whatisMyPrivate
\end{document}

I seek \whatisMyPrivate, so that I can uncomment that line and have the document print out the name of the font (in this case, Latin Modern Roman). I see that the \newfontfamily command is defined in file fontspec-luatex.sty, but alas I cannot decipher where it stores the information for subsequent read-back.

I am hoping to do this without actually setting any text in the private font. But if the only way to do this is to use the font to set some text, then read-back from within that text, then that's OK.

5

You could for example apply the font in a group and extract the altered definition of \f@family which holds the font name. I wrapped this into a higher level macro.

\documentclass{article}
\usepackage{fontspec}
\newfontfamily\MyPrivate{Latin Modern Roman} % or some other font
\def\getfontname#1{%
  {#1\expandafter\xdef\csname private_font_name\endcsname{\csname f@family\endcsname}}%
  \csname private_font_name\endcsname
}
\begin{document}
Your private font is \getfontname\MyPrivate
\end{document}

enter image description here

  • Perfect. Reason I asked: I like to have the compiler write a sidecar text file with a summary of certain info. Once I know how to get the info, I can put it in easy-to-read form in the summary. – user103221 Oct 7 '16 at 20:03
2

You can get the family name by pure expansion.

The trick is knowing that the assigned family name is stored in the internal variable \g__fontspec_MyPrivate_family:

\documentclass{article}
\usepackage{fontspec}

\ExplSyntaxOn
\DeclareExpandableDocumentCommand{\getfamilyname}{m}
 {
  \use:c { g__fontspec_ \cs_to_str:N #1 _family }
 }
\ExplSyntaxOff

\newfontfamily\MyPrivate{Latin Modern Roman} % or some other font

\begin{document}

Your private font is \getfamilyname\MyPrivate

% emulate a \write operation
\edef\test{\getfamilyname\MyPrivate}

\texttt{\meaning\test}

\end{document}

enter image description here

The final example shows that if you do

\write\baz{\getfamilyname\MyPrivate}

(where \baz is an output stream), the requested family name will be written.

However fontspec doesn't provide a public interface for accessing font family data, so this method might fail in future releases. Actually, recording the particular internal name assigned to the font family is not useful at all. In case an explicit name is needed, the manual explains how (and why) to assign one: see section 5.2.

\newfontfamily\MyPrivate{Latin Modern Roman}[
  NFSSFamily=foo,
  % <other options>
]

The family name assigned will be foo.

  • I'll have a look at that. "Might fail in future release" always makes me nervous, since I hope to circulate some code to others, and don't want to get into a fix-it loop. Sure wish that TeX had a try-catch mechanism, but I know (having searched) that it does not. – user103221 Oct 8 '16 at 15:16
  • @RobtA You're starting with the wrong foot: the fact that the string LatinModernRoman(0) is assigned might change in the future, too, so the knowledge of this string is pretty useless. If you want a foolproof method, that of assigning the family name yourself is the only one. – egreg Oct 8 '16 at 15:18
  • Thanks for that info. Only reason for knowing the string is to write it into a sidecar file, not into the PDF. If the string name changes, that will not hurt, as long as the code does not throw an error. Of course, the PDF reader tells me which fonts are embedded, but my sidecar will tell me where the fonts are being used (header or main text). This is for subsequent review by someone who does not see the original *.tex file and does not know what code was used there. The sidecar is not essential, so I will omit code that may bomb. – user103221 Oct 8 '16 at 15:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy