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.
Asked
Active
Viewed 1,212 times
10
sgmoye
- 8,586
-
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
2 Answers
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
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
egreg
- 1,121,712
-
\fontname\fontreveals the main font name, how does one reveal the mono or sans font name? – MatteoS Jun 04 '18 at 12:53 -
2@MatteoS
{\sffamily\printinternalcurrentfont\par\printexternalcurrentfont}for the sans font, use\ttfamilyinstead for the mono font. – egreg Jun 04 '18 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? – Peter Flynn Jul 16 '20 at 22:13
-
@PeterFlynn It depends on how you load the font. In the example I loaded fonts installed system-wide. – egreg Jul 16 '20 at 22:22
-
Same, but it was my fault: I was reading the wrong command (int not ext). – Peter Flynn Jul 18 '20 at 22:19
-
Note that in the external font, with XeLaTeX the "/OT" is included, while with LuaLaTeX it is not. For consistency, one must strip the "/OT" part in the XeLaTeX case as well. – ShreevatsaR Dec 23 '20 at 09:21
-
Actually not just the "/OT" mentioned in the previous comment, but there are other inconsistencies between xelatex and lualatex: with the former there are spaces and (for only some fonts) a "Regular" suffix that is not present with lualatex. Oh well… giving up on consistency. – ShreevatsaR Dec 23 '20 at 09:30
-
@ShreevatsaR For instance, italic might be denoted by appending
/Iwith XeLaTeX and withItalicwith LuaLaTeX. Different approach by the engine (actually byluaotfloadin case of LuaLaTeX). – egreg Dec 23 '20 at 09:39 -
@egreg Ah I see, thanks for explaining! The situation is not ideal, but makes sense. – ShreevatsaR Dec 23 '20 at 10:14
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.
Peter Flynn
- 2,870


