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

feat(array): add support for repetition in format description #3583

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

aaruni96
Copy link

It is now possible to describe an array environment like
\begin{array}{* {35}{l}}
This completes task number 3 on
#269

What is the previous behavior before this PR?
When encountering an array environment described with the repetition format ( \begin{array}{* {7}{cl} ), KaTeX would throw an error and not render the equation.

What is the new behavior after this PR?
Equation now renders as expected.

Contribtues to #269

@aaruni96
Copy link
Author

Can someone explain why the test for flow is breaking ?

The logs seem to say that loc doesn't have start in it :

Cannot get `colalign[1].loc.start` because property `start` is missing in null or undefined [1]. [incompatible-use]

   src/environments/array.js:786:46
   786|             const numStart = colalign[1].loc.start + 1;
                                                     ^^^^^

References:
   src/parseNode.js:33:15
    33|         loc?: ?SourceLocation,

It seems to ignore that source location has other properties :

export default class SourceLocation {
    // The + prefix indicates that these fields aren't writeable
    +lexer LexerInterface; // Lexer holding the input string.
    +start: number;           // Start offset, zero-based inclusive.
    +end: number;            // Emd offset, zero based exclusive.

@edemaine edemaine changed the title fix (array): add support for repetition in format description feat(array): add support for repetition in format description Mar 14, 2022
@edemaine
Copy link
Member

Can someone explain why the test for flow is breaking ?

Because loc?: ?SourceLocation says that loc is optional. I don't remember exactly why, but I imagine this can happen with macros, e.g. *\foo.

Instead of reading the text directly from the lexer, I think it would be better to iterate over the contents of the node and extract the text from them, verifying that each is /[0-9]/. Check out how this is done in functions/char.js, for example.

@aaruni96
Copy link
Author

I have made the changes and and my commit is awaiting approval before it will be tested.

@codecov
Copy link

codecov bot commented Mar 16, 2022

Codecov Report

Merging #3583 (6b87dff) into main (3d5de92) will increase coverage by 0.49%.
The diff coverage is 94.73%.

❗ Current head 6b87dff differs from pull request most recent head 0528b89. Consider uploading reports for the commit 0528b89 to get more accurate results

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #3583      +/-   ##
==========================================
+ Coverage   92.99%   93.49%   +0.49%     
==========================================
  Files          91       89       -2     
  Lines        6779     6637     -142     
  Branches     1576     1541      -35     
==========================================
- Hits         6304     6205      -99     
+ Misses        437      401      -36     
+ Partials       38       31       -7     
Files Coverage Δ
src/environments/array.js 97.89% <94.73%> (-0.16%) ⬇️

... and 19 files with indirect coverage changes


Continue to review full report in Codecov by Sentry.

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

@aaruni96
Copy link
Author

Hello, I have added a test for this feature so that codecov is also happy.

@aaruni96
Copy link
Author

Hello, I have fixed all the things I could figure out how to fix.

The deploy checks for netlify are currently failing, but as far as I can tell, its not because of and of my changes ?

@aaruni96
Copy link
Author

Hello,
The PR now appears to pass all checks. Is it ready to merge?

@aaruni96
Copy link
Author

bump

@aaruni96
Copy link
Author

aaruni96 commented Oct 4, 2022

@edemaine does this PR require any more work from me to get merged?

It is now possible to describe an array environment like
\begin{array}{* {35}{l}}
This completes task number 3 on
[KaTeX#269](KaTeX#269)
Copy link
Member

@edemaine edemaine left a comment

Choose a reason for hiding this comment

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

Sorry this took so long to review. The main comment here is we should support * anywhere in the colalign string, not just at the beginning.

@@ -1260,6 +1260,10 @@ describe("A begin/end parser", function() {
expect`\begin{array}{cc}a&b\\c&d\end{array}`.toParse();
});

it("should parse an environment with argument in repetition format", function() {
expect`\begin{array}{*{35}{l}} x+y\le 12, \ 2x-y\ge 0, \ x-2y\le 0\end{array}`.toParse();
Copy link
Member

Choose a reason for hiding this comment

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

I think a toParseLike test would be more helpful here. E.g. \begin{array}{*{10}{l}}... should parse like \begin{array}{llllllllll}....

@@ -786,7 +786,31 @@ defineEnvironment({
const symNode = checkSymbolNodeType(args[0]);
const colalign: AnyParseNode[] =
symNode ? [args[0]] : assertNodeType(args[0], "ordgroup").body;
const cols = colalign.map(function(nde) {
let myColAlign = [];
if (colalign[0].text === "*") {
Copy link
Member

Choose a reason for hiding this comment

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

This seems to only support * at the beginning of colalign. But * can be used anywhere, e.g. \begin{array}{ll*{5}{lcr}rr*5{rrcl}}. I think ideally you'd do a map to expand all instances of *.

const cols = colalign.map(function(nde) {
let myColAlign = [];
if (colalign[0].text === "*") {
const repeatCol = assertNodeType(colalign[1], "ordgroup");
Copy link
Member

Choose a reason for hiding this comment

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

This seems to only support *{123} with braces. But for single-character numbers, you can omit the braces. For example, *5 should work.

const iRepeat = assertNodeType(repeatCol.body[i], "textord");
repeatString += iRepeat.text ;
}
const nRepeat = parseInt(repeatString);
Copy link
Member

Choose a reason for hiding this comment

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

Should use Number (or parseInt(..., 10)) to force decimal parsing.

I see that there's similar code (which uses Number) when computing numMaths. Could this be factored out into a helper function? (with the comment above too) Converting text to numbers like this is quite useful... In fact, I see a totally different implementation of it (which I maybe wrote?) in macros.js under defineMacro("\\char". 😮

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants