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

Rewrite spacing commands and \mod as macros #1156

Merged
merged 16 commits into from
May 29, 2018

Conversation

edemaine
Copy link
Member

@edemaine edemaine commented Feb 13, 2018

Fix #1130 by defining \!, \negthinspace, \,, \thinspace, \:, \medspace, \;, \thickspace, \negmedspace, \negthickspace, \enspace, \enskip, \qquad, \quad as macros. Some of these are new. Also, we now use the AMS definitions of spaces, which are slightly different (e.g., using 1mu = 1/18em instead of .05555em, and working in text mode instead of just math mode).

Fix #1036 by defining a new SpaceNode in mathMLTree that recognizes all special space amounts from https://www.w3.org/TR/2000/WD-MathML2-20000328/chapter6.html

Fix KaTeX#1130 by defining `\!`, `\negthinspace`, `\,`, `\thinspace`, `\:`,
`\medspace`, `\;`, `\thickspace`, `\negmedspace`, `\negthickspace`,
`\enspace`, `\enskip`, `\qquad`, `\quad` as macros.

Fix KaTeX#1036 by defining a new `SpaceNode` in mathMLTree that recognizes
all special space amounts from
https://www.w3.org/TR/2000/WD-MathML2-20000328/chapter6.html
@codecov
Copy link

codecov bot commented Feb 13, 2018

Codecov Report

Merging #1156 into master will increase coverage by 0.72%.
The diff coverage is 80%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1156      +/-   ##
==========================================
+ Coverage   81.56%   82.29%   +0.72%     
==========================================
  Files          78       77       -1     
  Lines        4259     4242      -17     
  Branches      734      732       -2     
==========================================
+ Hits         3474     3491      +17     
+ Misses        670      649      -21     
+ Partials      115      102      -13
Impacted Files Coverage Δ
src/defineFunction.js 93.75% <ø> (ø) ⬆️
src/functions.js 100% <ø> (ø) ⬆️
src/symbols.js 100% <ø> (ø) ⬆️
src/functions/symbolsSpacing.js 64.7% <0%> (-35.3%) ⬇️
src/buildCommon.js 89.78% <100%> (ø) ⬆️
src/macros.js 87.9% <100%> (+0.97%) ⬆️
src/functions/kern.js 81.25% <100%> (+9.02%) ⬆️
src/functions/operatorname.js 42.42% <100%> (ø) ⬆️
src/mathMLTree.js 75.43% <78.78%> (+4.47%) ⬆️
src/SourceLocation.js 100% <0%> (+11.11%) ⬆️
... and 1 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 978148e...200ded1. Read the comment docs.

