6

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!

3
  • 2
    There is no Unicode "x with tilde"; combining characters do not work in math mode, because they come after the main character.
    – egreg
    Commented Nov 11, 2013 at 10:14
  • 6
    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. Commented Nov 11, 2013 at 11:30
  • In my case 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 is however different: in my case, it is part of the symbol and should be typed as such. That is my humble opinion.
    – MunHo
    Commented Nov 11, 2013 at 12:57

2 Answers 2

7

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

4
  • 3
    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 .
    – MunHo
    Commented Nov 11, 2013 at 10:34
  • @MunHo I added a possible way out of the dilemma; not something I would really try in full generality.
    – egreg
    Commented Nov 11, 2013 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
    Commented Nov 12, 2013 at 14:26
  • 1
    @MunHo Of course such constructions could be abstracted. I'm not going to do it.
    – egreg
    Commented Nov 12, 2013 at 14:32
1

Based on egreg's answer:

\documentclass{scrartcl}

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

\AtBeginDocument{\mathcode`x="8000 }
\begingroup\lccode`~=`x
\lowercase{\endgroup\def~}{\addaccent{x}}

\makeatletter
\newcommand\addaccent[1]{%
  \@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̃ŷ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.

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .