Skip to content

Instantly share code, notes, and snippets.

@koddsson
Last active March 8, 2022 20:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save koddsson/ddc512954b47172e79d6e23900fa640e to your computer and use it in GitHub Desktop.
Save koddsson/ddc512954b47172e79d6e23900fa640e to your computer and use it in GitHub Desktop.
Convert LaTeX to MathML in Ruby
#!/bin/bash
ruby tex-to-mathml.rb "\( f(x) = \sum_{i=0}^{n} \frac{a_i}{1+x} \)"
source 'https://rubygems.org'
gem 'itextomml'
<!DOCTYPE html>
<html>
<head>
<title>LaTeX to MathML example</title>
</head>
<body>
<math xmlns='http://www.w3.org/1998/Math/MathML' display='inline'><semantics><mrow><mi>f</mi><mo stretchy="false">(</mo><mi>x</mi><mo stretchy="false">)</mo><mo>=</mo><msubsup><mo lspace="0.16667em" rspace="0.16667em">&Sum;</mo> <mrow><mi>i</mi><mo>=</mo><mn>0</mn></mrow> <mi>n</mi></msubsup><mfrac><mrow><msub><mi>a</mi> <mi>i</mi></msub></mrow><mrow><mn>1</mn><mo>+</mo><mi>x</mi></mrow></mfrac></mrow><annotation encoding='application/x-tex'> f(x) = \sum_{i=0}^{n} \frac{a_i}{1+x} </annotation></semantics></math>
</body>
</html>
require 'rubygems'
require 'itextomml'
tex = ARGV[0].dup
mathML = Itex2MML::Parser.new.send(:filter, tex)
html = %{
<!DOCTYPE html>
<html>
<head>
<title>LaTeX to MathML example</title>
</head>
<body>
#{mathML}
</body>
</html>
}
puts html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment