Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

spacing with \frac #969

Closed
Alex-Jordan opened this issue Dec 5, 2014 · 11 comments

Comments

Projects
None yet
3 participants
@Alex-Jordan
Copy link

commented Dec 5, 2014

$-\frac12$ appears to treat the minus sign like a subtraction sign, putting more space between the minus sign and the fraction than I would expect. $-{\frac12}$ treats the minus sign like a negative sign with less space, and it's how I bypass the issue in practice. Similar things happen if you want to make a mixed number: $1\frac12$ versus $1{\frac12}$.

I see these issues in HTML-CSS and SVG output modes, but not in MathML with Firefox 33.1. Maybe this is expected behavior, but I couldn't find any discussion of it anywhere so I thought I would open this issue.

@Alex-Jordan

This comment has been minimized.

Copy link
Author

commented Dec 5, 2014

Actually now that it occurred to me to check, the same thing happens with other elements like \sum, \prod, and \int too instead of \frac. Perhaps this is already discussed in an issue because I limited my searching to the issue with \frac.

@dpvc

This comment has been minimized.

Copy link
Member

commented Dec 6, 2014

It turns out that the extra spacing is due to treating fractions as having TeX class INNER, as the TeXBook indicates that they should be (page 158, and in Appendix 6 rule 15e). But it turns out that TeX doesn't seem to really do that, and, as far as I can tell, fractions are treated as class ORD. perhaps I am missing something in the TeXBook, but fractions in TeX don't get the spacing of an INNER atom. So I will have to change that in MathJax.

@dpvc dpvc added the Accepted label Dec 6, 2014

@dpvc dpvc added this to the MathJax 2.5 milestone Dec 6, 2014

dpvc pushed a commit to dpvc/MathJax that referenced this issue Dec 6, 2014

Davide P. Cervone
Make fractions be TeX class ORD rather than INNER (as appears to be t…
…he case with TeX, despite the claims in the TeXbook to the contrary). Also, fix a problem with getting the correct form attribute for mo elements. Resolves issue mathjax#969.
@dpvc

This comment has been minimized.

Copy link
Member

commented Dec 6, 2014

The issue969 branch of my fork of MathJax resolves this issue.

@Alex-Jordan

This comment has been minimized.

Copy link
Author

commented Dec 6, 2014

OK, and I realized that the other things I mentioned (like -\sum x_n and -{\sum x_n} also have different minus sign spacing) is I guess how it should be, since I now see that they are different with pdflatex too.

@dpvc

This comment has been minimized.

Copy link
Member

commented Dec 6, 2014

Yes, the spacing before an operator is different from before a fraction. The extra space between the minus sign and the sum is supposed to be there. In The TeXBook, page 170, there is a table that gives the spacing used between two items depending on their TeX class. A unary minus sign is type ORD, and \sum is type OP, so there should be a thinspace between them. When you put \sum in braces, the braced group has class ORD, and there is no extra space between two ORDs, which is why you get less space in -{\sum}. The same goes for \prod, \int, etc. I would not modify the TeX to change the spacing for these.

The TeXBook says that fractions are class INNER, which is how I had marked then in MathJax, and that would put extra space between the minus and fraction like with the operators; however, empirical testing suggests that the fraction is actually treated as type ORD, so I've changed that in the issue969 branch. This should resolve the spacing with fractions, but will not alter the (correct) spacing for operators like \sum.

dpvc pushed a commit to dpvc/MathJax that referenced this issue Dec 7, 2014

@dpvc

This comment has been minimized.

Copy link
Member

commented Dec 7, 2014

=> Merged.

@dpvc dpvc added Merged and removed Ready for Review labels Dec 7, 2014

@dpvc dpvc closed this Dec 7, 2014

@davidcarlisle

This comment has been minimized.

Copy link

commented Dec 11, 2014

On 6 December 2014 at 12:58, Davide P. Cervone notifications@github.com
wrote:

Yes, the spacing before an operator is different from before a fraction.
The extra space between the minus sign and the sum is supposed to be there.
In The TeXBook, page 170, there is a table that gives the spacing used
between two items depending on their TeX class. A unary minus sign is type
ORD, and \sum is type OP, so there should be a thinspace between them.
When you put \sum in braces, the braced group has class ORD, and there is
no extra space between two ORDs, which is why you get less space in
-{\sum}. The same goes for \prod, \int, etc. I would not modify the TeX
to change the spacing for these.

The TeXBook says that fractions are class INNER, which is how I had marked
then in MathJax, and that would put extra space between the minus and
fraction like with the operators; however, empirical testing suggests that
the fraction is actually treated as type ORD, so I've changed that in the
issue969 branch. This should resolve the spacing with fractions, but will
not alter the (correct) spacing for operators like \sum.

The space around fractions is actually governed by \nulldelimiterspace
(1.2pt by default)
although I don't think that length parameters are settable in mathjax you
can see the effect in tex clearly enough if you change this length

\documentclass{article}

\begin{document}

$\left. x\right|$

$\nulldelimiterspace=0pt \left. x\right|$

$\left. x\right|$

$1\frac{a}{b}2$

$\nulldelimiterspace=10pt 1\frac{a}{b}2$

\end{document}

@dpvc

This comment has been minimized.

Copy link
Member

commented Dec 11, 2014

David, thanks for the additional information.

MathJax does insert the space from \nulldelimiterspace (though, as you point out, it doesn't provide a way to set \nulldelimiterspace from within TeX, though you can do it from MathJax's configuration). This space is part of Rule 15e in Appendix G of The TeXbook, but it is in addition to the space inserted between elements based on their class. Rule 15e says to add those delimiters to the vbox constructed for the fraction, and "replace the generalized fraction by an Inner atom whose nucleus is the resulting sequence of three boxes (left delimiter, vbox, right delimiter)." That suggests that the resulting fraction would be treated as an Inner atom when computing the inter-element spacing added in rule 20.

If I use

\setbox0=\hbox{$\nulldelimiterspace=10pt 1\frac{a}{b}2$}
\tracingonline=1 \showboxdepth=10 \showboxbreadth=10
\showbox0

the result is

> \box0=
\hbox(6.9512+3.44841)x34.33768
.\mathon
.\OT1/cmr/m/n/10 1
.\hbox(6.9512+3.44841)x24.33765
..\hbox(6.9512+3.44841)x24.33765
...\hbox(0.0+0.0)x10.0, shifted -2.5
...\vbox(6.9512+3.44841)x4.33765
....\hbox(3.01389+0.0)x4.33765
.....\OML/cmm/m/it/7 a
....\kern1.23732
....\rule(0.39998+0.0)x*
....\kern0.88731
....\hbox(4.8611+0.0)x4.33765, glue set 0.41049fil
.....\glue 0.0 plus 1.0fil minus 1.0fil
.....\OML/cmm/m/it/7 b
.....\glue 0.0 plus 1.0fil minus 1.0fil
...\hbox(0.0+0.0)x10.0, shifted -2.5
.\OT1/cmr/m/n/10 2
.\mathoff

Here you can see the \nulldelimiterspace appears as the two \hbox(0.0+0.0)x10.0, shifted -2.5 that surround the \vbox containing the fraction. These are inside two addition \hbox and the 1 and 2 are outside those. There is no additional space between the 1 and the \hbox. In this case, the 1 is class Ord, and if the fraction is really class Inner, the table on page 170 of The TeXbook indicates that the spacing should be a thin space (1), but there is no additional space.

On the other hand, forcing the 1 to be an Op using \mathop{1}, we see that

\setbox0=\hbox{$\nulldelimiterspace=10pt \mathop{1}\frac{a}{b}2$}
\tracingonline=1 \showboxdepth=10 \showboxbreadth=10
\showbox0

produces

> \box0=
\hbox(6.9512+3.44841)x36.0043
.\mathon
.\hbox(6.44444+0.0)x5.00002, shifted 0.72223
..\OT1/cmr/m/n/10 1
.\glue(\thinmuskip) 1.66663
.\hbox(6.9512+3.44841)x24.33765
..\hbox(6.9512+3.44841)x24.33765
...\hbox(0.0+0.0)x10.0, shifted -2.5
...\vbox(6.9512+3.44841)x4.33765
....\hbox(3.01389+0.0)x4.33765
.....\OML/cmm/m/it/7 a
....\kern1.23732
....\rule(0.39998+0.0)x*
....\kern0.88731
....\hbox(4.8611+0.0)x4.33765, glue set 0.41049fil
.....\glue 0.0 plus 1.0fil minus 1.0fil
.....\OML/cmm/m/it/7 b
.....\glue 0.0 plus 1.0fil minus 1.0fil
...\hbox(0.0+0.0)x10.0, shifted -2.5
.\OT1/cmr/m/n/10 2
.\mathoff

Here you see the extra space as the \glue(\thinmuskip) 1.66663 following the 1 and preceding the fraction \hbox. Note that this is in addition to the \nulldelimiterspace, which is still inside the fraction's \hbox.

If you replace \mathop by \mathbin, \mathrel, etc, for all the difference classes, you get the a \medmuskip for \mathbin, \thickmuskip for \mathrel, no skip for \mathopen and \mathclose, and \thinmuskip for \mathpunct and \mathinner. These are the values that correspond to the second atom being an Ord, not an Inner, which is why I said that the fraction doesn't seem to be an Inner atom despite the statement in Rule 15e (and on page 158).

The only thing I can think of to explain the discrepancy is that the fraction does appear within two \hboxes, and so the inner one might be the box that is class Inner, and the outer one may be coming from a brace group or something that produces a box of class ord. I haven't looked into the definition of \frac to see how it is produced. I did try using 1{a\over b}2, which produces the same results, but of course, this has a braced group. I can't think of a way to produce the fraction without braces, so it may be that that is the source of the discrepancy.

In any case, MathJax was treating the fraction as Inner when it appears it should be treated as Ord, and that is what the patch listed above corrects. The \nulldelimiterspace is already being applied correctly, so I don't think there is any problem there.

@davidcarlisle

This comment has been minimized.

Copy link

commented Dec 11, 2014

but of course, this has a braced group. I can't think of a way to produce
the fraction without braces, so it may be that that is the source of the
discrepancy.

yes I think so (much of the texbook you can only interpret after debugging
the source:-)

$1{a\over b} 2\showlists$

produces

math mode entered at line 14

\mathord
.\fam0 1
\mathord
.\fraction, thickness = default
.\mathord
..\fam1 a
./\mathord
./.\fam1 b
\mathord
.\fam0 2

where I think \faction is the \mathinner variant but it gets wrapped by
\mathord from the mandatory
brace group.

so I don't think there is any problem there.

agreed, thanks.

David

@dpvc

This comment has been minimized.

Copy link
Member

commented Dec 11, 2014

OK, thanks for the additional information. I suspected the extra bracing was the culprit, but I wanted to avoid debugging the TeX code myself. :-)

I think we're good at this point.

dpvc pushed a commit to mathjax/MathJax-test that referenced this issue Jan 13, 2015

@dpvc dpvc added Test Available and removed Test Needed labels Jan 13, 2015

@dpvc

This comment has been minimized.

Copy link
Member

commented Jan 13, 2015

==> In Testsuite

LaTeXToMathML/frac/issue969.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.