Code Monkey home page Code Monkey logo

Comments (2)

justforuse avatar justforuse commented on June 3, 2024

Have you know how to do it? if yes, you can add a comment under this issue

from vue-mathjax.

PhilipJonasFranz avatar PhilipJonasFranz commented on June 3, 2024

I have somewhat figured this out. Using domToImage, it is possible to save the rendered formula into SVG, JPG or PNG:

Using the code-snippet below i was able to select the formula preview and save it as image:

<template>
    <div>
        <v-textarea v-model="formula" no-resize outlined name="formula-input" label="Latex Formula"></v-textarea>

        <vue-mathjax id="putty" :formula="getFormulaInput"></vue-mathjax>

        <v-btn color="primary" class="ma-5" @click="saveAsSVG">
            Save as SVG
       </v-btn>
    </div>
</template>

<style scoped>
a {
    text-decoration: none;
}
</style>

<script>
import { VueMathjax } from 'vue-mathjax'
import domtoimage from 'dom-to-image';

export default {
    name: "TMathjaxViewer",
    components: {
        'vue-mathjax': VueMathjax
    },
    data () {
        return {
            formula: 'x = {-b \\pm \\sqrt{b^2-4ac} \\over 2a}'
        }
    },
    computed: {
        getFormulaInput() {
            return '$$' + this.formula + '$$';
        }
    },
    methods: {
        getMathElement() {
            /* 
             * I dont know a better way to do this, but what we try to do here is the following:
             * We want to select the HTML element on the page that contains the rendered formula.
             * Our top-level reference to this element is the 'putty' element. From here we have
             * to select: 'MathJax_Display > MathJax-Element-1-Frame > nobr > MathJax-Span-1'.
             * This selection path is implemented below.
             */
            return document.getElementById("putty").children [1].children [0].children [0].children [0];
        },
        saveAsSVG() { 
            var element = this.getMathElement();

            domtoimage.toSvg(element)
            .then(function (dataUrl) {
                var img = new Image();
                img.src = dataUrl;

                var link = document.createElement("a");

                document.body.appendChild(link); // for Firefox

                link.setAttribute("href", img.src);
                link.setAttribute("download", "formula.svg");
                link.click();
            })
            .catch(function (error) {
                console.error('Something went wrong!', error);
            });
        }
    }
}
</script>

As a fully working example, you can inspect my modified version of mathjax-viewer.

from vue-mathjax.

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.