LaTeX: Support math in section titles

Fix #1480
This commit is contained in:
Hong Xu 2015-12-19 19:24:16 -08:00
parent 9669f2a48b
commit eac142f8b8
2 changed files with 19 additions and 3 deletions

View File

@ -56,6 +56,17 @@ def eq_role(role, rawtext, text, lineno, inliner, options={}, content=[]):
return [node], [] return [node], []
def is_in_section_title(node):
"""Determine whether the node is in a section title"""
from sphinx.util.nodes import traverse_parent
for ancestor in traverse_parent(node):
if isinstance(ancestor, nodes.title) and \
isinstance(ancestor.parent, nodes.section):
return True
return False
class MathDirective(Directive): class MathDirective(Directive):
has_content = True has_content = True
@ -91,7 +102,12 @@ class MathDirective(Directive):
def latex_visit_math(self, node): def latex_visit_math(self, node):
self.body.append('\\(' + node['latex'] + '\\)') if is_in_section_title(node):
protect = r'\protect'
else:
protect = ''
equation = protect + r'\(' + node['latex'] + protect + r'\)'
self.body.append(equation)
raise nodes.SkipNode raise nodes.SkipNode

View File

@ -1,5 +1,5 @@
Test math extensions Test math extensions :math:`E = m c^2`
==================== ======================================
This is inline math: :math:`a^2 + b^2 = c^2`. This is inline math: :math:`a^2 + b^2 = c^2`.