Coder Social home page Coder Social logo

Comments (7)

RonaldBecerra avatar RonaldBecerra commented on June 3, 2024 1

@pkra Thank you, it worked. I had to use the result.css in the style of the web page. In the code above, I changed the server part:

app.post('/example', async(req, res) => {
    let result = await returnMathJax(req.body.latexFormula);
    let head = "<head><style>" + result.css.toString() + "</style></head>";
    let body = "<body>" + result.html.toString() + "</body>";
    let mypage = "<html>"+ head + body + "</html>";
    res.send(mypage);
});

And as you said, I could avoid calculating result.css everytime and just use its predetermined result.

from mathjax-node.

pkra avatar pkra commented on June 3, 2024

The second screenshot indicates you want display mode but your configuration sets format: "inline-TeX". Try format: "TeX" instead (and check the readme for more information).

(Also: consider switching to MathJax v3 which works in nodejs directly, see https://github.com/mathjax/MathJax-demos-node for some examples.)

from mathjax-node.

RonaldBecerra avatar RonaldBecerra commented on June 3, 2024

@pkra The problem with MathJax v3 is that it does not allow the user to select (underline) portions of the rendered formula or even it as a whole, a feature that I need. I have only achieved that when the returned HTML code consists on <span> objects, that v2 gives.

(I hadn't put the backlash (\) in my original comment, so the <span> was not shown. Sorry for that).

With "mathjax-node" the returned code consists on <span> objects when I put html:true in the typeset, and then use result.html, but it does not render as desired. I already tried with format:"TeX", but I got:

image

from mathjax-node.

pkra avatar pkra commented on June 3, 2024

That image looks like the CSS is missing completely. Try posting a full self-contained output example somewhere people can access.

from mathjax-node.

RonaldBecerra avatar RonaldBecerra commented on June 3, 2024

@pkra Ok, I am working with two files. The first one is called "index.html". The user inputs a formula in there and sends it to a server. The code is:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width">
		<title>MathJax example V3</title>
		<script src="https://polyfill.io/v3/polyfill.min.js?features=es6"></script>
	</head>

	<body>
		<form action="http://localhost:90/example" method="POST">
			Formula: <input type="text" name="latexFormula"><br>
			<button type="submit">Send to backend</button>
		</form>
		<p>Click on the submit button, and the input will be sent to a page on the server.</p>
	</body>
</html>

The second one is called App.js, which has the server that runs "mathjax-node" and returns the HTML code on a different page. In need to have running it with the instruction "node App.js" in the console.

// ----------------- Express server -----------------
const express = require('express');
const bodyParser = require('body-parser');
const app = express();

app.use(bodyParser.urlencoded({ extended: true })); 

app.post('/example', async(req, res) => {
    let result = await returnMathJax(req.body.latexFormula);
    res.send(result.html);
});

const port = 90;

app.listen(port, () => {
    console.log(`Server running on port ${port}`);
});

// --------------- MathJax configuration -----------
var mjAPI = require("mathjax-node");
mjAPI.config({
    MathJax: {
        jax: ["input/TeX","output/HTML-CSS"],
        tex2jax: {
            inlineMath: [ ['$','$'], ["\\(","\\)"] ],
            displayMath: [ ['$$','$$'], ["\\[","\\]"] ],
            processEscapes: true,
            processEnvironments: true,
        },
        TeX: {extensions: ["AMSmath.js","AMSsymbols.js"]},

        "HTML-CSS": {
            linebreaks: { automatic: true },
            fonts: ["TeX"],
        }
    },
});
mjAPI.start();

// ---------------- Execute MathJax -----------------
async function returnMathJax(formula){  
    let result = await mjAPI.typeset({
        "format":"TeX", 
        "math": formula,
        "html": true, 
        "htmlNode": true,
        "css": true,
    });
    return result;
}

The formula I was using for tests is "\sum_{i=0}^ni>2"

from mathjax-node.

pkra avatar pkra commented on June 3, 2024

You never do anything with result.css so the necessary CSS is missing.

Since result.css is actually fixed for each mathjax version, you could just call it once during development and store its output in a regular stylesheet to load into the page.

from mathjax-node.

pkra avatar pkra commented on June 3, 2024

Glad to hear you could work it out. (FYI This might be interesting: mathjax/MathJax-src#734)

from mathjax-node.

Related Issues (20)

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.