MultiMarkdown-6 icon indicating copy to clipboard operation
MultiMarkdown-6 copied to clipboard

Inline SVG math in HTML output.

Open brianhempel opened this issue 7 years ago • 5 comments

It'd be super great if MultiMarkdown had an option to produce inline SVG's for its math so one doesn't have to include a JS-based math renderer on one's page.

This can be accomplished with dvisvgm --no-fonts, which is included with several Tex distros. If it's helpful, below is a functioning Ruby script for replacing math spans from MultiMarkdown HTML with inline SVGs.

USAGE = 'Converts math in <span class="math">...</span> to inline SVG. Usage: $ ruby math_to_svg.rb file.html'

path = ARGV[0] || (STDERR.puts USAGE; exit 1)

`mkdir -p tmp`

new_html =
  File.read(path).gsub(/<span class="math">\\\(([\s\S]*?)\\\)<\/span>/) do |match_str|
    latex_str = $1.gsub("&lt;", "<").gsub("&gt;", ">")
    latex_doc = %Q|\\documentclass{standalone}\n\\usepackage{amsmath}\n\\begin{document}\n$#{latex_str}$\n\\end{document}|

    File.write("tmp/tmp.tex", latex_doc)

    if system("pdflatex -output-format=dvi -interaction=nonstopmode -output-directory=tmp tmp/tmp.tex")
      if system("dvisvgm --output=tmp/tmp.svg --no-fonts  tmp/tmp.dvi")
        File.read("tmp/tmp.svg")
      else
        match_str
      end
    else
      match_str
    end
  end

`rm -r tmp`

File.write(path, new_html)

brianhempel avatar Sep 27 '18 04:09 brianhempel

Thanks for the suggestion.

I'm not sure it's a good fit for building into MultiMarkdown, however.

MMD has no external requirements in order to do its job (converting plain text into another format.) (Not counting the option to include support for libcurl in order to download remote files for packaging into supported formats.) This is on purpose, and I don't want to change this.

This would require packaging/discovering/installing/whatever the dvisvgm program and any requirements in order to generate HTML. This inserts additional "moving parts" that make it hard for MMD to work reliably and consistently across different operating systems and hardware.

Additionally, it seems that program works by first creating a DVI/EPS/PDF file, and then converting that into SVG. It's not converting directly from LaTeX to SVG. So this creates additional issues.

A better alternative would be a suitable LaTeX -> MathML library. This has come up periodically, but when I last looked there was not a suitable library for my needs. This was probably 2 years ago, however. In fact, early versions of MMD used ASCIIMath instead of LaTeX for math, and used MathML directly. However, MathML wasn't well supported at the time. For a combination of reasons this made switching to raw LaTeX for math, and the use of MathJax for HTML presentation a better option.

If starting from scratch today, I might make different choices because of a better selection of tools. That said, the users I hear from seem to prefer LaTeX for math. So I may have accidentally made the best choice.

So, at this point:

  1. The best option would probably be a library that could convert LaTeX source into MathML directly.

  2. It would need to be usable as a C library in order to be reasonably likely to run across various OS/Hardware combinations. Pragmatically, it should be a project that was designed to be easily included as a library in other projects (e.g. a single .c and .h file would be ideal).

  3. Converting Math into a static image on an HTML page is not the direction I prefer to go -- it limits the ability to dynamically resize the page, copy/paste into other programs, etc.

  4. I have not found such a library, but am open to considering it if there is one out there.

  5. I have no desire to limit you from doing what works best for you. If you want to write something up about how to use your tool in order to share with others, I am happy to link to it. (Or I could include it in the MMD User's Guide if you prefer.)

Thoughts?

fletcher avatar Sep 27 '18 21:09 fletcher

Have you heard of mtex2MML? It's written in C and is used by the Asciidoctor project as a library.

averms avatar Oct 27 '18 21:10 averms

I'll take a look. If I remember correctly, itex2MML was not a good fit for some reason when I checked several years ago. I'll take a look at this project (a fork of the older one) and see if it fits better. A quick glance suggests it might.

fletcher avatar Oct 28 '18 00:10 fletcher

There is now a project to implement MathML in Chromium https://mathml.igalia.com/news/2019/02/12/launch-of-the-project/

aslakr avatar Mar 20 '19 14:03 aslakr

I'm not sure it's a good fit for building into MultiMarkdown, however.

I would agree with this. Plus it is super easy to make the svg yourself with https://github.com/pkra/mathjax-node-page, a command line app that runs mathjax on your own computer. It can output to svg or html. The html requires mathjax css and fonts but the svg output is completely free of dependencies on any mathjax stuff.

averms avatar Mar 25 '19 03:03 averms