Skip to content

Instantly share code, notes, and snippets.

@preshing
Created October 27, 2013 15:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save preshing/7183310 to your computer and use it in GitHub Desktop.
Save preshing/7183310 to your computer and use it in GitHub Desktop.
MathJax tags for Octopress
#--------------------------------------------------------------
# MathJax tags for Octopress
#
# Put this in the plugins folder.
# Use a single "inlinemath" tag for inline math.
# Use a pair of "math/endmath" tags for math as a standalone paragraph.
# The "math" tag takes an optional size argument.
# You also need to include MathJax in the page header, for example by adding
# <script type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
# to source/_includes/custom/head.html
#
# Examples:
#
# ... our approximation takes the form {% inlinemath 1 - e^{-X} %}, because ...
#
# {% math %}
# \frac{N-1}{N}\times\frac{N-2}{N}\times\dots\times\frac{N-(k-2)}{N}\times\frac{N-(k-1)}{N}
# {% endmath %}
#
# {% math 170% %}
# e^{\frac{-k(k-1)}{2N}}
# {% endmath %}
#--------------------------------------------------------------
module Jekyll
class MathBlock < Liquid::Block
def initialize(tag_name, params, tokens)
super
@size = params.strip
end
def render(context)
if @size == ''
"$$ #{super} $$"
else
"<div style=\"font-size:#{@size};\">$$ #{super} $$</div>"
end
end
end
class InlineMathTag < Liquid::Tag
def initialize(tag_name, params, tokens)
super
@text = params
end
def render(context)
"\\\\(#{@text}\\\\)"
end
end
end
Liquid::Template.register_tag('math', Jekyll::MathBlock)
Liquid::Template.register_tag('inlinemath', Jekyll::InlineMathTag)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment