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

[gatsby-remark-katex] Doesn’t work with gatsby-plugin-mdx #21866

Closed
leafac opened this issue Feb 29, 2020 · 39 comments · Fixed by #24194
Closed

[gatsby-remark-katex] Doesn’t work with gatsby-plugin-mdx #21866

leafac opened this issue Feb 29, 2020 · 39 comments · Fixed by #24194
Assignees
Labels
help wanted Issue with a clear description that the community can help with. status: awaiting author response Additional information has been requested from the author status: confirmed Issue with steps to reproduce the bug that’s been verified by at least one reviewer. topic: remark/mdx Related to Markdown, remark & MDX ecosystem type: bug An issue or pull request relating to a bug in Gatsby

Comments

@leafac
Copy link
Contributor

leafac commented Feb 29, 2020

Description

I followed the instructions to install gatsby-remark-katex on a website using gatsby-plugin-mdx and it didn’t work. No errors, no nothing, it just didn’t transform the LaTeX in the Markdown.

Steps to reproduce

In this zip there are two projects:

doesnt-work: This is a minimal project to show the issue. To create this project I just followed the instructions to install gatsby-remark-katex.

works: This is a workaround I found based on this comment and the documentation for remark-math, in which they suggest to use rehype-katex instead of remark-html-katex.

You can run the projects with the usual npm install && npx gatsby develop and visit http://localhost:8000.

In summary, the difference is instead of:

// doesnt-work
$ npm install gatsby-transformer-remark gatsby-remark-katex katex

// gatsby-config.js
{
  resolve: `gatsby-plugin-mdx`,
  options: {
    gatsbyRemarkPlugins: [`gatsby-remark-katex`]
  }
}

you do:

// works
$ npm install remark-math rehype-katex katex

// gatsby-config.js
{
  resolve: `gatsby-plugin-mdx`,
  options: {
    remarkPlugins: [require(`remark-math`)],
    rehypePlugins: [require(`rehype-katex`)]
  }
}

Expected result

The LaTeX in the Markdown should be rendered.

Actual result

The LaTeX in the Markdown appears unmodified.

Environment

System:
  OS: macOS 10.15.3
  CPU: (4) x64 Intel(R) Core(TM) i5-7360U CPU @ 2.30GHz
  Shell: 5.7.1 - /bin/zsh
Binaries:
  Node: 13.7.0 - /usr/local/bin/node
  npm: 6.14.1 - /usr/local/bin/npm
Languages:
  Python: 2.7.16 - /usr/bin/python
Browsers:
  Chrome: 80.0.3987.122
  Safari: 13.0.5
@leafac leafac added the type: bug An issue or pull request relating to a bug in Gatsby label Feb 29, 2020
@TylerBarnes TylerBarnes added help wanted Issue with a clear description that the community can help with. status: confirmed Issue with steps to reproduce the bug that’s been verified by at least one reviewer. labels Mar 7, 2020
@github-actions
Copy link

Hiya!

This issue has gone quiet. Spooky quiet. 👻

We get a lot of issues, so we currently close issues after 30 days of inactivity. It’s been at least 20 days since the last update here.
If we missed this issue or if you want to keep it open, please reply here. You can also add the label "not stale" to keep this issue open!
As a friendly reminder: the best way to see this issue, or any other, fixed is to open a Pull Request. Check out gatsby.dev/contribute for more information about opening PRs, triaging issues, and contributing!

Thanks for being a part of the Gatsby community! 💪💜

@github-actions github-actions bot added the stale? Issue that may be closed soon due to the original author not responding any more. label Mar 27, 2020
@danedavid
Copy link
Contributor

Hey, I'd like to take a shot at this as my first contribution to the gatsby codebase. Been looking into how plugins work in the past days. Need to invest more time over the weekend. Meanwhile, any tips anyone?

@leafac leafac added not stale and removed stale? Issue that may be closed soon due to the original author not responding any more. labels Mar 27, 2020
@leafac
Copy link
Contributor Author

leafac commented Mar 27, 2020

@danedavid: Here’s what I’d start with:

  1. Setup the local development environment.

  2. Modify the sample website doesnt-work that I included in this issue to use the local Gatsby installation. (Use npm link.)

  3. Check that the remark-math parser plugin is being picked up. I’d modify the line to read something like:

module.exports.setParserPlugins = () => { console.log("PLUGIN IS BEING PICKED UP"); return [remarkMath]; }
  1. Check that the gatsby-remark-katex plugin itself is being picked up. I’d add a console.log() there as well.

  2. Check that the visitors are finding the math nodes and producing appropriate results.

I’d expect the problem to become evident somewhere along this process.

Warning: Take what I’m saying with a grain of salt, as I’ve never contributed to the Gatsby codebase myself.

@danedavid
Copy link
Contributor

danedavid commented Apr 3, 2020

I looked into this. It appears the plugin is picked up. It looks like the issue is with the generated markdownAST. When I console.log(markdownAST) inside the gatsby-remark-plugin, the AST does not contain any inlineMath node. But when I check in GraphiQL with query:

{
  allMdx {
    edges {
      node {
        mdxAST
      }
    }
  }
}

the result AST has an inlineMath node. The issue then could be parser not being picked up, but I did do step 3 of @leafac 's comment, and the log showed up.

I also put logs inside visit callback. The logs are not appearing when the pages are built, but logs show (including the correct generated HTML) when I run the query in GraphiQL.

@leafac
Copy link
Contributor Author

leafac commented Apr 3, 2020

Could it be that gatsby-plugin-mdx isn’t using the mdxAST field, but something like the body field?

@danedavid
Copy link
Contributor

Finally got time, and fixed it. The issue was with mdx-loader used by webpack not picking up gatsby remark parsers. Thank you @leafac for your support!

@danabrit danabrit added the topic: remark/mdx Related to Markdown, remark & MDX ecosystem label May 29, 2020
@danabrit
Copy link
Contributor

@leafac Can you please confirm whether this PR - #21861 - resolved your issue?

@danabrit danabrit added the status: awaiting author response Additional information has been requested from the author label May 29, 2020
@leafac
Copy link
Contributor Author

leafac commented Jun 1, 2020

What’s the best way for me to check?

I tried to clone https://github.com/jlkiri/gatsby/, switch to the mdx-interop-default-exp branch, go to packages/gatsby-plugin-mdx/ directory, run npm install && npm link, go to my doesnt-work example, and run npm link gatsby-plugin-mdx. But that failed with something to the effect of Cannot find module 'webpack'. I think it has to do with missing peer dependencies. There must be a better way to do this…

@ascorbic
Copy link
Contributor

ascorbic commented Jun 4, 2020

@leafac The branch has been merged, so you should just need to check with the latest version of Gatsby. If you can do that, let us know if it fixes it for you.

@leafac
Copy link
Contributor Author

leafac commented Jun 4, 2020

I tried it with the latest versions and the problem remains. Here’s my package.json:

{
  "dependencies": {
    "@mdx-js/mdx": "^1.6.5",
    "@mdx-js/react": "^1.6.5",
    "gatsby": "^2.23.0",
    "gatsby-plugin-mdx": "^1.2.14",
    "gatsby-remark-katex": "^3.3.3",
    "gatsby-source-filesystem": "^2.3.10",
    "katex": "^0.11.1",
    "react": "^16.13.1",
    "react-dom": "^16.13.1"
  }
}

To try it for yourself, just use this package.json on the original doesnt-work reproduction project I posted in my original message.

@danedavid
Copy link
Contributor

The issue is with webpack mdx-loader not picking up remark parsers in gatsby-plugin-mdx. I have raised a PR #24194, but not sure if the approach is right. But in that branch the issue is fixed.

@leafac
Copy link
Contributor Author

leafac commented Jun 5, 2020

What’s a practical way for me to test that branch?

@danedavid
Copy link
Contributor

@leafac No easy way. Clone, build gatsby-plugin-mdx and link the project that reproduces the error 🤷

@leafac
Copy link
Contributor Author

leafac commented Jun 12, 2020

I’m sorry but I don’t have the time to test this right now, so I’ll just take @danedavid’s word that the issue is fixed.

