Permalink
Browse files

Use pngmath to typeset Latex

  • Loading branch information...
certik committed Nov 13, 2013
1 parent dec0d58 commit 54e8cb18d9e266e731c80f3a0f0850b25c1063d1
Showing with 55 additions and 1 deletion.
  1. +2 −1 conf.py
  2. +53 −0 exts/math_dollar.py
View
@@ -17,6 +17,7 @@
# add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here.
#sys.path.insert(0, os.path.abspath('.'))
+sys.path.append(os.path.abspath('exts'))
# -- General configuration -----------------------------------------------------
@@ -25,7 +26,7 @@
# Add any Sphinx extension module names here, as strings. They can be extensions
# coming with Sphinx (named 'sphinx.ext.*') or your custom ones.
-extensions = []
+extensions = ['sphinx.ext.pngmath', "math_dollar"]
# Add any paths that contain templates here, relative to this directory.
templates_path = ['_templates']
View
@@ -0,0 +1,53 @@
+import re
+
+def process_dollars(app, docname, source):
+ r"""
+ Replace dollar signs with backticks.
+
+ More precisely, do a regular expression search. Replace a plain
+ dollar sign ($) by a backtick (`). Replace an escaped dollar sign
+ (\$) by a dollar sign ($). Don't change a dollar sign preceded or
+ followed by a backtick (`$ or $`), because of strings like
+ "``$HOME``". Don't make any changes on lines starting with
+ spaces, because those are indented and hence part of a block of
+ code or examples.
+
+ This also doesn't replaces dollar signs enclosed in curly braces,
+ to avoid nested math environments, such as ::
+
+ $f(n) = 0 \text{ if $n$ is prime}$
+
+ Thus the above line would get changed to
+
+ `f(n) = 0 \text{ if $n$ is prime}`
+ """
+ s = "\n".join(source)
+ if s.find("$") == -1:
+ return
+ # This searches for "$blah$" inside a pair of curly braces --
+ # don't change these, since they're probably coming from a nested
+ # math environment. So for each match, we replace it with a temporary
+ # string, and later on we substitute the original back.
+ global _data
+ _data = {}
+ def repl(matchobj):
+ global _data
+ s = matchobj.group(0)
+ t = "___XXX_REPL_%d___" % len(_data)
+ _data[t] = s
+ return t
+ s = re.sub(r"({[^{}$]*\$[^{}$]*\$[^{}]*})", repl, s)
+ # matches $...$
+ dollars = re.compile(r"(?<!\$)(?<!\\)\$([^\$]+?)\$")
+ # regular expression for \$
+ slashdollar = re.compile(r"\\\$")
+ s = dollars.sub(r":math:`\1`", s)
+ s = slashdollar.sub(r"$", s)
+ # change the original {...} things in:
+ for r in _data:
+ s = s.replace(r, _data[r])
+ # now save results in "source"
+ source[:] = [s]
+
+def setup(app):
+ app.connect("source-read", process_dollars)

0 comments on commit 54e8cb1

Please sign in to comment.