question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

KaTeX expressions with escapes and braces throw errors

See original GitHub issue

Subject of the issue

I’m trying to setup a Next.js site with mdx and these remark plugins:

  • remark-math@1.0.5
  • @kwangkim/remark-jsx-katex@1.11.4 (I couldn’t find the module’s source on GitHub, but you can view it on this RunKit)

And this file under pages/:

This is some extra text that gets rendered correctly if I remove the KaTeX block below:

$$
23_{10} = 2^4+2^2+2^1+2^0 = \underbrace{10111_2}_{\mathrm{binary\ representation}}
$$

I assume the escapes inside braces ({}) are confusing MDX and it’s trying to parse them as a expression, which causes the following error:

React error

(gist error)

As you can see, it’s complaining about a “bad character escape sequence”. One way to fix this would be to ignore all the braces and escapes {}\ inside $ (inlineMath) or $$ (math) blocks.

Your environment

  • Windows 10 Pro 1803
  • @zeit/next-mdx: 1.2.0
    • @mdx-js/mdx: 0.15.7
  • next: 7.0.2-canary.49

Steps to reproduce

Create new Next.js project with the following next.config.js file:

const remarkMath = require('remark-math');
const katex = require('@kwangkim/remark-jsx-katex');

module.exports = require('@zeit/next-mdx')({
  options: {
    mdPlugins: [remarkMath, katex],
  },
  pageExtensions: ['js', 'mdx']
});

Now, create pages/foo.mdx:

This is some extra text that gets rendered correctly if I remove the KaTeX block below:

$$
23_{10} = 2^4+2^2+2^1+2^0 = \underbrace{10111_2}_{\mathrm{binary\ representation}}
$$

Now, run next and load localhost:3000/foo in your browser.

Expected behaviour

The KaTeX escapes should be ignored even if they are inside braces ({}).

Actual behaviour

The KaTeX escapes and generally, all the content inside braces doesn’t get ignored, causing compiling and React rendering issues.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:3
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
ChristopherBiscardicommented, Dec 30, 2018

The escapes get removed by KaTeX when rendering

I believe this is true for any backslashed content in an MDX file (especially ones that are escape-code related cause trouble). It’s also the way I got https://github.com/mdx-js/mdx/pull/366#issuecomment-449937820 working. Wonder if something like this rehype plugin should be included by default?

1reaction
ChristopherBiscardicommented, Dec 29, 2018

We ran into this in gatsby-mdx too (actually we sort of run into things like this a lot because of the way the gatsby plugins function). I added some replacement code in a babel plugin that we use to post-process MDX content after remark and hast plugins have run but before we ship the JSX.

if (
  path.node.openingElement.name.name ===
  "annotation" /* && if gatsby-remark-katex is enabled */
) {
  const genCode = path.node.children.map(ast => generate.default(ast).code);
  path.node.children = [
    t.jSXText(
      genCode
        .join("")
        .replace("{", "{")
        .replace("}", "}")
    )
  ];
}

As you can see, it’s complaining about a “bad character escape sequence”. One way to fix this would be to ignore all the braces and escapes {}\ inside $ (inlineMath) or $$ (math) blocks.

There’s a similar “javascript string escaping”-related issue in https://github.com/mdx-js/mdx/pull/366. so I wonder if we should keep adding to the list of “non-escaped sections” (code blocks, $ or $$ sequences, etc) or somehow default to “html style” interpretation for special chars like {, \u, etc and force \{ or something for bracket literals to get passed through into the final jsx? dunno, haven’t had a moment to think through the ramifications here all the way.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Handling Errors - KaTeX
If KaTeX encounters an error (invalid or unsupported LaTeX) and throwOnError hasn't been set to false , then katex.render and katex.
Read more >
Why aren't the braces showing - TeX - LaTeX Stack Exchange
If you change the expression to the one in your question then you get the error message ! LaTeX Error: Bad math environment...
Read more >
Getting PEP8 "invalid escape sequence" warning trying to ...
If you're trying to split on ")(" then here's a working example that doesn't throw PEP8 warnings: import re string = r"Hello my...
Read more >
Troubleshooting MDX
The reason for this error is that the parser is expecting a JavaScript expression. If you just want braces instead of an expression,...
Read more >
Template literals (Template strings) - JavaScript | MDN
However, invalid escape sequences will cause a syntax error, ... which are embedded expressions delimited by a dollar sign and curly braces: ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found