@activatedgeek
Copy link

Hmm, funny. I have had this issue before and was using the workaround for a while. Not sure what has changed since then but the usual instructions work for me now

In gatsby-config.js,

    {
      resolve: `gatsby-plugin-mdx`,
      options: {
        gatsbyRemarkPlugins: [
          {
            resolve: `gatsby-remark-katex`,
            options: {
              strict: `ignore`,
            },
          },
       }
    }

and in gatsby-browser.js

import "katex/dist/katex.min.css"

For someone who wants to inspect further, the complete source is here.

@ehowey
Copy link
Contributor

ehowey commented Jul 17, 2020

@danedavid I will just second what @leafac already said. Sounds like you are missing the CSS. There are instructions on the plugin page for Gatsby.

@Stvad
Copy link

Stvad commented Mar 20, 2021

Still facing this and had to use the remark/rehype plugins directly =\

@nitzanvolman
Copy link

What ended fixing the problem for me (march/2021 - "gatsby": "^3.1.0", "gatsby-plugin-mdx": "^2.1.0", ) was explicitly including an older version of "remark-math": "^3.0.1", instead of the latest version which is 4
version 4 relies on the newest version of remark with all of the newest bells and whistles, while gatsby-plugin-mdx still relies on remark v12.
I hope this helps someone.

@tnorlund
Copy link

What ended fixing the problem for me (march/2021 - "gatsby": "^3.1.0", "gatsby-plugin-mdx": "^2.1.0", ) was explicitly including an older version of "remark-math": "^3.0.1", instead of the latest version which is 4
version 4 relies on the newest version of remark with all of the newest bells and whistles, while gatsby-plugin-mdx still relies on remark v12.
I hope this helps someone.

What's your gatsby-config look like with these packages?

@princefishthrower
Copy link

@nitzanvolman @tnorlund - I'd also be interested in seeing that. Still struggling with MDX + Katex on Gatsby v3

@nikoladev
Copy link
Contributor

@tnorlund @princefishthrower I had the same problem and here's the package versions I used and my gatsby-config.js. Hope it helps!

@tnorlund
Copy link

@tnorlund @princefishthrower I had the same problem and here's the package versions I used and my gatsby-config.js. Hope it helps!

Thanks for the help, but the Katex font doesn't match with the one I'm using in Latexit. Do you know how I can get this font?
LatexKatex

@nitzanvolman
Copy link

@tnorlund @princefishthrower

What's your gatsby-config look like with these packages?

// gatsby-config.js
{
    resolve: 'gatsby-plugin-mdx',
    options: {
      extensions: ['.mdx', '.md'],
      remarkPlugins: [
        require('../../plugins/remark-element-directive'),
        // require('remark-images'),
        require('remark-math'),
        require('remark-emoji'),
      ],
      rehypePlugins: [
        require('rehype-katex'),
      ],
      gatsbyRemarkPlugins: [
        {
          resolve: 'gatsby-remark-autolink-headers',
          options: {
            // offsetY: '100',
            icon: false,
            // icon: '<svg aria-hidden="true" height="20" version="1.1" viewBox="0 0 16 16" width="20"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg>',
            // maintainCase: true,
            // removeAccents: true,
            isIconAfterHeader: false,
            elements: ['h2'],
          },
        },
      ],
    },
  },
// package.json
...
    "@mdx-js/mdx": "^1.6.22",
    "@mdx-js/react": "^1.6.22",
    "gatsby": "^3.1.0",
    "gatsby-plugin-mdx": "^2.1.0",
    "remark-html-katex": "^3.0.0",
   "remark-math": "^3.0.1",
...

@tnorlund
Copy link

@nitzanvolman

//gatsby-config.js
module.exports = {
...
  plugins: [
    {
      resolve: `gatsby-plugin-mdx`,
      options: {
        remarkPlugins: [
...
          require( `remark-math` ),
          require( `remark-html-katex` ),
        ],
        extensions: [`.md`, `.mdx`],
      },
    },
...
  ]
}
// package.json
{
...
  "dependencies": {
...
    "@mdx-js/mdx": "^1.6.22",
    "@mdx-js/react": "^1.6.22",
    "gatsby": "^3.1.2",
    "gatsby-plugin-mdx": "^2.1.0",
    "remark-html-katex": "^3.0.0",
    "remark-math": "^3.0.1",
...
  },
...
}

@tnorlund
Copy link

I'm a node n00b, but I can't resolve the packages correctly using this configuration of remark-math and remark-html-katex. How do I still use my other packages like gatsby-remark-vscode?

//gatsby-config.js
module.exports = {
  plugins: [
    {
      resolve: `gatsby-plugin-mdx`,
      options: {
        remarkPlugins: [
          {
            resolve: `gatsby-remark-vscode`,
          },
          // `gatsby-remark-vscode`, <- This no longer works
          require( `remark-math` ),
          require( `remark-html-katex` ),
        ],
        extensions: [`.md`, `.mdx`],
      },
    },

@ehowey
Copy link
Contributor

ehowey commented Mar 29, 2021

Has anyone had problems with gatsby-remark-katex not installing into your node_modules? It is the weirdest thing. It is in my package.json, in my yarn.lock, but nowhere to be found in the node_modules folder. I have never had that kind of thing happen before. Any ideas about that?

@ascorbic
Copy link
Contributor

@ehowey Try running node -e "console.log(require.resolve('gatsby-remark-katex'))" in the root of the project

@tnorlund
Copy link

@tnorlund @princefishthrower I had the same problem and here's the package versions I used and my gatsby-config.js. Hope it helps!

Thanks for the help, but the Katex font doesn't match with the one I'm using in Latexit. Do you know how I can get this font?
LatexKatex

\mathbf{} Produces the same thing in Latexit and Katex. What is Katex's default mode for inline formatting? How do I replicate it in normal Latexit?

@tnorlund
Copy link

Just figured out that the default is \text{}. Would still like to know how to continue to use my packages with require( 'remark-math' ) and require( 'remark-html-katex' ) in gatsby-config.js.

@tnorlund
Copy link

This is what I have so far. It works, but I cannot write 'complex' equations.

// gatsby-config.js
const remark_math = require( `remark-math` )
const remark_html_katex = require( `remark-html-katex` )

module.exports = {
  plugins: [
    {
      resolve: `gatsby-plugin-mdx`,
      options: {
        gatsbyRemarkPlugins: [
          {
            resolve: `gatsby-remark-katex`,
            options: {
              strict: `ignore`,
            }
          },
        ],
        remarkPlugins: [ remark_math, remark_html_katex ],
        extensions: [`.md`, `.mdx`],
      },
    },

@ben-dyer
Copy link

@tnorlund thanks for sharing the config, it also works for me except my macros in the gatsby-remark-katex options stopped working.

@jannikbuschke
Copy link
Contributor

It seems this is not properly documented yet and maybe the root issue is not yet resolved?

When digging through this thread I also faced issues with the recommend solutions, i.e. require('remark-math'), as require is not allowed.

And the different plugins (remark-math, remark-html-katex, gatsby-remark-katex, rehype-katex) also confuse me.

Am I assuming correct, that what is currently documented

// doesnt-work
$ npm install gatsby-transformer-remark gatsby-remark-katex katex

// gatsby-config.js
{
  resolve: `gatsby-plugin-mdx`,
  options: {
    gatsbyRemarkPlugins: [`gatsby-remark-katex`]
  }
}

is the way it should work, but does not?

@yuhsak
Copy link

yuhsak commented Mar 10, 2022

@jannikbuschke

It seems this is not properly documented yet and maybe the root issue is not yet resolved?

Unfortunately not yet. The root issue is that remark-math which is a core dependency of gatsby-remark-katex requires remark 13 to get working.

However then it seems to need updating @mdx-js/mdx to 2.0.0 first and it'd be a bit heavy work to get done.

is the way it should work, but does not?

I agree with you. It should work unless such incompatibility. But thus we need some workaround for right now.

Since it's not a specific issue only with gatsby-remark-katex but more about general version compatibility, it maybe more helpful if it was somehow documented.

Workaround

FYI, here is the workaround to enable KaTeX with gatsby-plugin-mdx.

There are two points.

  1. use remark-math and rehype-katex insted of gatsby-remark-katex
  2. explicitly specify compatible version of each

Version

[email protected] [email protected]

Snippet

gatsby-config.js

module.exports = {
  plugins: [
    {
      resolve: 'gatsby-plugin-mdx',
      options: {
        remarkPlugins: [
          require('remark-math'),
        ],
        rehypePlugins: [
          [require('rehype-katex'), { strict: 'ignore' }],
        ],
      }
    }
  ]
}

gatsby-browser.js

import 'katex/dist/katex.min.css'

Note that katex module comes with rehype-katex therefor it doesn't have to be installed individually.

@dinhanhthi
Copy link

@jannikbuschke

It seems this is not properly documented yet and maybe the root issue is not yet resolved?

Unfortunately not yet. The root issue is that remark-math which is a core dependency of gatsby-remark-katex requires remark 13 to get working.

However then it seems to need updating @mdx-js/mdx to 2.0.0 first and it'd be a bit heavy work to get done.

is the way it should work, but does not?

I agree with you. It should work unless such incompatibility. But thus we need some workaround for right now.

Since it's not a specific issue only with gatsby-remark-katex but more about general version compatibility, it maybe more helpful if it was somehow documented.

Workaround

FYI, here is the workaround to enable KaTeX with gatsby-plugin-mdx.

There are two points.

  1. use remark-math and rehype-katex insted of gatsby-remark-katex
  2. explicitly specify compatible version of each

Version

[email protected] [email protected]

Snippet

gatsby-config.js

module.exports = {
  plugins: [
    {
      resolve: 'gatsby-plugin-mdx',
      options: {
        remarkPlugins: [
          require('remark-math'),
        ],
        rehypePlugins: [
          [require('rehype-katex'), { strict: 'ignore' }],
        ],
      }
    }
  ]
}

gatsby-browser.js

import 'katex/dist/katex.min.css'

Note that katex module comes with rehype-katex therefor it doesn't have to be installed individually.

I works (I'm using Gatsby 4.12.1 and gatsby-plugin-mdx 3.9.1)

@ingyer-ks
Copy link

Dear all,
I found inserting

import 'katex/dist/katex.min.css'

in gatsby-browser.js doesn't work.
Instead, I linked the css in gatsby-ssr.js like

  <link
    rel="stylesheet"
    href="https://cdn.jsdelivr.net/npm/[email protected]/dist/katex.min.css"
    integrity="sha384-AfEj0r4/OFrOo5t7NnNe46zW/tFgW6x/bCJG8FqQCEo3+Aro6EYUG4+cU+KJWu/X"
    crossorigin="anonymous"
  ></link>

and now it works.
My environment is Gatsby 3.13.0, gatsby-plugin-mdx 2.13.0.
Hope help somebody.

@RobinL
Copy link

RobinL commented Sep 15, 2023

For What It's Worth, this is the setup that worked for me:

Dependencies:

"gatsby": "^5.12.4",
"remark-math": "^5.1.1",
"rehype-katex": "^6.0.3",

Gatsby Configuration:

{
  resolve: `gatsby-plugin-mdx`,
  options: {
    mdxOptions: {
      remarkPlugins: [remarkMath],
      rehypePlugins: [
        [rehypeKatex, { strict: 'ignore' }],
      ],
    },
  },
}

Example in .mdx:

import 'katex/dist/katex.min.css'

$$
x = \frac{{-b \pm \sqrt{{b^2 - 4ac}}}}{{2a}}
$$

The formula \( a + b = 2 \) tells us something important.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Issue with a clear description that the community can help with. status: awaiting author response Additional information has been requested from the author status: confirmed Issue with steps to reproduce the bug that’s been verified by at least one reviewer. topic: remark/mdx Related to Markdown, remark & MDX ecosystem type: bug An issue or pull request relating to a bug in Gatsby
Projects
None yet
Development

Successfully merging a pull request may close this issue.