What are the advantages of using \Umathchardef over \Umathcode with \let?

First, there is performance: \Umathchardef will be slightly faster (there is less indirection after all), but that shouldn't be relevant in most cases.

On the other hand, setting the \Umathcode for + first and then using \let to define your symbol also sets the right \Umathcode for +, so you can also use + directly. Of course, even if you use \Umathchardef you can still assign the \Umathcode, but then they are not connected: If someone changes the \Umathcode for +, the command \plus defined as \let\plus=+ changes too, but \Umathchardef has to be changed separately. This is especially important in e.g. unicode-math which changes mathcodes a lot.

Also, they behave differently outside of math mode: For XeTeX, a command defined by \Umathchardef always yields an error outside of math mode. (In LuaTeX, it will insert the corresponding character from the text font.) While the \let based command always inserts the character the command was \let to in text mode. What is better again depends on the character: For \sum, there is no point in allowing it outside of math mode, so it should create an error there. On the other hand for \alpha, you might want the name to also allow inserting the greek character in text mode. In that case you could for example do

\Umathcode"03B1=2 0 "1D6FC
\let\alpha=α

then your math \alpha is italic math alpha , while your text mode \alpha is still a regular text α.

So \Umathchardef is a bit faster, has better error handling in XeTeX and doesn't introduce hidden dependencies on the mathcode of some character, but \Umathcode and \let are more flexible and might be shorter if you set the \Umathcode anyway.


hyperref won't like it if you \let a command to a char. It is tricky to detect such commands and the hyperref code currently can't do it (this will perhaps change but not directly). There are a number of bug reports at the hyperref and unicode-math github about this, e.g. https://github.com/latex3/hyperref/issues/63 and https://github.com/wspr/unicode-math/issues/532. Instead of using \let it is better to use \def (\newcommand).

In the bookmarks you will see differences too. The \Umathchardef variant is currently lost (this will perhaps change too but also not directly).

\documentclass{article}

\Umathchardef\plusmathchar = 2 0 "002B

\Umathcode "002B = 2 0 "002B
\let\pluslet=+

\Umathcode "002B = 2 0 "002B
\newcommand\plusdef{+}

\usepackage{hyperref}
\begin{document}
$a$ % initialize math fonts

text: M \plusmathchar\ L \pluslet\ D  \plusdef  %error with xelatex

math: $ M \plusmathchar L \pluslet D \plusdef  $


\tableofcontents
\section{mathchar text X \plusmathchar\ Y} % error with xelatex
\section{mathchar math X $\plusmathchar$ Y}

%\section{let text X \pluslet\ Y} %lots of errors with hyperref
%\section{let math X $\pluslet$ Y}%lots of errors with hyperref 

\section{def text X \plusdef\ Y}
\section{def math X $\plusdef$ Y}

\end{document}

output with lualatex

Compiled without hyperref and with the commented part:

enter image description here

Bookmarks:

enter image description here


We can check the difference of these two assignments in the log:

\Umathchardef\plus = 2 0 "002B
\show\plus

\Umathcode "002B = 2 0 "002B
\let\plus=+
\show\plus
\bye
> \plus=\Umathchar"2"00"00002B.
l.2 \show\plus

? 
> \plus=the character +.
l.6 \show\plus

? 

The fundamental difference is that with \Umathchardef\plus you get a token that represents this particular math symbol in the output, whereas with \let\plus=+ you make an alias for the + character in the input.

Another problem with the second variant is that it is non-local, i.e. the assignment of the mathcode and the definition of the control sequence do not take place in the same statement.

Also, the first statement is how it is meant to be done™. From the TeXbook:

A~hundred or so definitions like
\begintt
\def\sum{\mathchar"1350 }
\endtt
would therefore suffice to define the special symbols of plain \TeX\null. But
there is a better way: \TeX\ has a primitive command ^|\mathchardef|,
which relates to |\mathchar| just as ^|\chardef| does to |\char|.
Appendix~B has a hundred or so definitions like
\begintt
\mathchardef\sum="1350
\endtt
to define the special symbols.

Tags:

Xetex

Luatex