[Tex/LaTex] UTF-8 characters in math mode

math-modeunicodexetex

I'm new to this forum and to LaTeX. It's the first time I use use it and I really like it. I have a problem with it, though, I have looked for solution on this forum but I haven't found anything (or I don't know how to use other solutions for my case).

I'm using XeLateX with the editor Texpad for Mac OS X and I can't use UTF-8 characters in math mode. I've read here that I shouldn't use babel, nor inputenc, but polyglossia instead. I'm doing it and even though I have Spanish UTF-8 characters in normal mode, I don't have them on math mode. Let's see some code:

\documentclass{article}
\usepackage{polyglossia}
\setdefaultlanguage{spanish}
\begin{document}
Algo de texto y una fórmula $á+b=\int_{\xi}^{\theta} f(x)\,dx$.
\end{document}

Which outputs:

Code output

As you can see, "ó" is working outside math mode but "á" isn't working in it. Maybe I should use some package such as mathspec or unicode-math, but I don't really know how.

Help would be much appreciated, thank you in advance!

Best Answer

Accents in math mode have to be added in a different way than in text mode; in general, an accented letter in math has no connection with the corresponding meaning in text, so ü in math is the second derivative of u, not “u with diaeresis”.

You can, if you really want to use accented letters in math, add a meaning for them.

\documentclass{article}
\usepackage{xparse}
\usepackage{fontspec}
\usepackage{polyglossia}
\setdefaultlanguage{spanish}

\ExplSyntaxOn
\NewDocumentCommand{\DeclareMathUnicode}{mm}
 {
  \cs_new_protected:cpn { mathutf_#1: } { #2 }
  \char_set_active_eq:Nc #1 { mathutf_#1: }
  \char_set_mathcode:nn { `#1 } { "8000 }
 }
\cs_generate_variant:Nn \char_set_active_eq:NN { Nc }
\ExplSyntaxOff

\DeclareMathUnicode{á}{\acute{a}}
% other needed declarations

\begin{document}
Algo de texto y una fórmula $á+b=\int_{\xi}^{\theta} f(x)\,dx$.
\end{document}

enter image description here