4

I do \font\testFont="Arial"\testFont to set the font to Arial (or to any font I need), but after I'm done displaying some characters I need to revert to previous font. What can be done to achieve this, aside from enclosing the above font change within {} (a group)? I use XeTeX so you can rely on XeTeX-related functionality, but non-XeTeX solutions (that would work there) are welcome too.

bp2017
  • 3,756
  • 1
  • 13
  • 33
  • 2
    if you are using latex why are you using primitive \font loading at all? – David Carlisle Nov 19 '19 at 20:37
  • 1
    it's like saying it's helpful to insert assembly code into a C program, yes it's possible to get some optimisations but it's also possible to completely break the system. Latex font handling is built around a coherent mechanism for tracking font size, baselines font styles etc, if you insert a primitive font selection in to the mix then anything that happens is accidental untested code path, if it works it works if it does anything else, it is unsupported and a user error. – David Carlisle Nov 19 '19 at 21:19
  • 1
    See \the\font under “Special uses” in https://tex.stackexchange.com/a/38680/4427 – egreg Nov 19 '19 at 21:34
  • 1
    \font itself isn't especially bad it is the general principle I am questioning that using tex primitives in a latex document is somehow normal. It is open source and due to the macro expansion nature of the system hard to prevent, but if you use commands (that are not mentioned in any user facing latex documentation) like \font or \countdef or for that matter \def, then at some point something will break, but however it breaks it will, by definition, be user error. – David Carlisle Nov 19 '19 at 21:40
  • 1
    @DavidCarlisle Wouldn't that rule out using most third party packages, since most use \def and many have to, given the limitations of LaTeX. (l3 changes this quite a bit, but setting that aside.) [I'd agree that using \def where you could use \newcommand or whatever is unnecessarily dangerous, but you can't avoid all the dangers. The path is full of perils.) – cfr Nov 20 '19 at 01:20
  • @cfr package code is package code, but that doesn't mean you should use arbitrary tex primitives in a document. – David Carlisle Nov 20 '19 at 01:27
  • When I look into packages to assess their quality seeing a primitive \font counts as warning - normally it indicates that either the package is very old and not maintained or that the author didn't understand the LaTeX font system. – Ulrike Fischer Nov 20 '19 at 07:44
  • 1
    @bp2017 in package code then the font should definitely not be loaded via \font. Any user of the package after selecting the font is going to expect \large to make the text larger and that will not happen if you switch using a command defined with \font, or rather the text will get larger but be in the font that the latex font system thinks is current ie the last font loaded via a supported command. – David Carlisle Nov 20 '19 at 09:17

1 Answers1

8

You can expand \font with \the, so \edef\lastfont{\the\font} should work (notice how the name "Arial" comes out different when typeset... weird ;-)

enter image description here

\edef\lastfont{\the\font}
Computer Modern

\font\testFont="Arial"\testFont
Arial

\lastfont
Computer Modern Again

\bye