# XeLaTeX, fontspec and printing the name of the current font

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.

• What is the name you want to see? file name? internal font name? family name used by fontspec? – Ulrike Fischer Apr 10 '16 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 '16 at 15:45

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}
\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 LuaLaTeX

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

• \fontname\font reveals the main font name, how does one reveal the mono or sans font name? – MatteoS Jun 4 '18 at 12:53
• @MatteoS {\sffamily\printinternalcurrentfont\par\printexternalcurrentfont} for the sans font, use \ttfamily instead for the mono font. – egreg Jun 4 '18 at 12:57