Math extension: support alignment of multiple equations for MathJAX.

This is a follow-up commit of #2254, which supported alignment of
multiple equations for imgmath and LaTeX output.
This commit is contained in:
Hong Xu 2016-02-18 18:46:45 -08:00
parent ae8cbec29a
commit bb9cde4e32
2 changed files with 25 additions and 11 deletions

View File

@ -35,21 +35,24 @@ def html_visit_displaymath(self, node):
self.body.append('</div>') self.body.append('</div>')
raise nodes.SkipNode raise nodes.SkipNode
# necessary to e.g. set the id property correctly
if node['number']:
self.body.append('<span class="eqno">(%s)</span>' % node['number'])
self.body.append(self.builder.config.mathjax_display[0])
parts = [prt for prt in node['latex'].split('\n\n') if prt.strip()] parts = [prt for prt in node['latex'].split('\n\n') if prt.strip()]
if len(parts) > 1: # Add alignment if there are more than 1 equation
self.body.append(r' \begin{align}\begin{aligned}')
for i, part in enumerate(parts): for i, part in enumerate(parts):
part = self.encode(part) part = self.encode(part)
if i == 0: if r'\\' in part:
# necessary to e.g. set the id property correctly self.body.append(r'\begin{split}' + part + r'\end{split}')
if node['number']:
self.body.append('<span class="eqno">(%s)</span>' %
node['number'])
if '&' in part or '\\\\' in part:
self.body.append(self.builder.config.mathjax_display[0] +
'\\begin{split}' + part + '\\end{split}' +
self.builder.config.mathjax_display[1])
else: else:
self.body.append(self.builder.config.mathjax_display[0] + part + self.body.append(part)
self.builder.config.mathjax_display[1]) if i < len(parts) - 1: # append new line if not the last equation
self.body.append(r'\\')
if len(parts) > 1: # Add alignment if there are more than 1 equation
self.body.append(r'\end{aligned}\end{align} ')
self.body.append(self.builder.config.mathjax_display[1])
self.body.append('</div>\n') self.body.append('</div>\n')
raise nodes.SkipNode raise nodes.SkipNode

View File

@ -43,6 +43,17 @@ def test_imgmath_svg(app, status, warning):
'\s*alt="a\^2\+b\^2=c\^2"/>\s*</p>\s*</div>') '\s*alt="a\^2\+b\^2=c\^2"/>\s*</p>\s*</div>')
assert re.search(html, content, re.S) assert re.search(html, content, re.S)
@with_app('html', testroot='ext-math',
confoverrides={'extensions': ['sphinx.ext.mathjax']})
def test_mathjax_align(app, status, warning):
app.builder.build_all()
content = (app.outdir / 'index.html').text()
html = (r'<div class="math">\s*'
r'\\\[ \\begin\{align\}\\begin\{aligned\}S \&amp;= \\pi r\^2\\\\'
r'V \&amp;= \\frac\{4\}\{3\} \\pi r\^3\\end\{aligned\}\\end\{align\} \\\]</div>')
assert re.search(html, content, re.S)
@with_app('html', testroot='ext-math', @with_app('html', testroot='ext-math',
confoverrides={'math_number_all': True, confoverrides={'math_number_all': True,
'extensions': ['sphinx.ext.mathjax']}) 'extensions': ['sphinx.ext.mathjax']})