GithubHelp home page GithubHelp logo

Comments (6)

mreishus avatar mreishus commented on July 27, 2024 6

I got it to work. Here's my solution.

In template file.

  1. Add script tag to helmet
        <Helmet
          htmlAttributes={{ lang: 'en' }}
          meta={[{ name: 'description', content: siteDescription }]}
          title={`${post.frontmatter.title} | ${siteTitle}`}
        >
          <script
            type="text/javascript"
            src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-AMS-MML_HTMLorMML"
            async
          />
        </Helmet>
  1. Create hacky on script load function. I grabbed this from an issue on the react helmet page.
function waitForGlobal(name, timeout = 300) {
  return new Promise((resolve, reject) => {
    let waited = 0

    function wait(interval) {
      setTimeout(() => {
        waited += interval
        // some logic to check if script is loaded
        // usually it something global in window object
        if (window[name] !== undefined) {
          return resolve()
        }
        if (waited >= timeout * 1000) {
          return reject({ message: 'Timeout' })
        }
        wait(interval * 2)
      }, interval)
    }

    wait(30)
  })
}
  1. Add to componentDidMount of the template.
  componentDidMount() {
    waitForGlobal('MathJax').then(() => {
      top.MathJax.Hub.Config({
        tex2jax: {
          inlineMath: [['$', '$'], ['\\(', '\\)']],
          displayMath: [['$$', '$$'], ['[', ']']],
          processEscapes: true,
          processEnvironments: true,
          skipTags: ['script', 'noscript', 'style', 'textarea', 'pre'],
          TeX: {
            equationNumbers: { autoNumber: 'AMS' },
            extensions: ['AMSmath.js', 'AMSsymbols.js'],
          },
        },
      })
    })
    if (top.MathJax != null) {
      top.MathJax.Hub.Queue(['Typeset', top.MathJax.Hub])
    }
  }
  1. Add to componentDidUpdate of the template
  componentDidUpdate() {
    if (top.MathJax != null) {
      top.MathJax.Hub.Queue(['Typeset', top.MathJax.Hub])
    }
  }

from gatsby-remark-mathjax.

martintasker avatar martintasker commented on July 27, 2024 2

Thanks. Unfortunately that didn't work for me (I put it into my site layout page).

I wondered, did you consider trying mathjax-node, to do server-side rendering, which could be integrated into the plug-in?

from gatsby-remark-mathjax.

martintasker avatar martintasker commented on July 27, 2024 1

Hi I have had the same problem. Could you give some suggestions about good ways to "include Mathjax in your template"? I guess I could work it out by a long read of mathjax.org, but if you have anything ready to hand it would save time :-). Thanks!

from gatsby-remark-mathjax.

hanai avatar hanai commented on July 27, 2024

This is the desired result. This plugin is used for keeping mathjax equations as raw after markdown transform.

from gatsby-remark-mathjax.

hanai avatar hanai commented on July 27, 2024

https://www.mathjax.org/#gettingstarted

add this to html:

<script src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-MML-AM_CHTML' async></script>

from gatsby-remark-mathjax.

mreishus avatar mreishus commented on July 27, 2024

I can't get it to work either. After adding the gatsby-remark-mathjax plugin to the options:{plugins:[... section of my gatsby-transformer-remark entry in gatsby-config, and adding the MathJax script tag in my template file:

          <Helmet
            htmlAttributes={{ lang: 'en' }}
            meta={[{ name: 'description', content: siteDescription }]}
            title={`${post.frontmatter.title} | ${siteTitle}`}
         >
           <script
             type="text/javascript"
             src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML"
           />
         </Helmet>

I have no luck with $a^2 + b^2 = c^2$ in an md file.

I have a jekyll site that uses MathJax. I had to insert this in its html files:

<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
    tex2jax: {
      inlineMath: [['$','$'], ['\\(','\\)']],
      displayMath: [['$$','$$'], ['\[','\]']],
      processEscapes: true,
      processEnvironments: true,
      skipTags: ['script', 'noscript', 'style', 'textarea', 'pre'],
      TeX: { equationNumbers: { autoNumber: "AMS" },
           extensions: ["AMSmath.js", "AMSsymbols.js"] }
    }
  });
</script>

However, I can't figure out how to do that in gatsby. Could this be what I'm missing or is there an easier way?

from gatsby-remark-mathjax.

Related Issues (2)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.