# Accents and unicode-math

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̃ŷz̄ $x̃ŷz̄$
\end{document}


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

• There is no Unicode "x with tilde"; combining characters do not work in math mode, because they come after the main character. – egreg Nov 11 '13 at 10:14
• TeX separates text accents \~ from math accents \tilde with good reason, the tilde in math is presumably denoting some mathematical operator and might potentially be applied to an arbitrary subterm rather than just a base letter. Therefore you don't want to use the combining character or pre-composed glyph in math even if the font has it, so that the same tilde construct is used as for built up expressions, it is also clearer in the input to mark this up as an operation on x rather than as a different character than just happens to look like an x with a tilde. – David Carlisle Nov 11 '13 at 11:30
• In my case x̃ is a symbol just like x, not an operator. Admittedly, in some situations \tilde{some_expression} is an operator, this tilde and the tilde over my x̃ is however different: in my case, it is part of the symbol x̃ and should be typed as such. That is my humble opinion. – MunHo Nov 11 '13 at 12:57

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 x̃ (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\Umathcodenumx }}
\AtBeginDocument{\mathcodex="8000 }
\begingroup\lccode~=x

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

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


• I am fully aware that there is no such codepoint. The reason I use unicode-math is to make input of math easier and more legible, therefore I do not find \tilde{x} a good solution, I would rather be able to type x̃. – MunHo Nov 11 '13 at 10:34
• @MunHo I added a possible way out of the dilemma; not something I would really try in full generality. – egreg Nov 11 '13 at 14:53
• Nice, it works just the way I like it! Is there any way to do this without having to manually define each and every letter and accent in the alphabet? Cannot \addaccent be defined to take an argument? Then only one such function is needed for all letters and all accents. Just for completeness: the code point for hat is U+0302, bar U+0304 and dot U+0307. – MunHo Nov 12 '13 at 14:26
• @MunHo Of course such constructions could be abstracted. I'm not going to do it. – egreg Nov 12 '13 at 14:32

\documentclass{scrartcl}

\usepackage{unicode-math}
\usepackage{amsmath}

\AtBeginDocument{\mathcodex="8000 }
\begingroup\lccode~=x

\makeatletter
\@ifnextchar ^^^^0303{\text{\emph{#1̃}}\@gobble}{%
\@ifnextchar ^^^^0302{\text{\emph{#1̂}}\@gobble}{%
\@ifnextchar ^^^^0304{\text{\emph{#1̄}}\@gobble}{%
\text{\emph{#1}}}}}%
}
\makeatother

\begin{document}
x̃ŷz̄ $x̃x̂x̄ \frac{1}{x̃}$
\end{document}
`

I redefined the function to take the letter as an argument (that will make it easier to extend the code to all letters) and added more accents.

To enter text mode is probably not the most correct way to do it, but I found no other solution.

• You're not using math letters in this way. – egreg Nov 14 '13 at 15:38