LoginSignup
4
3

More than 5 years have passed since last update.

sphinx+mathjaxでマクロをusepackageみたいに読み込む

Last updated at Posted at 2013-05-20

gitで公開されている Sphinx + MathJax で自作 TeX マクロを利用する を使えば良い。

だけど、usepackageみたいに、読み込みたいと思うものですよね。

PYTHONPATHが通っているところならどこでも(今回は ~/lib/python を想定)いいから、 mathjaxlib というディレクトリを作る。(今回は、 ~/lib/python/mathjaxlib
(ん?その適応したいディレクトリに入れればいいじゃないかって?あ〜ん〜そだね〜)

ここに上のgitで公開されている mathjax.py を放り込む。

続いて、

__init__.py
from pkgutil import extend_path
__path__ = extend_path(__path__, __name__)

と、

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"""
"""

を書いて、 ~/lib/python/mathjaxlib/ に放り込む。計3っつのファイルが入っていたら、OK。

っで、自分で作っているページのconf.pyに

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"])

とか書いておくとおk。"abbreb","matope"は私の使ってる短縮語集ですので、自分の環境に合わせてください。

あ、当然kpsewhichが通らないとダメですよ!!

簡単なテストなら。

conf.py
mathjax_preamble+="""\
\def\oo{\infty}
"""

とかしておいて、

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

っで、試してみてください。

4
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
3