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

Fix \mathit font and italic correction and add \mathnormal #1700

Merged
merged 6 commits into from
Oct 10, 2018
Merged

Fix \mathit font and italic correction and add \mathnormal #1700

merged 6 commits into from
Oct 10, 2018

Conversation

ylemkimon
Copy link
Member

@ylemkimon ylemkimon commented Sep 5, 2018

Fixes #584 and fixes #1250.

KaTeX uses the same font (Math-Italic, Computer Modern Math Italic[cmmi], class mathit for alphabets and Main-Regular, Computer Modern Roman[cmr] for digits) for math atoms outside font commands (default) and in \mathit.

However, \mathit in LaTeX uses text italic font (Main-Italic, Computer Modern Text Italic[cmti], class mainit) for both alphabets and digits, which has narrow widths (spacing) and no italic correction, same as \textit. This PR changes \mathit font to Main-Italic and remove its italic correction. (0d52009) This also fixes old font command \it, as it uses mathit class.

\DeclareMathAlphabet{\mathit}{OT1}{cmr}{m}{it}

mathit

(Green is LaTeX, red is KaTeX, aligned manually. The spacing around accent character seems to be still wrong but is in the right direction, I'll open an issue after this is merged. Wrong accent position is tracked by #1099, incorrect spacing around dotless i by #354, and \KaTeX mismatch by #1703)

Also, I've renamed the name mathit to mathdefault and mainit to mathit to avoid confusion. (f354da8) We should consider changing the font name.

Furthermore, I've added \mathnormal, which is similar to default math font but uses old style (Caligraphic) font for digits and math italic (Math-Italic) font for uppercase Greek alphabets as well as other letters that is in math italic font. (702c454)

mathnormal

(Green is LaTeX, red is KaTeX, incorrect spacing around dotless i is tracked by #354 and \KaTeX mismatch by #1703)

@ylemkimon ylemkimon requested a review from edemaine September 5, 2018 09:04
@ylemkimon ylemkimon changed the title Fix \mathit spacing (italic correction) [WIP] Fix \mathit spacing (italic correction) Sep 5, 2018
@ylemkimon ylemkimon removed the request for review from edemaine September 5, 2018 14:28
@ylemkimon ylemkimon closed this Sep 5, 2018
@ylemkimon ylemkimon reopened this Sep 5, 2018
@codecov
Copy link

codecov bot commented Sep 5, 2018

Codecov Report

Merging #1700 into master will increase coverage by <.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1700      +/-   ##
==========================================
+ Coverage   93.88%   93.89%   +<.01%     
==========================================
  Files          78       78              
  Lines        4564     4569       +5     
  Branches      802      805       +3     
==========================================
+ Hits         4285     4290       +5     
  Misses        246      246              
  Partials       33       33
Flag Coverage Δ
#screenshotter 88.76% <100%> (+0.01%) ⬆️
#test 85.24% <56.25%> (-0.08%) ⬇️
Impacted Files Coverage Δ
src/wide-character.js 72.22% <ø> (ø) ⬆️
src/functions/font.js 95.83% <ø> (ø) ⬆️
src/buildCommon.js 98.44% <100%> (+0.03%) ⬆️
src/buildMathML.js 98.73% <100%> (ø) ⬆️

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 68fdb52...a9fdd2a. Read the comment docs.

@ylemkimon ylemkimon requested a review from edemaine September 5, 2018 15:50
@ylemkimon ylemkimon changed the title [WIP] Fix \mathit spacing (italic correction) Fix \mathit spacing and italic correction Sep 6, 2018
@ylemkimon ylemkimon changed the title Fix \mathit spacing and italic correction Fix \mathit font and italic correction Sep 6, 2018
@ylemkimon ylemkimon changed the title Fix \mathit font and italic correction Fix \mathit font and italic correction and add \mathnormal Sep 6, 2018
@@ -85,7 +85,7 @@ export const getVariant = function(
}

const font = options.font;
if (!font) {
if (!font || font === "mathnormal") {
Copy link
Member Author

@ylemkimon ylemkimon Sep 6, 2018

Choose a reason for hiding this comment

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

I'm not sure what variant \mathnormal should use, but as it acts similar to the default math font, I used null (none).

Copy link
Member

Choose a reason for hiding this comment

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

That should be fine.

fontName = fontData.fontName;
fontClasses = [fontData.fontClass];
} else if (utils.contains(mathitLetters, text)) {
fontName = "Main-Italic";
fontClasses = ["mathit"];
Copy link
Member

Choose a reason for hiding this comment

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

This feels redundant. Shouldn't the mathdefault call below take care of this case?

@@ -72,7 +72,7 @@ const makeSymbol = function(
let symbolNode;
if (metrics) {
let italic = metrics.italic;
if (mode === "text") {
if (mode === "text" || (options && options.font === "mathit")) {
italic = 0;
Copy link
Member

Choose a reason for hiding this comment

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

Interesting. The combining of text glyphs we do should have a similar affect, shouldn't it. I guess it still keeps the italic value from the last glyph so it still changing the spacing at the boundaries between font types whereas this won't.

Copy link
Member Author

Choose a reason for hiding this comment

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

@kevinbarabash Should we remove mode === "text"?

Copy link
Member

Choose a reason for hiding this comment

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

Would this change affect the screenshots? I'm not exactly sure where we use .italic, as we don't support \/ (but maybe we should). We probably use it in accent placement, so removing mode === "text" might improve (or worsen) accents.

Copy link
Member

Choose a reason for hiding this comment

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

Not sure, but it's something we could investigate.

fontClass: "mathit",
};
} else if (/[0-9]/.test(value.charAt(0))) {
Copy link
Member

Choose a reason for hiding this comment

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

It'd be nice to have some helper functions for some of these tests, maybe isLatinLetter, isNumber, etc.

Copy link
Member

Choose a reason for hiding this comment

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

Even better (faster) would be to use hasOwnProperty on an object with the appropriate keys. We should replace all/most uses of utils.contains with this. But not necessarily for this PR.

Copy link
Member

@kevinbarabash kevinbarabash Oct 13, 2018

Choose a reason for hiding this comment

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

That's clever. We'd want to construct the object programmatically at load time though.

@@ -705,11 +734,17 @@ const fontMap: {[string]: {| variant: FontVariant, fontName: string |}} = {
variant: "italic",
fontName: "Main-Italic",
},
"mathit": {
variant: "italic",
fontName: "Main-Italic",
Copy link
Member

Choose a reason for hiding this comment

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

What were you thinking in terms of renaming fonts?

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 the PR.

|$\text{Ab0}$ `\text{Ab0}` |$\bm{Ab}$ `\bm{Ab}` |$\frak{Ab0}$ `\frak{Ab0}`
|$\mathsf{Ab0}$ `\mathsf{Ab0}` |$\mathtt{Ab0}$ `\mathtt{Ab0}` |$\mathfrak{Ab0}$ `\mathfrak{Ab0}`
|$\textsf{Ab0}$ `\textsf{Ab0}` |$\texttt{Ab0}$ `\texttt{Ab0}` |$\mathcal{AB0}$ `\mathcal{AB0}`
|$\sf Ab0$ `\sf Ab0` |$\tt Ab0$ `\tt Ab0` |$\mathscr{AB}$ `\mathscr{AB}`
Copy link
Member

Choose a reason for hiding this comment

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

Any guesses why I'm not seeing these changes on https://deploy-preview-1700--katex.netlify.com/docs/supported.html ? Is there something wrong with our Netlify builds?

Copy link
Member Author

Choose a reason for hiding this comment

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

@edemaine It's because versioned docs are not updated. You can check them in https://deploy-preview-1700--katex.netlify.com/docs/next/supported.html.

Copy link
Member

Choose a reason for hiding this comment

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

Of course! Thanks.

Now I see a bug, though: the 0 in \mathnormal his rendering in the incorrect font.

image

According to inspect, it's rendering with the HTML <span class="mord mathcal">0</span> whereas the letters are rendering as e.g. <span class="mord mathdefault">A</span>.

Copy link
Member

@edemaine edemaine Oct 2, 2018

Choose a reason for hiding this comment

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

Oops, I guess that's exactly what LaTeX does. Sorry, \mathnormal does not do what I thought it did!

\mathnormal{Ab0}\mathit{Ab0}

image

I do get the feeling that there's too much space after the A in all other KaTeX examples (\mathit, \mathrm, \textrm, \mathbf, ...) though...

Copy link
Member

Choose a reason for hiding this comment

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

Comparing more with LaTeX, everything seems like a good match. Which your texcmp outputs confirm. 😄

I do remain confused about what the initial/default mode of LaTeX is. I thought it was \mathnormal, but that differs on numerals. Is there a name for it?

Copy link
Member

Choose a reason for hiding this comment

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

\mathdefault? I don't even though if that's a thing or not, but there must be some way of getting back to that mode from inside of \mathnormal.

@ylemkimon
Copy link
Member Author

I'll merge this PR, as I'm planning to completely refactor font generation and selection.

@ylemkimon ylemkimon merged commit ba8e224 into KaTeX:master Oct 10, 2018
@kevinbarabash
Copy link
Member

@ylemkimon what kinds of changes are you planning?

maxlazio pushed a commit to gitlabhq/gitlabhq that referenced this pull request Jan 21, 2019
tigefa4u pushed a commit to tigefa4u/gitlabhq that referenced this pull request Jan 25, 2019
tigefa4u pushed a commit to tigefa4u/gitlabhq that referenced this pull request Jan 30, 2019
@ylemkimon ylemkimon deleted the mathit branch March 21, 2019 14:28
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.

\mathnormal vs. \mathit \mathit uses the wrong font
3 participants