From bb9cde4e322d3c68a0c5cdfd94895accd0449d7b Mon Sep 17 00:00:00 2001
From: Hong Xu
Date: Thu, 18 Feb 2016 18:46:45 -0800
Subject: [PATCH 1/2] 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.
---
sphinx/ext/mathjax.py | 25 ++++++++++++++-----------
tests/test_ext_math.py | 11 +++++++++++
2 files changed, 25 insertions(+), 11 deletions(-)
diff --git a/sphinx/ext/mathjax.py b/sphinx/ext/mathjax.py
index 511c4f3dd..325003d0c 100644
--- a/sphinx/ext/mathjax.py
+++ b/sphinx/ext/mathjax.py
@@ -35,21 +35,24 @@ def html_visit_displaymath(self, node):
self.body.append('')
raise nodes.SkipNode
+ # necessary to e.g. set the id property correctly
+ if node['number']:
+ self.body.append('(%s)' % node['number'])
+ self.body.append(self.builder.config.mathjax_display[0])
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):
part = self.encode(part)
- if i == 0:
- # necessary to e.g. set the id property correctly
- if node['number']:
- self.body.append('(%s)' %
- 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])
+ if r'\\' in part:
+ self.body.append(r'\begin{split}' + part + r'\end{split}')
else:
- self.body.append(self.builder.config.mathjax_display[0] + part +
- self.builder.config.mathjax_display[1])
+ self.body.append(part)
+ 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('\n')
raise nodes.SkipNode
diff --git a/tests/test_ext_math.py b/tests/test_ext_math.py
index d72da7934..be90dfe9c 100644
--- a/tests/test_ext_math.py
+++ b/tests/test_ext_math.py
@@ -43,6 +43,17 @@ def test_imgmath_svg(app, status, warning):
'\s*alt="a\^2\+b\^2=c\^2"/>\s*
\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'\s*'
+ r'\\\[ \\begin\{align\}\\begin\{aligned\}S \&= \\pi r\^2\\\\'
+ r'V \&= \\frac\{4\}\{3\} \\pi r\^3\\end\{aligned\}\\end\{align\} \\\]
')
+ assert re.search(html, content, re.S)
+
@with_app('html', testroot='ext-math',
confoverrides={'math_number_all': True,
'extensions': ['sphinx.ext.mathjax']})
From d019d3f9b8de7859fd26fcb77d09929faf9c180b Mon Sep 17 00:00:00 2001
From: Hong Xu
Date: Thu, 18 Feb 2016 18:50:37 -0800
Subject: [PATCH 2/2] Correct a duplicated test case name in test_ext_math
---
tests/test_ext_math.py | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/tests/test_ext_math.py b/tests/test_ext_math.py
index be90dfe9c..e0f8fb9e7 100644
--- a/tests/test_ext_math.py
+++ b/tests/test_ext_math.py
@@ -57,7 +57,7 @@ def test_mathjax_align(app, status, warning):
@with_app('html', testroot='ext-math',
confoverrides={'math_number_all': True,
'extensions': ['sphinx.ext.mathjax']})
-def test_math_number_all(app, status, warning):
+def test_math_number_all_mathjax(app, status, warning):
app.builder.build_all()
content = (app.outdir / 'index.html').text()
@@ -67,7 +67,7 @@ def test_math_number_all(app, status, warning):
@with_app('latex', testroot='ext-math',
confoverrides={'extensions': ['sphinx.ext.mathjax']})
-def test_math_number_all(app, status, warning):
+def test_math_number_all_latex(app, status, warning):
app.builder.build_all()
content = (app.outdir / 'test.tex').text()