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

The error "KaTeX parse error: Invalid size" occurred when using \\ [ #3745

Closed
ZigZagK opened this issue Nov 1, 2022 · 5 comments · Fixed by #3746
Closed

The error "KaTeX parse error: Invalid size" occurred when using \\ [ #3745

ZigZagK opened this issue Nov 1, 2022 · 5 comments · Fixed by #3746
Labels

Comments

@ZigZagK
Copy link

ZigZagK commented Nov 1, 2022

Describe the bug:

When using \\[ such as:

a+b=c\\
[f(x)]^2

The error KaTeX parse error: Invalid size: 'f(x)' at position 9: a+b=c\\ [̲f̲(̲x̲)̲]̲^2 occurred.

To Reproduce:
Can be reproduced at https://katex.org/.

Environment (please complete the following information):

  • KaTeX Version: 0.16.3
@ZigZagK ZigZagK added the bug label Nov 1, 2022
@ronkok
Copy link
Collaborator

ronkok commented Nov 1, 2022

That is not a bug. KaTeX is acting in precisely the same way that LaTeX acts with this input. When you write \\[,KaTeX is expecting you to specify a line skip distance, e.g., \\[2em].

There are many work arounds. For instance

a+b=c\\
{}[f(x)]^2

@ronkok ronkok removed the bug label Nov 1, 2022
@ZigZagK
Copy link
Author

ZigZagK commented Nov 1, 2022

Thank you for replying.
However, I think it's a bit strange that \\[ is considered to be a line skip distance when \\ and [ are not even in the same line.
And it can be parsed in MathJax successfully, so I still think it should be improved.
Thanks again!

@edemaine
Copy link
Member

edemaine commented Nov 1, 2022

@ronkok I thought the same, but then I tested the following in LaTeX:

\begin{align*}
a+b=c \\
[f(x)]^2
\end{align*}

In fact, \\ seems to detect the whitespace before the [, and not treat it as an optional argument in this case.

In fact, the following KaTeX code seems to be trying to implement exactly this:

if (parser.gullet.future().text !== " ") {
size = parser.parseSizeGroup(true);
}

In fact, the above example works in KaTeX! What doesn't work is \\ outside of an align environment. Technically that shouldn't work at all in LaTeX (can't write $a \\ b$ for example), but we've accepted it as a kind of extension (inspired by MathJax).

It looks like the culprit is src/functions/cr.js, which uses numOptionalArgs to fetch the optional argument. It does seem that \@ifnextchar consumes spaces in LaTeX, so the default processing of optional arguments is correct. AMSMath's definition of \math@cr is just special to avoid consuming spaces, via \new@ifnextchar.

@edemaine edemaine changed the title The error "KaTeX parse error: Invalid size" occurred when using \\[ The error "KaTeX parse error: Invalid size" occurred when using \\ [ Nov 1, 2022
edemaine added a commit that referenced this issue Nov 1, 2022
Fix `src/functions/cr.js`'s definition of `\\` to manually look for an
optional argument via `future()` instead of `numOptionalArgs`, so that
it does *not* skip over spaces.  This matches the existing behavior of
`\\` in `src/environments/array.js` and AMSMath's behavior of `\math@cr`
via `\new@ifnextchar`.

Fixes #3745
ronkok added a commit that referenced this issue Dec 7, 2022
Fix `src/functions/cr.js`'s definition of `\\` to manually look for an
optional argument via `future()` instead of `numOptionalArgs`, so that
it does *not* skip over spaces.  This matches the existing behavior of
`\\` in `src/environments/array.js` and AMSMath's behavior of `\math@cr`
via `\new@ifnextchar`.

Fixes #3745

Co-authored-by: Ron Kok <ronkok@comcast.net>
@ronkok
Copy link
Collaborator

ronkok commented Dec 7, 2022

Resolved via PR #3746.

KaTeX-bot added a commit that referenced this issue Dec 7, 2022
## [0.16.4](v0.16.3...v0.16.4) (2022-12-07)

### Bug Fixes

* space should prevent optional argument to \\ ([#3746](#3746)) ([a0deb34](a0deb34)), closes [#3745](#3745)
@KaTeX-bot
Copy link
Member

🎉 This issue has been resolved in version 0.16.4 🎉

The release is available on:

Your semantic-release bot 📦🚀

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

Successfully merging a pull request may close this issue.

4 participants