8

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
8

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

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

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.