3

I know LaTeX doesn't have strings as such but I want to be able to get and save the human readable version of the current font name.

\f@family does not work as expected because it shows the base font not the variant.

I saw somewhere on here an answer from @DavidCarlisle which gets the information I want but I can't find that answer again. It used \expandafter\meaning\the\font and it does print what I need plus stuff I don't.

How can I "save" that information and extract the substring which is just the human friendly font name? So it would capture NotoSerif-ExtraBold from example c below.

I thought the source code for fontspec might show me but I can't understand it.

I have seen \StrBetween from xstring but I can't make it work.

StrBetween ( \StrBetween{\expandafter\meaning\the\font}{s}{t} )

has nothing between the ears ( ) I think it should at least print elec since the output starts select font. I thought may StrBetween was seeing the actual \expandafter... so I tried \expandafter\expandafter... but that did not work either.

Minimal Not Working Example

\documentclass{article}
\usepackage{fontspec,xstring}

% I don't know if you are supposed to load the packages
\usepackage{AnonymousPro}
\usepackage{noto}

% The font choices are deliberately silly to show the font name printing is working
\setmainfont{NotoSerif-Medium}[
     BoldFont       = NotoSerif-ExtraBold
    ,ItalicFont     = NotoSerif-ExtraLightItalic
    ,BoldItalicFont = NotoSerif-BlackItalic
]
\setsansfont{NotoSans-CondensedMedium}[
     BoldFont       = NotoSans-ExtraCondensedBlack
    ,ItalicFont     = NotoSans-LightItalic
    ,BoldItalicFont = NotoSans-CondensedSemiBoldItalic
]
\setmonofont{NotoSansMono-Regular}[
     BoldFont       = NotoSansMono-ExtraCondensedExtraBold
    ,ItalicFont     = AnonymousPro-Italic
    ,BoldItalicFont = AnonymousPro-BoldItalic.ttf
    % could not get it to work without the .ttf
]

\makeatletter
\newcommand*{\howbig}{\f@size}
\newcommand*{\whatfontami}{\f@family}
\makeatother


\begin{document}
a. \normalfont serif normal \rmdefault\ \howbig\ pt

b. \normalfont serif normal \expandafter\meaning\the\font\ \howbig\ pt

c. \normalfont serif bold \bfseries\expandafter\meaning\the\font\ \howbig\ pt

d. \normalfont sans italic \sffamily\expandafter\meaning\the\font\ \howbig\ pt

e. \normalfont mono bold italic \ttfamily\bfseries\itshape\expandafter\meaning\the\font\ \howbig\ pt
\end{document}

correct but unusable output

which used \expandafter\meaning\the\font to print some font information, for example:

. The font name I w

1 Answer 1

4

Is this what you want? Sorry for not mixing the mono fonts, but there are problems with AnonymousPro.

\documentclass{article}
\usepackage{fontspec}

% The font choices are deliberately silly to show the font name printing is working
\setmainfont{NotoSerif}[
  UprightFont    = *-Medium,
  BoldFont       = *-ExtraBold,
  ItalicFont     = *-ExtraLightItalic,
  BoldItalicFont = *-BlackItalic,
  Scale = 0.9
]
\setsansfont{NotoSans}[
  UprightFont    = *-CondensedMedium,
  BoldFont       = *-ExtraCondensedBlack,
  ItalicFont     = *-LightItalic,
  BoldItalicFont = *-CondensedSemiBoldItalic,
  Scale = MatchUppercase,
]
\setmonofont{AnonymousPro}[
  Extension=.ttf,
  UprightFont    = *-Regular,
  BoldFont       = *-Bold,
  ItalicFont     = *-Italic,
  BoldItalicFont = *-BoldItalic,
  Scale = MatchUppercase,
]

\ExplSyntaxOn
\NewDocumentCommand{\extractfont}{}
 {
  \str_set:Nx \l_tmpa_str { \fontname\font }
  \regex_replace_once:nnN { \:.* } { } \l_tmpa_str
  \regex_replace_once:nnN { \A \[(.*)\] \Z } { \1 } \l_tmpa_str
  \regex_replace_once:nnN { \.[^\.]* \Z } { } \l_tmpa_str
  \str_use:N \l_tmpa_str
 }
\ExplSyntaxOff

\makeatletter
\newcommand*{\howbig}{\f@size}
\newcommand*{\whatfontami}{\f@family}
\makeatother


\begin{document}

a. {serif normal \rmdefault\ \howbig\ pt}

b. {serif normal \extractfont\ \howbig\ pt}

c. {serif bold \bfseries\extractfont\ \howbig\ pt}

d. {sans italic \sffamily\extractfont\ \howbig\ pt}

e. {mono bold italic \ttfamily\bfseries\itshape\extractfont\ \howbig\ pt}

\end{document}

enter image description here

The primitive construct \fontname\font provides the current font name (without select font at the beginning) and I remove everything from the first colon inclusive, then possible brackets [] around the font name and finally a possible extension (from a period followed by characters not including periods up to the end).

1
  • Perfect, thank you @egreg. Your code that strips the unwanted parts is even better than I had hoped for. The exact mono font didn't matter. I was just picking fonts with different names to test the code really was picking the current font.
    – Doc Octal
    Sep 3, 2021 at 22:56

Your Answer

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

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