latex3 / mathtools

Mathematical tools to use with amsmath
LaTeX Project Public License v1.3c
54 stars 6 forks source link

xelatex + unicode-math breaks over/underbrackets #29

Open andrewheiss opened 2 years ago

andrewheiss commented 2 years ago

When using mathools v1.25 (2021/03/18) with xelatex (TeX Live 2021) and unicode-math, underbrackets and overbrackets fail to render correctly if thickness/height arguments are provided:

\documentclass{article}
\usepackage{mathtools}
\usepackage{unicode-math}
\begin{document}

Default options:

$$
\underbracket{A}_{\text{Some text}} = \overbracket{\pi r^2}^{\text{More text}}
$$

Custom options:

$$
\underbracket[0.25pt][1pt]{A}_{\text{Some text}} = \overbracket[1pt][3pt]{\pi r^2}^{\text{More text}}
$$

\end{document}
image

Everything works fine if unicode-math is omitted:

\documentclass{article}
\usepackage{mathtools}
\begin{document}

Default options:

$$
\underbracket{A}_{\text{Some text}} = \overbracket{\pi r^2}^{\text{More text}}
$$

Custom options:

$$
\underbracket[0.25pt][1pt]{A}_{\text{Some text}} = \overbracket[1pt][3pt]{\pi r^2}^{\text{More text}}
$$

\end{document}
image

It seems like unicode-math might be providing its own underbracket and overbracket that are overriding mathtools' brackets?

zauguin commented 2 years ago

It seems like unicode-math might be providing its own underbracket and overbracket that are overriding mathtools' brackets?

It's not unicode-math itself, but the font. Unicode math fonts contain instructions for drawing e.g. underbrackets/overbrackets and many similar things, so they don't have to be emulated with TeX code. unicode-math uses these. I would assume that this is by design.

andrewheiss commented 2 years ago

According to this, there might be a way to redefine over/underbracket macros so there's compatibility (since unicode-math does have its own argument-free over/underbracket) https://github.com/wspr/unicode-math/issues/544 - and once upon a time it seems to have worked by preserving and redefining mathtools' brackets: https://github.com/wspr/unicode-math/commit/872775eed44857706c0041bae798a04d8971125a

andrewheiss commented 2 years ago

In the meantime, here's a solution that works (via this)! Store mathtools' brackets as separate commands, then restore them after \begin{document}. They have to be restored after the document begins because unicode-math apparently executes its Unicode table after the document starts—including \let\underbracket=\normalunderbracket in the preamble thus doesn't work

\documentclass{article}
\usepackage{mathtools}
% Save mathtools' brackets
\let\normalunderbracket=\underbracket
\let\normaloverbracket=\overbracket

\usepackage{unicode-math}

\begin{document}

% Restore mathtools' brackets *after* \begin{document}
\let\underbracket=\normalunderbracket
\let\overbracket=\normaloverbracket

Default options:

$$
\underbracket[0.25pt][1pt]{A}_{\text{Some text}} = \overbracket[1pt][3pt]{\pi r^2}^{\text{More text}}
$$

Custom options:

$$
\underbracket[0.25pt][10pt]{A}_{\text{Some text}} = \overbracket[1pt][8pt]{\pi r^2}^{\text{More text}}
$$

\end{document}
image
zepinglee commented 2 years ago

mathtools also breaks over/underbrace of unicode-math (see https://tex.stackexchange.com/q/521394/82731). I suggest checking if unicode-math is loaded.

\@ifpackageloaded{unicode-math}{
  \let\underbrace\LaTeXunderbrace
  \let\overbrace\LaTeXoverbrace
}{}
daleif commented 2 years ago

@zepinglee doesn't unicode-math fix that by it self? The problem with the linked example is that mathtools is loaded after unicode-math which is not recommended.