hexo-theme-next icon indicating copy to clipboard operation
hexo-theme-next copied to clipboard

Provide more options for MathJax & KaTeX

Open pilgrimlyieu opened this issue 11 months ago • 8 comments

Issue Checklist

Expected behavior

Add detailed options for MathJax & KaTeX. For example adds some macros.(This way seems not pretty, it's just an example. Maybe there are some prettier ways)

math:
  # Default (false) will load mathjax / katex script on demand.
  # That is it only render those page which has `mathjax: true` in front-matter.
  # If you set it to true, it will load mathjax / katex script EVERY PAGE.
  every_page: false

  mathjax:
    enable: false
    # Available values: none | ams | all
    tags: none
    macros:
      "\\R": "{\\bf R}"

  katex:
    enable: false
    # See: https://github.com/KaTeX/KaTeX/tree/master/contrib/copy-tex
    copy_tex: false
    macros:
      "\\R": "{\\bf R}"

Actual behavior

Currently I can only switch on or off MathJax & KaTeX. I can not modify its detailed option.

math:
  # Default (false) will load mathjax / katex script on demand.
  # That is it only render those page which has `mathjax: true` in front-matter.
  # If you set it to true, it will load mathjax / katex script EVERY PAGE.
  every_page: false

  mathjax:
    enable: false
    # Available values: none | ams | all
    tags: none

  katex:
    enable: false
    # See: https://github.com/KaTeX/KaTeX/tree/master/contrib/copy-tex
    copy_tex: false

For example, if I would like to change delimiters(I use \(...\) & \[...\] instead of $...$ & $$...$$) or add some custom macros, I will fail.

Steps to reproduce the behavior

Regardless of it

Other Information

Maybe these websites would help.

I've searched the Internet, read lots of issues, tried many plugins and modified some files while no one satisfied my requests.

I don't know how to achieve it since I didn't have relevant knowledge(like JavaScript) and I failed again and again when I tried.

It'll be appreciated if someone can help me. Thanks in advance

pilgrimlyieu avatar Jul 23 '23 07:07 pilgrimlyieu

Thanks for opening this issue, maintainers will get back to you as soon as possible!

welcome[bot] avatar Jul 23 '23 07:07 welcome[bot]

Delimiters like \(...\) & \[...\] are supported by default: https://docs.mathjax.org/en/latest/input/tex/delimiters.html You can check if there are other reasons causing the separator not to work. As for the issue with adding macros, I will investigate if there is a convenient solution, or you can edit this file yourself: https://github.com/next-theme/hexo-theme-next/blob/master/source/js/third-party/math/mathjax.js

stevenjoezhang avatar Jul 25 '23 08:07 stevenjoezhang

Delimiters like \(...\) & \[...\] are supported by default: docs.mathjax.org/en/latest/input/tex/delimiters.html  You can check if there are other reasons causing the separator not to work. As for the issue with adding macros, I will investigate if there is a convenient solution, or you can edit this file yourself: master/source/js/third-party/math/mathjax.js

What about KaTeX? I mainly use KaTeX. Is there a way to achieve it?

pilgrimlyieu avatar Jul 25 '23 08:07 pilgrimlyieu

Delimiters like \(...\) & \[...\] are supported by default: docs.mathjax.org/en/latest/input/tex/delimiters.html  You can check if there are other reasons causing the separator not to work. As for the issue with adding macros, I will investigate if there is a convenient solution, or you can edit this file yourself: master/source/js/third-party/math/mathjax.js

Full options support is important in my opinion. Hope there's a way to do it.

Additionally, I think it's inconvenient to write KaTeX & MathJax configs with YAML(specifically dictionary) and it's cumbersome to put every option into one config file.

Specifically speaking, I mean that we can modify KaTeX & MathJax settings in other config file, or JavaScript file.

pilgrimlyieu avatar Jul 25 '23 14:07 pilgrimlyieu

Delimiters like \(...\) & \[...\] are supported by default: docs.mathjax.org/en/latest/input/tex/delimiters.html  You can check if there are other reasons causing the separator not to work. As for the issue with adding macros, I will investigate if there is a convenient solution, or you can edit this file yourself: master/source/js/third-party/math/mathjax.js

Hey, is it(Custom Configuration) possible for KaTeX? I'm still searching for solution but there's no new messages from this thread. Thanks!

pilgrimlyieu avatar Aug 02 '23 11:08 pilgrimlyieu

Delimiters like \(...\) & \[...\] are supported by default: docs.mathjax.org/en/latest/input/tex/delimiters.html  You can check if there are other reasons causing the separator not to work. As for the issue with adding macros, I will investigate if there is a convenient solution, or you can edit this file yourself: master/source/js/third-party/math/mathjax.js

I've tried to use MathJax under the guide of the doc. However I should use \\\( ... \\\) as InlineMath delimeter instead of simple \( ... \). Besides, the TeX commands in \\\( ... \\\) cann't be rendered properly unless I double the backslash of every command like \\\( \\mathrm{e} \\\). This case would not be replicated if I use $ ... $ as InlineMath delimeters. Is it a bug?

Self Complaint

I turned to brackets delimeters from dollar delimeters because of many reliable and credible reasons, then I met so many issues(not only this one).

pilgrimlyieu avatar Aug 05 '23 02:08 pilgrimlyieu

Well, I kept KaTeX and found a solution to add macros. I followed the doc and installed hexo-markdown-it and @renbaoshuo/markdown-it-katex. Then I opened index.js and added & modified the following code.

*const custom_options = {
*    strict: "ignore",
*    macros: {
*    "\\d": "\\mathrm{d}"
*    }
*}
...
*      return katex.renderToString(latex, { ...options, displayMode, ...custom_options });
...

Now I can custom most of settings of KaTeX except delimeters, which means I, exhausted physically and mentally, should make a compromise and adapt to dollar delimeters.

And I found copy-tex cann't take effect. So I downloaded copy-tex.min.js and moved it to source\js\third-party\math and added a line to layout\_third-party\math\katex.njk

<script src="/js/third-party/math/copy-tex.min.js"></script>

Then it worked.

Final question How can I enable mhchem? I did the same to mhchem.min.js but it didn't work.

pilgrimlyieu avatar Aug 05 '23 10:08 pilgrimlyieu

Final question How can I enable mhchem? I did the same to mhchem.min.js but it didn't work.

Find solution from

  • waylonflinn/markdown-it-katex#38

In detail, add the starred line to index.js.

 const katex = require('katex');
*require('katex/contrib/mhchem');
 const inline = require('./lib/inline');
 const block = require('./lib/block');

pilgrimlyieu avatar Aug 05 '23 10:08 pilgrimlyieu