[Tex/LaTex] Accents and unicode-math

accentsunicodeunicode-mathxetex

Why are certain accents in math mode invisible? I am using unicode-math and compile with XeLaTeX. Here is a MWE:

\documentclass{scrartcl}
\usepackage{unicode-math}
\begin{document}
x̃ŷz̄ $x̃ŷz̄$
\end{document}

This produces accents on the text mode letters but not on the math mode ones. Strange!

Best Answer

There is no Unicode “LATIN SMALL LETTER X WITH TILDE” so you get “x̃” by typing x followed by U+0303 “COMBINING TILDE”.

This can't work in math mode, which strictly works from left to right and a following character doesn't influence what's before it.

You can make existent Unicode points such as “LATIN SMALL LETTER O WITH TILDE” to behave like \tilde{o} in math mode with a trick such as

\AtBeginDocument{\mathcode`õ="8000 }
\begingroup\lccode`~=`õ
\lowercase{\endgroup\def~}{\tilde{o}}

One can think to do something like this for x (doing a lookahead to see if a combining character follows) but it would be slow and not particularly robust. It's really better to make your intentions clearer by typing

\tilde{x}

Just as a proof of concept, here's how you could manage (I also left the precomposed õ). The \@ifnextchar test should be extended to the other needed composite characters.

\documentclass{scrartcl}
\usepackage{unicode-math}

\AtBeginDocument{\mathcode`õ="8000 }
\begingroup\lccode`~=`õ
\lowercase{\endgroup\def~}{\tilde{o}}

\AtBeginDocument{\edef\mathcodex{\Umathcharnum\the\Umathcodenum`x }}
\AtBeginDocument{\mathcode`x="8000 }
\begingroup\lccode`~=`x
\lowercase{\endgroup\def~}{\addaccentx}

\makeatletter
\newcommand\addaccentx{%
  \@ifnextchar ^^^^0303{\tilde{\mathcodex}\@gobble}{\mathcodex}%
}
\makeatother

\begin{document}
x̃ŷz̄ $õx̃$
\end{document}

enter image description here

Related Question