55 from md5 import md5
66
77from docutils import nodes
8+ from docutils .parsers .rst import directives
89from docutils .writers .html4css1 import HTMLTranslator
910from sphinx .latexwriter import LaTeXTranslator
1011
1112# Define LaTeX math node:
1213class latex_math (nodes .General , nodes .Element ):
1314 pass
1415
16+ def fontset_choice (arg ):
17+ return directives .choice (arg , ['cm' , 'stix' , 'stixsans' ])
18+
19+ options_spec = {'fontset' : fontset_choice }
20+
1521def math_role (role , rawtext , text , lineno , inliner ,
1622 options = {}, content = []):
1723 i = rawtext .find ('`' )
1824 latex = rawtext [i + 1 :- 1 ]
1925 node = latex_math (rawtext )
2026 node ['latex' ] = latex
27+ node ['fontset' ] = options .get ('fontset' , 'cm' )
2128 return [node ], []
29+ math_role .options = options_spec
2230
31+ def math_directive_run (content , block_text , options ):
32+ latex = '' .join (content )
33+ node = latex_math (block_text )
34+ node ['latex' ] = latex
35+ node ['fontset' ] = options .get ('fontset' , 'cm' )
36+ return [node ]
2337
2438try :
2539 from docutils .parsers .rst import Directive
@@ -28,22 +42,19 @@ def math_role(role, rawtext, text, lineno, inliner,
2842 from docutils .parsers .rst .directives import _directives
2943 def math_directive (name , arguments , options , content , lineno ,
3044 content_offset , block_text , state , state_machine ):
31- latex = '' .join (content )
32- node = latex_math (block_text )
33- node ['latex' ] = latex
34- return [node ]
45+ return math_directive_run (content , block_text , options )
3546 math_directive .arguments = None
36- math_directive .options = {}
47+ math_directive .options = options_spec
3748 math_directive .content = 1
3849 _directives ['math' ] = math_directive
3950else :
4051 class math_directive (Directive ):
4152 has_content = True
53+ option_spec = options_spec
54+
4255 def run (self ):
43- latex = ' ' .join (self .content )
44- node = latex_math (self .block_text )
45- node ['latex' ] = latex
46- return [node ]
56+ return math_directive_run (self .content , self .block_text ,
57+ self .options )
4758 from docutils .parsers .rst import directives
4859 directives .register_directive ('math' , math_directive )
4960
@@ -100,11 +111,13 @@ def latex2png(latex, name):
100111
101112
102113# This uses mathtext to render the expression
103- def latex2png (latex , filename ):
114+ def latex2png (latex , filename , fontset = 'cm' ):
104115 if os .path .exists (filename ):
105116 return
117+ orig_fontset = rcParams ['mathtext.fontset' ]
118+ rcParams ['mathtext.fontset' ] = fontset
106119 mathtext_parser .to_png (filename , "$%s$" % latex , dpi = 120 )
107-
120+ rcParams [ 'mathtext.fontset' ] = orig_fontset
108121
109122# LaTeX to HTML translation stuff:
110123def latex2html (node , source ):
@@ -114,7 +127,7 @@ def latex2html(node, source):
114127 name = 'math-%s' % md5 (latex ).hexdigest ()[- 10 :]
115128 dest = '_static/%s.png' % name
116129 if not isfile (dest ):
117- latex2png (latex , dest )
130+ latex2png (latex , dest , node [ 'fontset' ] )
118131
119132 path = '_static'
120133 count = source .split ('/doc/' )[- 1 ].count ('/' )
0 commit comments