[XeTeX] Determining current font

Will Robertson wspr81 at gmail.com
Tue Jun 10 00:37:44 CEST 2008


On 10/06/2008, at 7:26 AM, James Crippen wrote:

> On Sun, Jun 8, 2008 at 8:52 PM, Will Robertson <wspr81 at gmail.com>  
> wrote:
>> James wanted to be able to query the current font in use. The best  
>> way to do
>> this, in my opinion (and in this particular circumstance), is to  
>> ignore what
>> \the\font says and go with \fontname. Here's a macro that should do  
>> what you
>> want:
>>
>> \documentclass{article}
>> \usepackage{fontspec}
>> \begin{document}
>> \makeatletter
>> \newcommand\IfFontIs[1]{%
>> \edef\@tempa{\detokenize{#1}}%
>> \edef\@tempb{\fontname\font}%
>> \expandafter\expandafter\expandafter\in@
>> \expandafter\expandafter\expandafter{%
>> \expandafter\@tempa\expandafter}\expandafter{\@tempb}%
>> \ifin@
>>   \expandafter\@firstoftwo
>> \else
>>   \expandafter\@secondoftwo
>> \fi}
>
> Of course, for everyday use we'll need \makeatother right here.
>
> I'm not even going to pretend that I can figure out all those
> \expandafter calls.

:)
They're easy enough to write, just hard to read.
It would have been easier for me and made more sense to you if I'd  
just written

   \edef\@tempa{%
     \noexpand\in@{\detokenize{#1}}{\fontname\font}}%
   \@tempa

But I started with expandafters and didn't think until now this would  
have been nicer :)

> Is this possible? It's probably asking a lot, and I won't be sad if it
> requires too much black magic.

Never one to pass up a challenge in the spare hours in the morning!

<some time later>

Hmmm, that took a little longer than I hoped. See rather ugly code  
attached below. It's probably not 100% foolproof, but see how you go.

> I heartily recommend that you add \IfFontIs to fontspec, perhaps
> naming it something like \ifcurrentfont.

Not such a bad idea. I'll have a bit of a think about it, but I don't  
see why not!

(Although in its current form I'd prefer to use capitalised macros; in  
my opinion, \ifcurrentfont looks like it should behave like a TeX- 
style \iftrue..\else..\fi, whereas \IfCurrentFont looks like it should  
behave like an \IfSomething{true}{false}-style construction.)

All the best,
Will

***

\documentclass{article}
\usepackage{fontspec}
\begin{document}
\makeatletter
\newcommand\IfFontIs[1]{%
   \edef\@tempa{%
     \noexpand\in@{\detokenize{#1}}{\fontname\font}}%
   \@tempa
   \ifin@
     \expandafter\@firstoftwo
   \else
     \expandafter\@secondoftwo
   \fi}

\fontspec{Hoefler Text}
\IfFontIs{Hoefler Text}{yes}{no}
\IfFontIs{Minion Pro}{yes}{no}

\newfontfamily\myfont{Skia}
\def\zf at getzf\fontfamily#1\selectfont{#1}
\def\zf at extract#1(#2){#1}
\def\zf at extractname"#1/#2"{#1}

\newcommand\IfFontMacroIs[1]{%
   \edef\@tempa{\expandafter\zap at space\fontname\font! \@empty}%
   \edef\@tempa{\expandafter\zf at extractname\@tempa}%
   \edef\@tempb{%
     \expandafter\expandafter\expandafter
     \zf at getzf\csname\expandafter\@gobble\string#1 \endcsname}%
   \edef\@tempb{%
     \expandafter\zf at extract\@tempb}%
   \edef\@tempc{%
     \noexpand\in@
       {\expandafter\detokenize\expandafter{\@tempb}}
       {\expandafter\detokenize\expandafter{\@tempa}}}%
   \@tempc
   \ifin@
     \expandafter\@firstoftwo
   \else
     \expandafter\@secondoftwo
   \fi}

\fontspec{Skia}
\IfFontMacroIs{\myfont}{yes}{no}

\newfontfamily\fontb{Warnock Pro}
\fontspec[Numbers=OldStyle]{Warnock Pro}
\IfFontMacroIs{\fontb}{yes}{no}
\IfFontMacroIs{\myfont}{yes}{no}

\end{document}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 2415 bytes
Desc: not available
Url : http://tug.org/pipermail/xetex/attachments/20080610/665dedc0/attachment.bin 


More information about the XeTeX mailing list