mirror of
https://github.com/sphinx-doc/sphinx.git
synced 2025-02-25 18:55:22 -06:00
Merge pull request #2230 from xuhdev/number-all-math
Add an option to number all displayed math
This commit is contained in:
commit
59a88e090c
@ -28,6 +28,13 @@ Keep in mind that when you put math markup in **Python docstrings** read by
|
|||||||
:mod:`autodoc <sphinx.ext.autodoc>`, you either have to double all backslashes,
|
:mod:`autodoc <sphinx.ext.autodoc>`, you either have to double all backslashes,
|
||||||
or use Python raw strings (``r"raw"``).
|
or use Python raw strings (``r"raw"``).
|
||||||
|
|
||||||
|
:mod:`.mathbase` provides the following config values:
|
||||||
|
|
||||||
|
.. confval:: math_number_all
|
||||||
|
|
||||||
|
Set this option to ``True`` if you want all displayed math to be numbered.
|
||||||
|
The default is ``False``.
|
||||||
|
|
||||||
:mod:`.mathbase` defines these new markup elements:
|
:mod:`.mathbase` defines these new markup elements:
|
||||||
|
|
||||||
.. rst:role:: math
|
.. rst:role:: math
|
||||||
|
@ -242,7 +242,8 @@ def html_visit_displaymath(self, node):
|
|||||||
if node['nowrap']:
|
if node['nowrap']:
|
||||||
latex = node['latex']
|
latex = node['latex']
|
||||||
else:
|
else:
|
||||||
latex = wrap_displaymath(node['latex'], None)
|
latex = wrap_displaymath(node['latex'], None,
|
||||||
|
self.builder.config.math_number_all)
|
||||||
try:
|
try:
|
||||||
fname, depth = render_math(self, latex)
|
fname, depth = render_math(self, latex)
|
||||||
except MathExtError as exc:
|
except MathExtError as exc:
|
||||||
|
@ -28,7 +28,7 @@ class eqref(nodes.Inline, nodes.TextElement):
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
def wrap_displaymath(math, label):
|
def wrap_displaymath(math, label, numbering):
|
||||||
parts = math.split('\n\n')
|
parts = math.split('\n\n')
|
||||||
ret = []
|
ret = []
|
||||||
for i, part in enumerate(parts):
|
for i, part in enumerate(parts):
|
||||||
@ -38,7 +38,9 @@ def wrap_displaymath(math, label):
|
|||||||
ret.append('\\begin{split}%s\\end{split}' % part +
|
ret.append('\\begin{split}%s\\end{split}' % part +
|
||||||
(label and '\\label{'+label+'}' or ''))
|
(label and '\\label{'+label+'}' or ''))
|
||||||
else:
|
else:
|
||||||
ret.append('\\begin{split}%s\\end{split}\\notag' % part)
|
ret.append(r'\begin{split}%s\end{split}' % part)
|
||||||
|
if not numbering:
|
||||||
|
ret.append(r'\notag')
|
||||||
if not ret:
|
if not ret:
|
||||||
return ''
|
return ''
|
||||||
return '\\begin{gather}\n' + '\\\\'.join(ret) + '\n\\end{gather}'
|
return '\\begin{gather}\n' + '\\\\'.join(ret) + '\n\\end{gather}'
|
||||||
@ -116,7 +118,8 @@ def latex_visit_displaymath(self, node):
|
|||||||
self.body.append(node['latex'])
|
self.body.append(node['latex'])
|
||||||
else:
|
else:
|
||||||
label = node['label'] and node['docname'] + '-' + node['label'] or None
|
label = node['label'] and node['docname'] + '-' + node['label'] or None
|
||||||
self.body.append(wrap_displaymath(node['latex'], label))
|
self.body.append(wrap_displaymath(node['latex'], label,
|
||||||
|
self.builder.config.math_number_all))
|
||||||
raise nodes.SkipNode
|
raise nodes.SkipNode
|
||||||
|
|
||||||
|
|
||||||
@ -194,10 +197,11 @@ def number_equations(app, doctree, docname):
|
|||||||
num = 0
|
num = 0
|
||||||
numbers = {}
|
numbers = {}
|
||||||
for node in doctree.traverse(displaymath):
|
for node in doctree.traverse(displaymath):
|
||||||
if node['label'] is not None:
|
if node['label'] is not None or app.config.math_number_all:
|
||||||
num += 1
|
num += 1
|
||||||
node['number'] = num
|
node['number'] = num
|
||||||
numbers[node['label']] = num
|
if node['label'] is not None:
|
||||||
|
numbers[node['label']] = num
|
||||||
else:
|
else:
|
||||||
node['number'] = None
|
node['number'] = None
|
||||||
for node in doctree.traverse(eqref):
|
for node in doctree.traverse(eqref):
|
||||||
@ -208,6 +212,7 @@ def number_equations(app, doctree, docname):
|
|||||||
|
|
||||||
|
|
||||||
def setup_math(app, htmlinlinevisitors, htmldisplayvisitors):
|
def setup_math(app, htmlinlinevisitors, htmldisplayvisitors):
|
||||||
|
app.add_config_value('math_number_all', False, 'html')
|
||||||
app.add_node(math, override=True,
|
app.add_node(math, override=True,
|
||||||
latex=(latex_visit_math, None),
|
latex=(latex_visit_math, None),
|
||||||
text=(text_visit_math, None),
|
text=(text_visit_math, None),
|
||||||
|
@ -213,7 +213,8 @@ def html_visit_displaymath(self, node):
|
|||||||
if node['nowrap']:
|
if node['nowrap']:
|
||||||
latex = node['latex']
|
latex = node['latex']
|
||||||
else:
|
else:
|
||||||
latex = wrap_displaymath(node['latex'], None)
|
latex = wrap_displaymath(node['latex'], None,
|
||||||
|
self.builder.config.math_number_all)
|
||||||
try:
|
try:
|
||||||
fname, depth = render_math(self, latex)
|
fname, depth = render_math(self, latex)
|
||||||
except MathExtError as exc:
|
except MathExtError as exc:
|
||||||
|
@ -1,6 +0,0 @@
|
|||||||
Test imgmath
|
|
||||||
============
|
|
||||||
|
|
||||||
.. math:: a^2+b^2=c^2
|
|
||||||
|
|
||||||
Inline :math:`E=mc^2`
|
|
@ -1,4 +1,3 @@
|
|||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
extensions = ['sphinx.ext.imgmath']
|
|
||||||
master_doc = 'index'
|
master_doc = 'index'
|
10
tests/roots/test-ext-math/index.rst
Normal file
10
tests/roots/test-ext-math/index.rst
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
Test Math
|
||||||
|
=========
|
||||||
|
|
||||||
|
.. math:: a^2+b^2=c^2
|
||||||
|
|
||||||
|
Inline :math:`E=mc^2`
|
||||||
|
|
||||||
|
Second math
|
||||||
|
|
||||||
|
.. math:: e^{i\pi}+1=0
|
@ -1,9 +1,9 @@
|
|||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
"""
|
"""
|
||||||
test_ext_imgmath
|
test_ext_math
|
||||||
~~~~~~~~~~~~~~~~
|
~~~~~~~~~~~~~
|
||||||
|
|
||||||
Test sphinx.ext.imgmath extension.
|
Test math extensions.
|
||||||
|
|
||||||
:copyright: Copyright 2007-2016 by the Sphinx team, see AUTHORS.
|
:copyright: Copyright 2007-2016 by the Sphinx team, see AUTHORS.
|
||||||
:license: BSD, see LICENSE for details.
|
:license: BSD, see LICENSE for details.
|
||||||
@ -14,7 +14,8 @@ import re
|
|||||||
from util import with_app, SkipTest
|
from util import with_app, SkipTest
|
||||||
|
|
||||||
|
|
||||||
@with_app('html', testroot='ext-imgmath')
|
@with_app('html', testroot='ext-math',
|
||||||
|
confoverrides = {'extensions': ['sphinx.ext.imgmath']})
|
||||||
def test_imgmath_png(app, status, warning):
|
def test_imgmath_png(app, status, warning):
|
||||||
app.builder.build_all()
|
app.builder.build_all()
|
||||||
if "LaTeX command 'latex' cannot be run" in warning.getvalue():
|
if "LaTeX command 'latex' cannot be run" in warning.getvalue():
|
||||||
@ -27,8 +28,9 @@ def test_imgmath_png(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-imgmath',
|
@with_app('html', testroot='ext-math',
|
||||||
confoverrides={'imgmath_image_format': 'svg'})
|
confoverrides={'extensions': ['sphinx.ext.imgmath'],
|
||||||
|
'imgmath_image_format': 'svg'})
|
||||||
def test_imgmath_svg(app, status, warning):
|
def test_imgmath_svg(app, status, warning):
|
||||||
app.builder.build_all()
|
app.builder.build_all()
|
||||||
if "LaTeX command 'latex' cannot be run" in warning.getvalue():
|
if "LaTeX command 'latex' cannot be run" in warning.getvalue():
|
||||||
@ -40,3 +42,14 @@ def test_imgmath_svg(app, status, warning):
|
|||||||
html = ('<div class="math">\s*<p>\s*<img src="_images/math/\w+.svg"'
|
html = ('<div class="math">\s*<p>\s*<img src="_images/math/\w+.svg"'
|
||||||
'\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={'math_number_all': True,
|
||||||
|
'extensions': ['sphinx.ext.mathjax']})
|
||||||
|
def test_math_number_all(app, status, warning):
|
||||||
|
app.builder.build_all()
|
||||||
|
|
||||||
|
content = (app.outdir / 'index.html').text()
|
||||||
|
html = (r'<div class="math">\s*'
|
||||||
|
r'<span class="eqno">\(1\)</span>\\\[a\^2\+b\^2=c\^2\\\]</div>')
|
||||||
|
assert re.search(html, content, re.S)
|
Loading…
Reference in New Issue
Block a user