gatsby-remark-katex plugin in my Gatsby blog is not working correctly

Viewed 35

I want to be able to render Math equations in my Gatsy blog both in inline mode & display mode.

I write my blogs using Markdown.

I installed the gatsby-remark-katex plugin by folling the instructions at the following link: gatsby-remark-katex

Essentially there are three steps:

STEP-1: Install gatsby-transformer-remark gatsby-remark-katex katex I have not made any mistake in installation. I see these packages in my package.json file.

STEP-2: Add the following code snippet in gatsby.config.js.

{
  resolve: `gatsby-remark-katex`,
  options: {
    strict: `ignore`,
  },
},

After I added the above code snippet, my gatsby.config.js looks like the following:

module.exports = {
  siteMetadata: { title: "Hemanta Sundaray - Full-stack JavaScript Engineer" },
  plugins: [
    {
      resolve: `gatsby-plugin-google-gtag`,
      options: {
        trackingIds: ["UA-180842449-2"],
      },
    },
    `gatsby-plugin-react-helmet`,
    `gatsby-plugin-sharp`,
    {
      resolve: "gatsby-source-filesystem",
      options: {
        name: "src",
        path: `${__dirname}/src`,
      },
    },

    {
      resolve: `gatsby-transformer-remark`,
      options: {
        plugins: [
          {
            resolve: `gatsby-remark-katex`,
            options: {
              strict: `ignore`,
            },
          },
          `gatsby-remark-reading-time`,
          {
            resolve: `gatsby-remark-code-titles`,
          },
          {
            resolve: `gatsby-remark-vscode`,
            options: {
              theme: `SynthWave '84`,
              extensions: ["synthwave-vscode"],
              inlineCode: {
                marker: ".",
              },
            },
          },
          {
            resolve: `gatsby-remark-images`,
            options: {
              maxWidth: 800,
              quality: 100,
            },
          },
          {
            resolve: `gatsby-remark-table-of-contents`,
            options: {
              exclude: "Table of Contents",
              // tight: false,
              // ordered: false,
              fromHeading: 2,
              toHeading: 4,
              className: "table-of-contents",
            },
          },
          `gatsby-remark-autolink-headers`,
        ],
      },
    },
  ],
}

Have I made any mistake in this step?

STEP-3: Add KATEX CSS to your template.

My gatsby.browser.js file looks like the following after adding the KATEX CSS.

import "./src/styles/global.css"
import "katex/dist/katex.min.css"

Is gatsby.browser.js the right file in which Katex CSS needs to be added?

I also tried adding the Katex CSS in the index.js file; however the output was not as expected.

After following the above-mentioned 3 steps, I tested the following markdown:

Example: $a^2 + b^2 = c^2$

However, Math equation did not render correctly:

I received the following output:

Example: $a^2 + b^2 = c^2$

What am I doing wrong?

1 Answers

I think your problem is in step 3.

You need to add import "katex/dist/katex.min.css" in the same file where you build your blog-post component starting from the markdown file.

The one file where you probably do something like dangerouslySetInnerHTML={{ __html: html }}

Related