Last active
March 8, 2022 20:43
-
-
Save koddsson/ddc512954b47172e79d6e23900fa640e to your computer and use it in GitHub Desktop.
Convert LaTeX to MathML in Ruby
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
ruby tex-to-mathml.rb "\( f(x) = \sum_{i=0}^{n} \frac{a_i}{1+x} \)" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
source 'https://rubygems.org' | |
gem 'itextomml' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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">∑</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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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