mathjax / MathJax-node

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

LaTex Conversion failed #454

Closed CrazyChenzi closed 4 years ago

CrazyChenzi commented 4 years ago
var mjAPI = require("mathjax-node-svg2png");

mjAPI.config({
  displayMessages: false, // determines whether Message.Set() calls are logged
  displayErrors: true, // determines whether error messages are shown on the console
  undefinedCharError: false, // determines whether "unknown characters" (i.e., no glyph in the configured fonts) are saved in the error array
  extensions: "", // a convenience option to add MathJax extensions
  fontURL:
    "https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/fonts/HTML-CSS", // for webfont urls in the CSS for HTML output
  paths: {}, // configures custom path variables (e.g., for third party extensions, cf. test/config-third-party-extensions.js)
  MathJax: {
    extensions: ["tex2jax.js"],
    jax: ["input/TeX", "output/SVG"],
    tex2jax: {
      inlineMath: [
        // ['$', '$'],
        ["\\(", "\\)"],
      ],
      displayMath: [
        ['$$', '$$'],
        ["\\[", "\\]"]
      ],
      processEscapes: true
    },
    "SVG": {
      availableFonts: ["TeX"]
    }
  } // standard MathJax configuration options, see https://docs.mathjax.org for more detail.
});

mjAPI.start();

var math = "X\ =\ \sqrt{10}";

mjAPI.typeset(
  {
    math: math,
    format: "TeX",
    svg: true,
    png: false,
    scale: 1,
    useFontCache: false,
    html: true,
    linebreaks: true
  },
  function(data) {
    console.log("data:");
    console.log(data);
  }
);

I hope to get like this image

but....It is like this image

Is there a problem with my configuration?

dpvc commented 4 years ago

The problem is not with your configuration, but with your TeX string. In javascript stings, the backslash is a special character (used to get things like newlines as \n, tabs as \t and so on), so to get a literal backslash in the string, you need to use \\. So your TeX string should be

var math = "X\\ =\\ \\sqrt{10}";

(though it would be better as "X = \\sqrt{10}"; in general you should not modify the spacing, which is set up be carefully balanced).

dpvc commented 4 years ago

Also, you don't need the MathJax block in your configuration, as none of the values supplied there pertain to mathjax-node.

CrazyChenzi commented 4 years ago

thank you very much @dpvc