# Creating mathXxxx type macros for Unicode private use area

I have a custom font with a dozen or so scripts 0-9,A-Z,a-z in the private use area. I need to create macros like symcal() and mathcal() for those scripts. I have something that works -- but it is clearly wrong. Since I am trying to get this in shape to put up on ctan, I don't want a kludge that might not work for someone else. Here is my current attempt

\documentclass[10pt]{article}

\usepackage{mathtools}
\usepackage{fontspec}
\usepackage{unicode-math}
\usepackage{lualatex-math}

\setmainfont{STIX Two Text}[NFSSFamily=mainfont]
\setmathfont{STIX Two Math}[NFSSFamily=mathfont]

\DeclareSymbolFont{MathFont}{TU}{mathfont}{m}{n}

\newcommand \setMathScript {
\Umathcode "31 = 7 \symLogix "F431
\Umathcode "41 = 7 \symLogix "F5C1
\Umathcode "42 = 7 \symLogix "F5C2
\Umathcode "43 = 7 \symLogix "F5C3 }

\newcommand \clrScript {
\Umathcode "31 = 7 \symMathFont "31
\Umathcode "41 = 7 \symMathFont "1D434
\Umathcode "42 = 7 \symMathFont "1D435
\Umathcode "43 = 7 \symMathFont "1D436 }

\newcommand \mathCal[1]   {\setMathScript #1 \clrScript}

\begin{document}

$A 1 B C \mathCal{ A 1 B C } A 1 B C$

\end{document}


and here is the result.

The first four symbols are the default in math mode. The second four are the result of applying my \mathCal{} macro. The last four are the result of attempting to reset back to the default math mode.

While this works, there are two problems. First, I had to use NFSSFamily=mathfont and \DeclareSymbolFont. Second, I had to hard code the locations of the symbols for the default math font. I cannot assume that a user of the package has done the first or that the second is correct.

Additionally, each of these scripts have a base, in this case F400 for the digits and F580 for the letters. I would prefer to be able to pass in the base(s) and then write something like \add{\base}{"41}. That would substantially reduce the number of lines needed in the LaTeX code.

You do not need to reset the mathcodes to a fixed set, you can use grouping, this typesets as aba for example

$a {\mathcodea=\mathcodeb a} a$

\bye


Also you can use arithmetic in your settings, for example

  \Umathcode "41 = 7 \symMathFont "1D434


can be written as

\count@="41
\Umathcode \count@ = 7 \symMathFont \numexpr "1D3F3 + \count@\relax


which makes it more amenable to being in a loop.

• Thanks for the suggestion.The \numexpr will be very useful. As far as the first part with mathcode, I do not see anywhere that I can specify the font family, which is essential since the custom font is NOT the default font. I was thinking some sort of grouping as in an environment would probably do the trick, so that is probably going in the right direction. Jun 4, 2019 at 22:18
• @MichaelLeeFinney the mathcode encodes the font family, you don't need to do it at the point of the switch (I used \mathcode but you want \Umathcode here of course) Jun 4, 2019 at 22:20
• I do not see a way to set the math code for a symbol in a different font because there is no font family on the left of the equals. So I tried\Umathcode "31 = \Umathcode {\Umathchar 7 \symLogix "F431} but while LuaLaTeX accepted it at the point of definition, it did not like where it was used. Without being able to set the mathcode in the custom font, I still don't see how I can use this. Jun 4, 2019 at 22:42
• The Umathcode encodes the font.  \Umathcode "41 = 7 \symMathFont "1D434 specifies that character hex 41 comes from font family \symMathFont so you do not need anything else. @MichaelLeeFinney Jun 4, 2019 at 22:46
• Thank you. I got that to work! It needed an extra set of braces to get the grouping. I will update my answer showing the solution that worked. Jun 4, 2019 at 22:57

Thanks to David Carlisle the example is now working with both addition and resetting the environment. Here is the working solution.

\documentclass[10pt]{article}

\usepackage{mathtools}
\usepackage{fontspec}
\usepackage{unicode-math}
\usepackage{lualatex-math}

\setmainfont{STIX Two Text}
\setmathfont{STIX Two Math}

\newcommand \calNumbrBase {"F400}
\newcommand \calAlphaBase {"F580}

\newcommand \setScript {
\Umathcode "31 = 7 \symLogix \numexpr \calNumbrBase + "31\relax
\Umathcode "41 = 7 \symLogix \numexpr \calAlphaBase + "41\relax
\Umathcode "42 = 7 \symLogix \numexpr \calAlphaBase + "42\relax
\Umathcode "43 = 7 \symLogix \numexpr \calAlphaBase + "43\relax }

\newcommand \mathCal[1]   {{\setScript #1}}

\begin{document}

$A 1 B C \mathCal{ A 1 B C } A 1 B C$

\end{document}