10

Is it possible, using XeLaTeX and fontspec.sty, to write a macro, say, \printcurrentfont, such that it would print the name of the font currently in use? I have looked through the fontspec documentation, but so far have not found an answer.

2
  • What is the name you want to see? file name? internal font name? family name used by fontspec? Apr 10, 2016 at 15:39
  • Any and all of those, depending on the information that I need to output. I am putting together a number of samples of typefaces and I would like to label them with the name of the current font being sampled.
    – sgmoye
    Apr 10, 2016 at 15:45

2 Answers 2

12

You can access the internal and the external names of the current font respectively with \the\font and \fontname\font. The former must be stringified in order to print it.

Further massaging of the external font name can be added.

\documentclass{article}

\usepackage{fontspec}

\setmainfont{Libertinus Serif}
\setsansfont{TeX Gyre Adventor}[Scale=MatchUppercase]
\newfontfamily{\junicode}{Junicode}

\DeclareTextFontCommand{\textttup}{\normalfont\ttfamily}

\newcommand{\printinternalcurrentfont}{%
  \expandafter\textttup\expandafter{\expandafter\string\the\font}%
}
\newcommand{\printexternalcurrentfont}{%
  \expandafter\textttup\expandafter{\fontname\font}%
}

\begin{document}

\printinternalcurrentfont

\printexternalcurrentfont

\bigskip

{\itshape\printinternalcurrentfont\par\printexternalcurrentfont}

\bigskip

\sffamily

\printinternalcurrentfont

\printexternalcurrentfont

\bigskip

\junicode

\printinternalcurrentfont

\printexternalcurrentfont

\end{document}

Output with XeLaTeX

enter image description here

Output with LuaLaTeX

enter image description here

If you're not interested in the finer details, but just in the main font name part, change the definition of \printexternalcurrentfont into

\makeatletter
\newcommand{\printexternalcurrentfont}{%
  \expandafter\format@externalcurrentfont\fontname\font:\@nil
}
\def\format@externalcurrentfont#1:#2\@nil{%
  \textttup{\@ifnextchar"{\@gobble}{}#1}%
}
\makeatother

The output would become

enter image description here

9
  • \fontname\font reveals the main font name, how does one reveal the mono or sans font name?
    – MatteoS
    Jun 4, 2018 at 12:53
  • 2
    @MatteoS {\sffamily\printinternalcurrentfont\par\printexternalcurrentfont} for the sans font, use \ttfamily instead for the mono font.
    – egreg
    Jun 4, 2018 at 12:57
  • I don't get that. The format I'm getting for \printexternalcurrentfont is the TTF font file name in square brackets before the /OT. How would I go about getting just the font name without the square brackets or the .ttf or the /OT or anything else? Jul 16, 2020 at 22:13
  • @PeterFlynn It depends on how you load the font. In the example I loaded fonts installed system-wide.
    – egreg
    Jul 16, 2020 at 22:22
  • Same, but it was my fault: I was reading the wrong command (int not ext). Jul 18, 2020 at 22:19
1

It turns out that the usable answer is much simpler:

\newcommand{\printexternalcurrentfont}{%
  \expandafter\getfontname\fontname\font:\@nil}
\def\getfontname"[#1.#2\@nil{#1}

You just need to parse out all the stuff except the actual font name. Unfortunately it does not give you the user-friendly name (eg Raleway Regular) but retains the hyphens. But it's close.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .