latex3 / mathtools

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

Package lualatex-math Warning: I've expected \MT_cramped_internal:Nn to have the meaning... #24

Closed dbitouze closed 3 years ago

dbitouze commented 3 years ago

FWIW, since the 1.25 version of mathtools, a new warning appears when it is used in conjunction with lualatex-math:

Package lualatex-math Warning: I've expected \MT_cramped_internal:Nn to have
(lualatex-math)                the meaning
(lualatex-math)                macro:#1#2->\sbox \z@ {$\m@th
(lualatex-math)                #1\nulldelimiterspace =\z@ \radical \z@
(lualatex-math)                {#2}$}\ifx #1\displaystyle \dimen@ =\fontdimen
(lualatex-math)                8\textfont 3\advance \dimen@ .25\fontdimen
(lualatex-math)                5\textfont 2\else \dimen@ =1.25\fontdimen 8\ifx
(lualatex-math)                #1\textstyle \textfont \else \ifx
(lualatex-math)                #1\scriptstyle \scriptfont \else
(lualatex-math)                \scriptscriptfont \fi \fi 3\fi \advance \dimen@
(lualatex-math)                -\ht \z@ \ht \z@ =-\dimen@ \box \z@ ,
(lualatex-math)                but it has the meaning
(lualatex-math)                macro:#1#2->\setbox \z@ \hbox {$\m@th #1\kern
(lualatex-math)                -\nulldelimiterspace \radical \z@ {#2}$}\ifx
(lualatex-math)                #1\displaystyle \dimen@ =\fontdimen 8\textfont
(lualatex-math)                3\advance \dimen@ .25\fontdimen 5\textfont
(lualatex-math)                2\else \dimen@ =1.25\fontdimen 8\ifx
(lualatex-math)                #1\textstyle \textfont \else \ifx
(lualatex-math)                #1\scriptstyle \scriptfont \else
(lualatex-math)                \scriptscriptfont \fi \fi 3\fi \advance \dimen@
(lualatex-math)                -\ht \z@ \ht \z@ =-\dimen@ \ifvmode \leavevmode
(lualatex-math)                \fi {}\box \z@ .

Here is a MCE:

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

and the file list:

 *File List*
 article.cls    2020/04/10 v1.4m Standard LaTeX document class
  size10.clo    2020/04/10 v1.4m Standard LaTeX file (size option)
unicode-math.sty    2020/01/31 v0.8q Unicode maths in XeLaTeX and LuaLaTeX
   expl3.sty    2021-02-18 L3 programming layer (loader) 
l3backend-luatex.def    2021-03-18 L3 backend support: PDF output (LuaTeX)
unicode-math-luatex.sty    2020/01/31 v0.8q Unicode maths in XeLaTeX and LuaLaT
eX
  xparse.sty    2021-01-09 L3 Experimental document command parser
xparse-2020-10-01.sty    
l3keys2e.sty    2021-03-12 LaTeX2e option processing using LaTeX3 keys
fontspec.sty    2020/02/21 v2.7i Font selection for XeLaTeX and LuaLaTeX
fontspec-luatex.sty    2020/02/21 v2.7i Font selection for XeLaTeX and LuaLaTeX

 fontenc.sty
fontspec.cfg
  fix-cm.sty    2015/01/14 v1.1t fixes to LaTeX
  ts1enc.def    2001/06/05 v3.0e (jk/car/fm) Standard LaTeX file
 amsmath.sty    2020/09/23 v2.17i AMS math features
 amstext.sty    2000/06/29 v2.01 AMS text
  amsgen.sty    1999/11/30 v2.0 generic functions
  amsbsy.sty    1999/11/29 v1.2d Bold Symbols
  amsopn.sty    2016/03/08 v2.02 operator names
lualatex-math.sty    2020/09/25 v1.9 Patches for mathematics typesetting with L
uaLaTeX
etoolbox.sty    2020/10/05 v2.5k e-TeX tools for LaTeX (JAW)
mathtools.sty    2021/03/18 v1.25 mathematical typesetting tools
  keyval.sty    2014/10/28 v1.15 key=value parser (DPC)
    calc.sty    2017/05/25 v4.3 Infix arithmetic (KKT,FJ)
 mhsetup.sty    2021/03/18 v1.4 programming setup (MH)
graphicx.sty    2020/09/09 v1.2b Enhanced LaTeX Graphics (DPC,SPQR)
graphics.sty    2020/08/30 v1.4c Standard LaTeX Graphics (DPC,SPQR)
    trig.sty    2016/01/03 v1.10 sin cos tan (DPC)
graphics.cfg    2016/06/04 v1.11 sample graphics configuration
  luatex.def    2020/10/05 v1.2a Graphics/color driver for luatex
supp-pdf.mkii
epstopdf-base.sty    2020-01-24 v2.11 Base part for package epstopdf
epstopdf-sys.cfg    2010/07/13 v1.3 Configuration of (r)epstopdf for TeX Live
 ***********

BTW: I will draw the attention of the author of lualatex-math.

daleif commented 3 years ago

\MT_cramped_internal:Nn was indeed updated in the latest release. IMO this should be a bug in lualatex-math .

See issue #18 and issue #19

u-fischer commented 3 years ago

@daleif I think we discussed once in the chat if mathtools should take over the lualatex-math patches.

daleif commented 3 years ago

@u-fischer I don't think I've heard any talk about mathtools and lualatex. That would also mean porting mathtools to real expl3 (it is not really written in expl3) Had a look at that patch and I don't even understand what it does and why.

u-fischer commented 3 years ago

@daleif we had a discussion around here: https://chat.stackexchange.com/transcript/message/55641430#55641430 (it is only for the case that unicode-math is used, not for traditional fonts).

daleif commented 3 years ago

Ahh, that one, forgot about that one.

Why is it \ensuremath is needed?

I never use unicode-math and never test against it

u-fischer commented 3 years ago

@daleif I don't know why ensuremath is used. I even don't know if the patch is good or not, but imho it is either good then it should move into mathtools, or it is bad then it should be removed from lualatex-math. Having many years such patches in external packages is not good.

daleif commented 3 years ago

The changes to crampedsubarray also gives problems. I have no idea what lualatex needs or does not need.

daleif commented 3 years ago

Ahh, right, lualatex has its own cramped primitives, and we cannot expect them to give the same cramped-ness we get in pdflatex.

Hmm, so why is

\ensuremath {
    \use:c { cramped \cs_to_str:N #1 } #2
     }

no longer enough in lualatex-math.sty. I keep getting an error about a missing { when I get with the example from https://github.com/latex3/mathtools/issues/17, presumably because we made this change

  %%$\m@th\scriptstyle\kern-\nulldelimiterspace\radical\z@{##}$% <-- changed line
  \span\MT_cramped_internal:Nn \scriptstyle {##}%

in the definition of crampedsubstack

zauguin commented 3 years ago

@daleif I can't reproduce the "missing {" with the code from issue 17, but if you send me a small example I'll have a look what's going on.

daleif commented 3 years ago

@zauguin Here is what I did

MWE

\documentclass{article}
\usepackage{mathtools}
\usepackage{unicode-math}
\usepackage{graphicx} % for \scalebox
\newcommand\drawbaseline{%
  \leavevmode
  \hbox to 0pt {\hss\vrule width 50pt height 0.1pt depth 0pt }%
}

\begin{document}
\centering
\scalebox{6}%
  {$\displaystyle\sum_{k^2=n}\quad
    \sum_{\crampedsubstack{k^2=n\drawbaseline}}$}
\end{document}

then I also changed a local copy of lualatex-math.sty to have

\__lltxmath_after_package_or_now:nn { mathtools } {
  \__lltxmath_patch:NNnnn \MT_cramped_internal:Nn
    \cs_set_nopar:Npn { #1 #2 } {
      \setbox\z@\hbox{$\m@th#1\kern-\nulldelimiterspace\radical\z@{#2}$}
      \ifx#1\displaystyle
      \dimen@=\fontdimen8\textfont3
      \advance\dimen@ .25\fontdimen5\textfont2
      \else
      \dimen@=1.25\fontdimen8
      \ifx#1\textstyle\textfont
      \else
      \ifx#1\scriptstyle
      \scriptfont
        \else
        \scriptscriptfont
        \fi
        \fi
        3
        \fi
        \advance\dimen@-\ht\z@ \ht\z@=-\dimen@
        \ifvmode\leavevmode\fi
        {}\box\z@
      } {
        {
          \ensuremath {
            \use:c { cramped \cs_to_str:N #1 } #2
          }
        }
      }
    }

basically updating the code it is trying to patch.

This now gives me

./bug.tex:16: Missing { inserted.
<inserted text> 
{
l.16 ...sum_{\crampedsubstack{k^2=n\drawbaseline}}$}

I should have explained more carefully.

I'm guessing this is now related to the following change to crampedsubarray (used by \crampedsubstack)

\newenvironment{crampedsubarray}[1]{%
  \vcenter\bgroup
  \Let@ \restore@math@cr \default@tag
  \baselineskip\fontdimen10 \scriptfont\tw@
  \advance\baselineskip\fontdimen12 \scriptfont\tw@
  \lineskip\thr@@\fontdimen8 \scriptfont\thr@@
  \lineskiplimit\lineskip
  \ialign\bgroup\ifx c#1\hfil\fi
  %%$\m@th\scriptstyle\kern-\nulldelimiterspace\radical\z@{##}$% <-- changed line
  \span\MT_cramped_internal:Nn \scriptstyle {##}%
  \hfil\crcr%
}{%
\crcr\egroup\egroup
}

the \span part is new

daleif commented 3 years ago

Seems for lualatex we then need crampedsubarray to be using

\renewenvironment{crampedsubarray}[1]{%
  \vcenter\bgroup
  \Let@ \restore@math@cr \default@tag
  \baselineskip\fontdimen10 \scriptfont\tw@
  \advance\baselineskip\fontdimen12 \scriptfont\tw@
  \lineskip\thr@@\fontdimen8 \scriptfont\thr@@
  \lineskiplimit\lineskip
  \ialign\bgroup\ifx c#1\hfil\fi
  \span$\MT_cramped_internal:Nn \scriptstyle {##}$% <---- I added the $'s
  \hfil\crcr%
}{%
\crcr\egroup\egroup
}

though I don't quite understand why? since under lualatex \MT_cramped_internal:Nn is now using \ensuremath + the lualatex crammped macros (@u-fischer it might be therefore \ensuremath was used.

Anyone understand why this extra $...$ is needed here?

I agree with @u-fischer, we ought to check for lualatex in mathtools and perform the change there.

daleif commented 3 years ago

Looking at amsmath, one should of course use that updated code for crampedsubarray instead

zauguin commented 3 years ago

Anyone understand why this extra $...$ is needed here?

The issue is that in \MT_cramped_internal:Nn, the # ends up in the argument of \ensuremath, which doesn't get expanded until after the cell is completed, therefore the end of the cell can't be identified correctly. So the \endcrampedsubarray ends up inside of the \ensuremath, leading to mismatched braces and math delimiters. Adding $...$ fixes this since it turns \ensuremath basically in a noop, so it's placement is less important. I'm not sure though why this didn't show up earlier without the \span.

daleif commented 3 years ago

@zauguin that makes sense, I was wondering in that direction. I think it \ensuremath was uses like that because the original \cramped actually works outside math mode. Accident?

It seems to be better to just adjust the version of subarray from amsmath using \crampedscriptstyle directly when lualatex is used: No need for MT_cramped_internal:Nn

\renewenvironment{crampedsubarray}[1]{%
  \vcenter\bgroup
  \Let@ \restore@math@cr \default@tag
  \baselineskip \Umathstacknumup \scriptstyle
  \advance\baselineskip \Umathstackdenomdown \scriptstyle
  \lineskip \Umathstackvgap \scriptstyle
  \lineskiplimit \lineskip
  \ialign\bgroup\ifx c#1\hfil\fi
  \Ustartmath
  % \m@th\scriptstyle##
  \crampedscriptstyle{##}
  \Ustopmath
  \hfil\crcr
}{%
  \crcr\egroup\egroup
}
daleif commented 3 years ago

@zauguin any idea why in lualatex-math.sty we have

    {
      \ensuremath {
        \use:c { cramped \cs_to_str:N #1 } #2
      }
    }

Why the extra grouping? Might be usage related

daleif commented 3 years ago

BTW: the grouping is explained in lualatex-math:

Here the additional set of braces is absolutely necessary, otherwise the changed mathematical style would be applied to the material after the \mathchoice construct. As the original command works in both text and math mode, we use \ensuremath here.

daleif commented 3 years ago

I think this solves this from the mathtools side. I'll write the maintainer of lualatex-math to note that their patch for lualatex is now in mathtools itself.

phst commented 3 years ago

I've fixed this in lualatex-math for mathtools 1.26 (https://github.com/phst/lualatex-math/commit/ad51bb060107da4e5cc82e16e19243049fe97a58). Since this is now fixed in mathtools, I'm planning to remove the patch eventually from lualatex-math. For now I haven't added a patch for mathtools 1.25; not sure how important that would be if 1.26 makes it into TeX Live 2021.