MathJax icon indicating copy to clipboard operation
MathJax copied to clipboard

How do I set the unit to em when I output SVG

Open cuixiaorui opened this issue 7 years ago • 7 comments

Hello, I want to know how to set up the ex transform em when output SVG. Or the ex becomes px. I wonder if this is feasible? Sorry, I didn't find how to set it in the document.

cuixiaorui avatar Jun 04 '18 13:06 cuixiaorui

image

As is shown in I want to change the unit of generated SVG from ex to em or px

cuixiaorui avatar Jun 04 '18 13:06 cuixiaorui

Can you be more specific about the use-case you have in mind? Why do you need to use em's or px's rather than ex's?

dpvc avatar Jun 04 '18 16:06 dpvc

Thank you for your reply. My project has a special need to synchronize the doodles on different ends. Let's say I draw a line on the PC side that needs to be displayed in the same place on the ipad side Ex is the height of the character 'x'. There is no problem with that in itself. But the height and width of SVG rendered by a PC or ipad are different in different end environments. Because ex is a relative unit. So I wonder if I can set the unit of exported SVG to px instead of ex. Can it be realized?

cuixiaorui avatar Jun 05 '18 03:06 cuixiaorui

any updates?

spiderhands avatar Nov 17 '20 07:11 spiderhands

LibreOffice Draw does not handle "ex" unit very well. I'm having trouble importing SVG with "ex" unit. an option to use px or pt will be very good.

bibhas2 avatar May 16 '22 17:05 bibhas2

@cuixiaorui, @spiderhands, @bibhas2, here is a version 3 configuration that uses px rather than ex for the output:

MathJax = {
  startup: {
    ready() {
      MathJax.startup.defaultReady();
      const fixed = MathJax.startup.document.outputJax.fixed;
      MathJax.startup.document.outputJax.postFilters.add(({math, document, data}) => {
        const svg = data.querySelector('svg');
        const [x, y, w, h] = svg.getAttribute('viewBox').split(/ /);
        const em = document.outputJax.pxPerEm;
        svg.setAttribute('width', fixed(w / 1000 * em) + 'px');
        svg.setAttribute('height', fixed(h / 1000 * em) + 'px');
      });
    }
  }
}

Perhaps that will do the trick for you.

dpvc avatar May 16 '22 17:05 dpvc

Yeah, I ended up doing something very similar in my app. See the convertUnit(svg) method there.

bibhas2 avatar May 16 '22 18:05 bibhas2