# How specify by Unicode number a particular character from another font (XeLaTeX)?

The following defines commands \cmark and \xmarkto produce the Unicode characters with codes U+2718 and U+2714, respectively.

Question:

Is there some simpler, or alternative, way that the following to specify an individual character, from a different font than the main one in use — when only one or two characters from that font are going to be used?

% !TeX program =xelatexmk
\documentclass{article}
\usepackage{fontspec}
\usepackage{unicode-math}
\setmainfont{TeX Gyre Termes}
\setmathfont{TeX Gyre Termes Math}

% How  create command for character that has Unicode U+2718?
\newfontfamily{\xmarkfont}{LibertinusMath-Regular.otf}
\newcommand{\xmark}{\xmarkfont\char"2718}
\newfontfamily{\cmarkfont}{TeX Gyre Pagella Math}
\newcommand{\cmark}{\xmarkfont\char"2714}

\begin{document}
\begin{tabular}{lc}
Wrong & \xmark\\
Right & \cmark
\end{tabular}

\end{document}


• Why not just load the bbding package and type \CheckmarkBold or \XSolidBrush ? – Fran Jul 11 at 1:24

You would really want to change the font inside a group, so the font change doesn’t bleed through to the text after it. So,

\newcommand{\xmark}{{\xmarkfont\char"2718}}


You would define \xmarkfont as you did in your MWE, to a font that has it, probably scaled with Scale=MatchUppercase.

In math mode with unicode-math, you can set the font for a specific math character with the range= option of \setmathfont.

You can use newunicodechar to set a character active, which allows you to paste it into your source, e.g.

\usepackage{newunicodechar}
\newunicodechar{^^^^2718}{\xmark}


This lets you type either \xmark, ઞ, or ^^^^2718 and have it just work.

In XeLaTeX, you can theoretically use ucharclasses to set the font for all dingbats, or any other Unicode block. As of July 2020, however, this package is broken and apparently unmaintained.

If you want to use the character from the current font if and only if the font has it, and fall back if and only if it does not, you would define the \xmark command as a conditional with \iffontchar.

In XeTEX, it is possible, but complicated, to insert commands (such as \begingroup\symbolfont and \endgroup) around a particular group of characters with \XeTeXinterchartoks. This lets you type them in and select the font correctly without explicit markup, and is what ucharclasses is supposed to be doing.

• :: But apparently, Unicode U+2718 does not have a name in at least some of the fonts that include it. – murray Jul 11 at 3:11
• This answer does not explain how to specify the particular font from which the character will be taken. That's precisely the underlying problem. The default font I'm using, TeX Gyre Termes, does not seem to have this character. (Or am I wrong about that?) In any case, in order to better match the corresponding character from pifont, I will want to use a font other than the default one! – murray Jul 11 at 3:13
• @murray But the question already did that: you selected it from Libertinus Math. – Davislor Jul 11 at 3:34
• @murray There is a way to define an error font in LaTeX, but that’s for when it can’t find a particular font face that you specify. – Davislor Jul 11 at 3:38
• So first using a \newfontfamily is reasonable even if only one or a couple of characters from that font is going to be used -- it doesn't overburden TeX with font families in use? – murray Jul 11 at 14:19