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.

remark-remark-katex replaces LaTeX math with wrong html

See original GitHub issue

Description

I am trying to make Gatsby render LaTeX math formulas in my blog posts.

I’m using the standard gatsby-transformer-remark with gatsby-remark-katex plugin configured as follows:

From gatsby-config.js (full code available here):

module.exports = {
  siteMetadata: {
    title: `Personal blog of Boris Burkov`,
    author: `Boris Burkov`,
    description: `A personal blog of Boris Burkov, version 4.1, this time written in Gatsby.`,
    siteUrl: `https://borisburkov.net/`,
    social: {
      telegram: `BorisBurkov`,
    },
  },
  plugins: [
      ...
      {
      resolve: `gatsby-transformer-remark`,
      options: {
        plugins: [
          ...
          {
            resolve: `gatsby-remark-katex`,
            options: {
              // Add any KaTeX options from https://github.com/KaTeX/KaTeX/blob/master/docs/options.md here
              strict: `ignore`
            }
          }
          ...
        ]
      ...
    ],
};

In the markdown of my blog posts I’m using both inline math and display-mode math like this:

If we assume for a second that HDD failures were independent, then probability of
a failure of X drives at the same time corresponds to Poisson distribution:

$$
p(failure of 1 HDD) = 10^{-5}
$$

$N(HDD) = 2400$

In the resulting html page these math formulas are strangely converted to the following incorrect html:

<div class="math math-display katex">p(failure of 1 HDD) = 10^{-5}</div>

Now, if I go to the source code of gatsby-remark-katex/index.js and add check, if it sees my inline math and if it replaces it with proper html, I see that it actually does. I add console.log('node.value = ${node.value}') statement to the code:

gatsby-remark-katex/index.js

"use strict";

const visit = require(`unist-util-visit`);

const katex = require(`katex`);

const remarkMath = require(`remark-math`);

module.exports = ({
  markdownAST
}, pluginOptions = {}) => {
  visit(markdownAST, `inlineMath`, node => {
    node.type = `html`;
    node.value = katex.renderToString(node.value, {
      displayMode: false,
      ...pluginOptions
    });
    console.log(`node.value = ${node.value}`)
  });
  visit(markdownAST, `math`, node => {
    node.type = `html`;
    node.value = katex.renderToString(node.value, {
      displayMode: true,
      ...pluginOptions
    });
  });
};

module.exports.setParserPlugins = () => [remarkMath];

When I say gatsby develop, in the console I see the expected html for math formulas, rendered by katex:

node.value = <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>N</mi><mo stretchy="false">(</mo><mi>H</mi><mi>D</mi><mi>D</mi><mo
stretchy="false">)</mo><mo>=</mo><mn>2400</mn></mrow><annotation encoding="application/x-tex">N(HDD) = 2400</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut"
style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathnormal" style="margin-right:0.10903em;">N</span><span class="mopen">(</span><span class="mord mathnormal" style="margin-right:0.08125em;">H</span><span
class="mord mathnormal" style="margin-right:0.02778em;">D</span><span class="mord mathnormal" style="margin-right:0.02778em;">D</span><span class="mclose">)</span><span class="mspace"
style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut"
style="height:0.64444em;vertical-align:0em;"></span><span class="mord">2</span><span class="mord">4</span><span class="mord">0</span><span class="mord">0</span></span></span></span>

Problem is that html wouldn’t show up in the resulting page. It wouldn’t appear in GraphQL of the page in html or htmlAST field either, if I go to query the GraphiQL for it:

Screenshot 2021-05-14 at 19 37 23

Here’s the page output, note the un-rendered formulae:

Screenshot 2021-05-14 at 19 55 00

Steps to reproduce

Full source code of my Gatsby 3-based blog is available here: https://github.com/BurkovBA/BurkovBA.github.io.git.

git clone https://github.com/BurkovBA/BurkovBA.github.io.git
nvm use 15
npm install
npm run develop
browser http://localhost:8000/2021-03-11-1/

Expected result

I expected LaTeX formulas rendered in the lower part of my latest blog post: http://localhost:8000/2021-03-11-1/ as the following html:

<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>N</mi><mo stretchy="false">(</mo><mi>H</mi><mi>D</mi><mi>D</mi><mo
stretchy="false">)</mo><mo>=</mo><mn>2400</mn></mrow><annotation encoding="application/x-tex">N(HDD) = 2400</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut"
style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathnormal" style="margin-right:0.10903em;">N</span><span class="mopen">(</span><span class="mord mathnormal" style="margin-right:0.08125em;">H</span><span
class="mord mathnormal" style="margin-right:0.02778em;">D</span><span class="mord mathnormal" style="margin-right:0.02778em;">D</span><span class="mclose">)</span><span class="mspace"
style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut"
style="height:0.64444em;vertical-align:0em;"></span><span class="mord">2</span><span class="mord">4</span><span class="mord">0</span><span class="mord">0</span></span></span></span>

