Skip to content

Instantly share code, notes, and snippets.

@pkra
pkra / png-sv2png.js
Last active May 30, 2016 08:06
An example module for wrapping PNG generation around mathjax-node
var mjAPI = require("../lib/main.js");
var svg2png = require('svg2png');
function createPNG(result, callback){
var sourceBuffer = new Buffer(result.svg, "utf-8");
svg2png(sourceBuffer).then(function(buffer){
result.png = "data:image/png;base64," + buffer.toString('base64');
return callback(result);
})
};
@pkra
pkra / mj-node-xhtml-with-mml.js
Last active May 27, 2016 12:00
Quick MathJax-node wrapper for xhtml files containig MathML
'use strict';
var mjAPI = require('mathjax-node/lib/mj-single');
var typeset = mjAPI.typeset;
var fs = require('fs');
var libxmljs = require('libxmljs');
var argv = require('yargs')
.strict()
.usage('Usage: jats -i input.html -o output.html -f HTML', {
"use strict";
var mjAPI = require("./lib/main");
var fs = require('fs');
var inputFile = "file.txt"; // use xargs or something
var output = [];
var array = fs.readFileSync('file.txt').toString().split("\n");
mjAPI.start();
@pkra
pkra / sre.py
Last active February 28, 2016 09:10
SpeechRuleEngine in Python
# Usage: python sre.py mml.txt
import os
import sys
from execjs import get
runtime = get('Node')
context = runtime.compile('''
module.paths.push('%s');
var sre = require('speech-rule-engine');
sre.setupEngine({semantics: true, domain: "mathspeak", style: "brief"});
@pkra
pkra / mml2speech.js
Created February 26, 2016 16:35
Simple SRE snippet
// `$ npm install speech-rule-engine`
var sre = require('speech-rule-engine');
var mathml = '<math xmlns="http://www.w3.org/1998/Math/MathML"> <mfrac> <mn>1</mn> <mn>2</mn> </mfrac> </math>';
sre.setupEngine({semantics: true, domain: "mathspeak", style: "brief"});
console.log(sre.processExpression(mathml));
// one-half
var id_mathjax_plugin;
(function() {
var mjp = id_mathjax_plugin = {
load_mathjax: function() {
function e(e){var t='.MathJax .mn {background: inherit;} .MathJax .mi {color: inherit;} .MathJax .mo {background: inherit;}',n=e.createElement('style');n.innerText=t;try{n.textContent=t}catch(r){}e.getElementsByTagName('body')[0].appendChild(n);var i=e.createElement('script'),s;i.src='https://c328740.ssl.cf1.rackcdn.com/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML.js',i.type='text/javascript',s='MathJax.Hub.Config({skipStartupTypeset:true,tex2jax:{inlineMath:[[\'$\',\'$\']],displayMath:[[\'\\\\[\',\'\\\\]\']],processEscapes:true}});MathJax.Hub.Startup.onload();',window.opera?i.innerHTML=s:i.text=s,e.getElementsByTagName('head')[0].appendChild(i)}function t(t){t.MathJax===undefined?e(t.document):t.MathJax.Hub.Queue(new t.Array('Typeset',t.MathJax.Hub))}var n=document.getElementsByTagName('iframe'),r,i;t(window);for(r=0;r<n.length;r++)i=n[r].contentWindow||n[r].contentDocument,i.document||(i=i.parentN
@pkra
pkra / gist:5601181
Created May 17, 2013 18:57
MathJax bookmarklet for Springer LaTeX search http://latexsearch.com/
javascript:(function(){ var s=document.createElement('script'); s.setAttribute('src','http://code.jquery.com/jquery.js'); document.getElementsByTagName('body')[0].appendChild(s); $('.img_overflow > img').wrap('<span class="MathJax_Preview" />'); $('.MathJax_Preview').after(function() { tex = $(this).find('img').attr("title"); return " <script type='math/tex'>" + tex + "</" + "script>" ;}); var s=document.create
@pkra
pkra / WP2mj.js
Last active December 12, 2015 05:58
A bookmarklet, see https://en.wikipedia.org/wiki/Bookmarklet#Installation A fork of [this](https://gist.github.com/pkra/4574933) (in turn based on https://github.com/dzejkej/mathjax-bookmarklet and http://docs.mathjax.org/en/latest/dynamic.html). Modified for wordpress.com, trying to incorporate [Davide Cervone's answer](http://stackoverflow.com…
javascript:(function(){var images = document.getElementsByTagName('img'), count = 0; for (var i = images.length - 1; i >= 0; i--) { var img = images[i]; if (img.className === "latex") { var script = document.createElement("script"); script.type = "math/tex"; if (window.opera) {script.innerHTML = img.title} else {script.text = img.title} img.parentNode.replaceChild(script,img);count++; }};function a(a){var b=a.createElement('script'),c;b.src='https://c328740.ssl.cf1.rackcdn.com/mathjax/2.1-latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML.js',b.type='text/javascript',c='MathJax.Hub.Config({tex2jax:{inlineMath:[[\'$\',\'$\']],displayMath:[[\'\\\\[\',\'\\\\]\']],processEscapes:true}});MathJax.Hub.Startup.onload();',window.opera?b.innerHTML=c:b.text=c,a.getElementsByTagName('head')[0].appendChild(b)}function b(b){b.MathJax===undefined?a(b.document):b.MathJax.Hub.Queue(new b.Array('Typeset',b.MathJax.Hub))}var c=document.getElementsByTagName('iframe'),d,e;b(window);for(d=0;d<c.length;d++)e=c[d].contentWi
@pkra
pkra / accessing_mathjax_mathml.html
Last active December 11, 2015 08:39
A MathJax sample that explains how to programatically access the MathML generated by MathJax. There's currently no public API for it, so this will get easier in the future.
<!DOCTYPE html>
<html>
<!--via Davide Cervone on the MathJax User Group https://groups.google.com/d/msg/mathjax-users/unL8IjcrTto/DjHpH4BbPRcJ -->
<head>
<title>MathJax TeX to MathML Page</title>
<script>
function toMathML(jax,callback) {
var mml;
try {
mml = jax.root.toMathML("");
@pkra
pkra / pic2MJ.js
Last active December 11, 2015 02:48
A MathJax bookmarklet for experimentation. It replaces images with their title -- hoping it's LaTeX source -- and calls MathJax to render the TeX. Note: replaces all images! Based on https://github.com/dzejkej/mathjax-bookmarklet and http://docs.mathjax.org/en/latest/dynamic.html Seems to works on wordpress.com // jetpack-LaTeX-images
javascript:(function(){var images = document.getElementsByTagName('img'), count = 0; for (var i = images.length - 1; i >= 0; i--) { var img = images[i]; var script = document.createElement("script"); script.type = "math/tex"; if (window.opera) {script.innerHTML = img.title} else {script.text = img.title} img.parentNode.replaceChild(script,img);count++; };function a(a){var b=a.createElement('script'),c;b.src='https://c328740.ssl.cf1.rackcdn.com/mathjax/2.1-latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML.js',b.type='text/javascript',c='MathJax.Hub.Config({tex2jax:{inlineMath:[[\'$\',\'$\']],displayMath:[[\'\\\\[\',\'\\\\]\']],processEscapes:true}});MathJax.Hub.Startup.onload();',window.opera?b.innerHTML=c:b.text=c,a.getElementsByTagName('head')[0].appendChild(b)}function b(b){b.MathJax===undefined?a(b.document):b.MathJax.Hub.Queue(new b.Array('Typeset',b.MathJax.Hub))}var c=document.getElementsByTagName('iframe'),d,e;b(window);for(d=0;d<c.length;d++)e=c[d].contentWindow||c[d].contentDocument,e.document||(e=e