mathjax / MathJax-node

MathJax for Node
Apache License 2.0
615 stars 97 forks source link

`tex2jax` inline delimiter specifiers don't seem to work #367

Closed ghost closed 6 years ago

ghost commented 6 years ago

My understanding is that the math attribute of the object passed to typeset is expected to be of type String.

Here is a setup I have on the server

  MathJax.typeset({
    math: req.body.text,
    format: 'TeX',
    mml: false,
    svg: true
  }

And the appropriate config

MathJax.config({
  extensions: 'tex2jax.js',
    MathJax: {
        tex2jax: {
            inlineMath: [["$","$"], ["\\(","\\)"]]
        }
  }
})
MathJax.start()

req.body.text is a simple String, like, This is an equation $x=4$

What I am trying to do is get a string from an input form, send to the MathJax, typeset only the math deilimited by parameters in inlineMath and sent back. Unfortunately, everything in the string is being typeset, so I get that Italic effect familiar to anyone who has used LaTeX and forgotten the closing $.

Any suggestions?

pkra commented 6 years ago

This is expected behavior; this kind of configuration of tex2jax will be ignored as the input is passed without delimiters anyway.

You control inline vs block of TeX input via the format key in the options object you pass to typeset, https://github.com/mathjax/MathJax-node#typesetoptions-callback (TeX vs inline-TeX).

ghost commented 6 years ago

Makes sense, however I want to pass input with delimiters ($ signs). Does MathJax parse the string for delimiters, typeset only what's inside, and return the result? Or does it just typeset everything?

pkra commented 6 years ago

I want to pass input with delimiters ($ signs).

You have to strip them yourself or use something like mathjax-node-page to do it for you.

Does MathJax parse the string for delimiters, typeset only what's inside, and return the result? Or does it just typeset everything?

On the one hand, strictly speaking, MathJax does none of this. MathJax "just" coordinates the various components, e.g., the tex2jax pre-processor (to find math in the text of a page and move it to custom script tags), the TeX input process (to convert the TeX in the script tags into the internal format), and the output process (to create HTML or SVG from the internal format and inject it in the page).

On the other hand, mathjax-node is quite different to (core) MathJax (in particular, pretty different APIs).

ghost commented 6 years ago

I see, So whatever string is passed to MathJax node, irrespective of any configuration options, is treated as math to be typeset. If I want to send a mixed string, like,

"This is an equation $x = 10$" and get back This is an equation <svg ...

I need to parse the text myself and Only pass the math portion to the API. Sound right?

pkra commented 6 years ago

I need to parse the text myself and Only pass the math portion to the API. Sound right?

Correct. You can, of course, re-use MathJax's tex2jax.js for this (the version in mathjax-node-page might be useful).

ghost commented 6 years ago

Beautiful, thanks!

ghost commented 6 years ago

I apologize, but I looked at the module you me tioned, and just to clarify, it requires a proper html string?

Simple "hello $x = 4$" won't do?

pkra commented 6 years ago

Yes. If you just have a string (not html), then a regexp should be sufficient (which you can find in tex2jax module). It depends a lot on your strings, I suppose.