Actual result

LaTeX is not rendered, but just replaced with pretty useless divs:

<div class="math math-display">p(failure of 1 HDD) = 10^{-5}</div>

Environment

  System:
    OS: macOS 11.1
    CPU: (8) arm64 Apple M1
    Shell: 3.2.57 - /bin/bash
  Binaries:
    Node: 15.14.0 - ~/.nvm/versions/node/v15.14.0/bin/node
    npm: 7.12.1 - ~/.nvm/versions/node/v15.14.0/bin/npm
  Languages:
    Python: 3.9.2 - /Users/burkov/miniforge3/bin/python
  Browsers:
    Safari: 14.0.2
  npmPackages:
    gatsby: ^3.4.2 => 3.4.2 
    gatsby-plugin-feed: ^3.4.0 => 3.4.0 
    gatsby-plugin-google-analytics: ^3.4.0 => 3.4.0 
    gatsby-plugin-image: ^1.4.1 => 1.4.1 
    gatsby-plugin-manifest: ^3.4.0 => 3.4.0 
    gatsby-plugin-offline: ^4.4.0 => 4.4.0 
    gatsby-plugin-react-helmet: ^4.4.0 => 4.4.0 
    gatsby-plugin-sass: ^4.4.0 => 4.4.0 
    gatsby-plugin-sharp: ^3.4.2 => 3.4.2 
    gatsby-plugin-styled-components: ^4.4.0 => 4.4.0 
    gatsby-plugin-typography: ^3.4.0 => 3.4.0 
    gatsby-remark-copy-linked-files: ^4.1.0 => 4.1.0 
    gatsby-remark-embed-gist: ^1.2.1 => 1.2.1 
    gatsby-remark-images: ^5.1.0 => 5.1.0 
    gatsby-remark-katex: ^5.1.0 => 5.1.0 
    gatsby-remark-prismjs: ^5.1.0 => 5.1.0 
    gatsby-remark-responsive-iframe: ^4.1.0 => 4.1.0 
    gatsby-remark-smartypants: ^4.1.0 => 4.1.0 
    gatsby-source-filesystem: ^3.4.0 => 3.4.0 
    gatsby-transformer-remark: ^4.1.0 => 4.1.0 
    gatsby-transformer-sharp: ^3.4.0 => 3.4.0 
  npmGlobalPackages:
    gatsby-cli: 3.4.1

No flags inside gatsby-config.js.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
LekoArtscommented, May 17, 2021
1reaction
hendra-gocommented, May 15, 2021

This might be duplicate #30865

Read more comments on GitHub >

github_iconTop Results From Across the Web

upgrading to gatsby-remark-katex v5.0.0 and/or ... - GitHub
The reason for katex math equations breaking after upgrading to the ... remark-remark-katex replaces LaTeX math with wrong html #31427.
Read more >
Wrong to delete/replace equation by using changes package
I am using changes package to check the changing in my latex file. For changing in sentence, it works well ...
Read more >
Math Rendering is Wrong - Daniel's Blog
A skeptic might say that it is not possible to render LaTeX to HTML and CSS ahead of time. This might even have...
Read more >
Some Tips and Tricks for Using LaTeX in Math Theses
This document (latextips.tex) is NOT a good model to build a math thesis from. The margins are wrong, the spacing is wrong, and...
Read more >
Missing $ inserted - Overleaf, Online LaTeX Editor
Many math symbols in LaTeX are accessed using commands which must only be used ... issue an error such as Missing $ inserted...
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