[PYTHON] Load macros like usepackage with sphinx + mathjax

You can use Use your own TeX macro with Sphinx + MathJax published on git.

However, like usepackage, you want to load it.

Create a directory called mathjaxlib because it can be anywhere that PYTHONPATH is in (this time ~ / lib / python is assumed). (This time, ~ / lib / python / mathjaxlib) (Hmm? Should I put it in the directory I want to adapt to? Ah ~ That's right ~)

Throw in ** mathjax.py ** published in git above here.

continue,

__init__.py


from pkgutil import extend_path
__path__ = extend_path(__path__, __name__)

When,

mathjax_preamble.py


def importstys(stys):
    """importsty for a list."""
    ret, delim  =  [], "%"*60
    for sty in stys :
        ret.append(delim+"\n%\n%   "+sty+"\n%\n"+delim)
        ret.append(importsty(sty))
    return "%\n".join(ret)

def importsty(sty):
    """import a refered sty file(.sty) by using kpsewhich. And this return the body in loaded sty as a string."""
    if sty.endswith(".sty") != True :
        sty += ".sty"
    import commands
    f = open( commands.getoutput("kpsewhich "+sty), 'rU' )
    tmp = f.read()
    f.close()
    return tmp

mathjax_preamble = r"""
"""

And throw it into ~ / lib / python / mathjaxlib /. If you have a total of 3 files, it's OK.

So, in conf.py of the page I made

conf.py


# MathJax
extensions.append( 'mathjaxlib.mathjax' )
mathjax_path = 'http://mathjax.connectmv.com/MathJax.js'
mathjax_path +='?config=default' # for mathjax v1.1
# TeX macros
from mathjaxlib.mathjax_preamble import mathjax_preamble, importstys
mathjax_preamble += importstys(["abbreb","matope"])

If you write something like that, k. "abbreb" and "matope" are abbreviations that I use, so please adjust to your own environment.

** Oh, of course, you have to pass kpsewhich! !! ** **

For a simple test.

conf.py


mathjax_preamble+="""\
\def\oo{\infty}
"""

Anyway

.. math:: \int_0^\oo \frac{\text{sin}(x)}{x} dx = \frac{\pi}{2}

Please give it a try.

Recommended Posts

Load macros like usepackage with sphinx + mathjax
LOAD DATA with PyMysql