*/
toMarkup(): string {
if (this.character) {
return this.character;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a little confused about the correct markup here. Perhaps it should be `<mtext>${this.character}</mtext>`, and then we need to remove this <mtext> wrapper if it gets included in an <mo> or other token element?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure how to do this either. In the past I've look at what MathJax outputs, but <mspace width="thinmathspace" /> doesn't seem ideal. Is it bad if we include <mtext> nodes inside <mo> or other element types?

@@ -142,7 +141,7 @@ exports[`A MathML builder should output \\limsup_{x \\rightarrow \\infty} correc
<msub>
<mo>
<mi mathvariant="normal">
limsup
lim&amp;ThinSpace;sup
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This extra escaping is a bug, caused by operatorname's mathmlBuilder creating a mathMLTree.TextNode, which escapes its text. I'm not quite sure how to fix it, other than switching to the Unicode representations...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're definitely doing something wrong here. In toNode we call document.createTextNode which expects to be passed an escaped string, yet we're passing this.text. I feel like we should maybe be doing the escaping within the constructor and then we could add an escape as an optional second parameter with the default being true.

@kevinbarabash
Copy link
Member

@edemaine looks like some of the screenshots changed. Could you add updated screenshots when you have some time?

@edemaine
Copy link
Member Author

@kevinbarabash Done. This has revealed more bugs, which I'll take a look at later.

@kevinbarabash
Copy link
Member

@edemaine what kind of bugs did this reveal?

@edemaine
Copy link
Member Author

edemaine commented May 21, 2018

@kevinbarabash I meant bugs in this PR. I just merged with the many changes on master, and will take another look at this. CircleCI's screenshot diffs will help too! Here are the diffs, for the record:

arraymode-chrome-diff
dots-chrome-diff
negativespacebetweenrel-chrome-diff
limitcontrols-chrome-diff
fractiontest-chrome-diff

Specifically, Dots and NegativeSpaceBetweenRel seem brokenvery different, and I should check whether the ArrayMode tweaks are for better or worse.

@edemaine
Copy link
Member Author

I finally took a look at these. It turns out that the big changes are actually bug fixes! NegativeSpaceBetweenRel was particularly broken -- the negative space wasn't rendering! Not sure how we missed that (perhaps a side-effect of the line-breaking code?). But I'm glad this fixes it. And Dots was similarly missing a negative space (\!). Here are texcmp outputs against the new behavior, confirming they are correct:

negativespacebetweenrel
dots

ArrayMode has minor changes, and the texcmp (below) is so far to not be very revealing. But given that the other changes are substantial improvements, I'd say we should merge.

arraymode

@kevinbarabash
Copy link
Member

@edemaine there are some flow errors that need resolving before we can merge.

@edemaine
Copy link
Member Author

@kevinbarabash Oops, fixed!

@kevinbarabash
Copy link
Member

The Mod screenshots changed quite a bit. Also the CJK rows in Unicode-chrome.png have disappeared.

@edemaine
Copy link
Member Author

edemaine commented May 25, 2018

@kevinbarabash Thanks for spotting those -- not sure how I missed Mod in particular.

c6f3a7a recompiles the Unicode-chrome screenshot. This was caused some unfortunate nondeterminism in the screenshotter loading the supporting fonts, as evidenced by the screenshot tests actually passing with the wrong image! But now it's the correct image, phew.

The problem with the Mod test is that the various mod operators created manual elements of class mspace with various subclasses to do the spacing. It seemed quite a bit easier to start over completely, and reimplement the mod operators (amsmath versions) via macros. It was quite easy, and cleans up the code further. This is 2bb10f7 . And now Mod doesn't differ at all!

ModScript seems to have an issue though, which I'm still looking at...

@edemaine
Copy link
Member Author

edemaine commented May 25, 2018

Fixed ModScript; I just had to remember what \nonscript meant again. 😄 So glad we have \mathchoice now! But I might try to implement \nonscript in a future PR.

texcmp for a slightly tweaked (single column, left justified) ModScript test:
modscript

For comparison, here's what the texcmp for master branch looks like:
modscript 1

Our spaces seem smaller than LaTeX's in both cases, but the changes from this PR seem like a step in the right direction. (And the script fonts are so different in LaTeX vs. KaTeX that it's difficult to know the precise cause. Maybe even ems are wrong in our script font...)

Copy link
Member

@kevinbarabash kevinbarabash left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks for also improving the MathML output to include all of those specific spacing characters.

@@ -24,7 +24,6 @@ import "./functions/lap";
import "./functions/math";
import "./functions/mathchoice";
import "./functions/mclass";
import "./functions/mod";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome!

if (width >= 0.05555 && width <= 0.05556) {
this.character = "&VeryThinSpace;"; // \u200a
} else if (width >= 0.1666 && width <= 0.1667) {
this.character = "&ThinSpace;"; // \u2009
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool!

// See https://www.w3.org/TR/2000/WD-MathML2-20000328/chapter6.html
// for a table of space-like characters. We consistently use the
// &LongNames; because Unicode does not have single characters for
// &ThickSpace; (\u2005\u200a) and all negative spaces.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't don't understand this comment. I don't see $LongNames; anywhere.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant &ThickSpace; is an example of a long name between a & and ;, as opposed to the Unicode equivalent. See the table in Section 6.1.4. I could change to &CharacterNames; or just "character names" if that would be clearer.

@edemaine
Copy link
Member Author

@kevinbarabash Thanks for the review!

@edemaine edemaine changed the title Rewrite spacing commands as macros Rewrite spacing commands and \mod as macros May 29, 2018
@ylemkimon ylemkimon mentioned this pull request Aug 18, 2018
1 task
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants