# Xelatex and LuaLatex incorrectly rendering formula

The following formula is incorrectly rendered in XeLatex and LuaLatex.

\documentclass[]{article}
\usepackage{fontspec}
\begin{document}

$${\mathbf{\dot y}},\mathbf{\tilde y}$$

\end{document}


Any solutions? In addition to fontspec, I have many packages.

PS: fontspec is not supported by pdfLatex

• Works fine with all three engines here (TeX Live 2018), so might be something in your document you're not showing us that causes this. Can you make a complete, minimal example? Unrelated: Don't use $$...$$ in LaTeX, cf. Why is $…$ preferable to ?. – Torbjørn T. May 20 '18 at 6:10
• @TorbjørnT. Added. Problem is created by fontspec – pkj May 20 '18 at 6:54
• Load lmodern package. – Johannes_B May 20 '18 at 6:57
• The title claims a problem with xelatex, yet you add luatex to the tags. Why? If this cannot be reproduced with luatex, remove the tag, or fix the title. – Johannes_B May 20 '18 at 7:00
• @Johannes_B provide as an answer. LuaLatex also produces incorrect formula – pkj May 20 '18 at 7:04

When you load fontspec it tries to adjust the math alphabets (\mathbf etc) to the main document font. E.g. if you set Arial as main document:

\documentclass[]{article}
\usepackage{fontspec}
\setmainfont{Arial}

\begin{document}

$\mathit{x} = \mathbf{y}$

\end{document}


you get

Sadly this has the side effect to break accents inside the arguments as the new math alphabets use a different encoding than the standard math operator font and \dot is defined so that inside \mathbf it tries to use a glyph from \mathbf which isn't there.

Using the no-math option solves the problem with the dot accent but also doesn't adapt the math alphabets:

\documentclass[]{article}
\usepackage[no-math]{fontspec}
\setmainfont{Arial}

\begin{document}

$\mathbf{\dot y}, \dot{\mathbf{y}}, \mathit{x} = \mathbf{y}$

\end{document}


If you want to retain the math alphabets you could redefine the \dot command (imho using \mathalph is wrong anyway):

\documentclass[]{article}
\usepackage{fontspec}
\setmainfont{Arial}

\AtBeginDocument{\DeclareMathAccent{\dot}     {\mathord}{legacymaths}{95}}
\begin{document}

$\dot{y}, \mathbf{\dot y}, \dot{\mathbf{y}}$

\end{document}


This gives the correct dot but it is not bold inside \mathbf (which isn't a good syntax anyway, so you don't gain much compared to the correct input):

To adjust this you could add a definition for a bold dot:

\documentclass[]{article}
\usepackage{fontspec}
\setmainfont{Arial}

\AtBeginDocument{\DeclareMathAccent{\dot}     {\mathord}{legacymaths}{95}}
\DeclareSymbolFont{boldlegacymaths}{OT1}{cmr}{bx}{n}
\DeclareMathAccent{\bolddot}     {\mathord}{boldlegacymaths}{95}
\begin{document}

$\mathbf{\dot y}, \dot{\mathbf{y}}, \bolddot{\mathbf{y}}$

\end{document}


With unicode-math you should really better avoid to use accent commands inside math alphabets and resort to some switch to text mode if you want the bold dot:

\documentclass[]{article}
\usepackage{amsmath}
\usepackage{unicode-math}
\setmainfont{Arial}

\begin{document}

$\dot{\mathbf{y}}, \mathbf{\dot{y}}, \text{\normalfont\textbf{y^^^^0307}}$
\end{document}


In your example, removing \usepackage{fontspec} is one way to get the correct result. But I suppose you need to keep that package. You have several options:

• add the option [no-math] to the fontspec package
• as Johannes_B suggested, use the lmodern package
• use the package unicode-math, and set a main math font that defines the \dot and \tilde accents. Use the \setmathfont directive to set the math font. If you don't set a math font explicitly (via \setmathfont), Latin Modern Math will be used. This particular math font does define \dot and \tilde accents.

The first two suggestions work correctly with both XeLaTeX and LuaLaTeX. The third suggestion works correctly with LuaLaTeX, but with XeLaTeX it gives a wrong placement of the \dot and \tilde accents relative to y, at least with the font I tried (Latin Modern Math). However, the XeLaTeX-specific accent placement problem goes away automatically if one writes \dot{y} and \tilde{y} instead of \dot y and \tilde y. Encasing the argument of \dot and \tilde -- and of other accent-producing directives, such as \hat -- is recommended syntax anyway.

There may be other ways to get the proper accents.

As to the why of the error: In your example, "fontspec" is redeclaring a lot of math accents and symbols, the [no-math] option prevents this, and the other suggestions load fonts that provide the redeclared characters.

• +1. I've taken the liberty of editing the third bullet to elaborate some more on the \dot and \tilde placement issue. Feel free to revert. – Mico May 20 '18 at 9:20
• @Mico No problem at all, In fact, I learned something about the internals :) Thanks – remco May 20 '18 at 10:11