Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e3a1bfb

Browse files
committed
Support using STIX fonts in math expressions. Use this functionality
to generate a png that used to be static. svn path=/trunk/matplotlib/; revision=5564
1 parent 55998c6 commit e3a1bfb

3 files changed

Lines changed: 52 additions & 22 deletions

File tree

doc/_static/stix_fonts.png

-4.81 KB
Binary file not shown.

doc/sphinxext/mathpng.py

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,35 @@
55
from md5 import md5
66

77
from docutils import nodes
8+
from docutils.parsers.rst import directives
89
from docutils.writers.html4css1 import HTMLTranslator
910
from sphinx.latexwriter import LaTeXTranslator
1011

1112
# Define LaTeX math node:
1213
class 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+
1521
def 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

2438
try:
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
3950
else:
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:
110123
def 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('/')

doc/users/mathtext.rst

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -147,22 +147,39 @@ Roman font, and the amplitude "A" is in calligraphy font.
147147

148148
The choices available with all fonts are:
149149

150-
=============== =================================
151-
Command Result
152-
=============== =================================
153-
``\mathrm`` :math:`\mathrm{Roman}`
154-
``\mathit`` :math:`\mathit{Italic}`
155-
``\mathtt`` :math:`\mathtt{Typewriter}`
156-
``\mathcal`` :math:`\mathcal{CALLIGRAPHY}`
157-
=============== =================================
150+
============================ ==================================
151+
Command Result
152+
============================ ==================================
153+
``\mathrm{Roman}`` :math:`\mathrm{Roman}`
154+
``\mathit{Italic}`` :math:`\mathit{Italic}`
155+
``\mathtt{Typewriter}`` :math:`\mathtt{Typewriter}`
156+
``\mathcal{CALLIGRAPHY}`` :math:`\mathcal{CALLIGRAPHY}`
157+
============================ ==================================
158+
159+
.. role:: math-stix(math)
160+
:fontset: stix
158161

159162
When using the STIX fonts, you also have the choice of:
160163

161-
.. image:: ../_static/stix_fonts.png
164+
====================================== =========================================
165+
Command Result
166+
====================================== =========================================
167+
``\mathbb{blackboard}`` :math-stix:`\mathbb{blackboard}`
168+
``\mathrm{\mathbb{blackboard}}`` :math-stix:`\mathrm{\mathbb{blackboard}}`
169+
``\mathfrak{Fraktur}`` :math-stix:`\mathfrak{Fraktur}`
170+
``\mathsf{sansserif}`` :math-stix:`\mathsf{sansserif}`
171+
``\mathrm{\mathsf{sansserif}}`` :math-stix:`\mathrm{\mathsf{sansserif}}`
172+
====================================== =========================================
173+
174+
.. htmlonly::
175+
176+
====================================== =========================================
177+
``\mathcircled{circled}`` :math-stix:`\mathcircled{circled}`
178+
====================================== =========================================
162179

163180
There are also three global "font sets" to choose from, which are
164181
selected using the ``mathtext.fontset`` parameter in
165-
::ref:`matplotlibrc <matplotlibrc-sample>`.
182+
:ref:`matplotlibrc <matplotlibrc-sample>`.
166183

167184
``cm``: **Computer Modern (TeX)**
168185

0 commit comments

Comments
 (0)