Skip to content

Commit

Permalink
sphinx.ext.mathbase: Refactor LaTeX output
Browse files Browse the repository at this point in the history
  • Loading branch information
tk0miya committed Apr 14, 2016
1 parent 39396a4 commit 3206ccb
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 23 deletions.
45 changes: 30 additions & 15 deletions sphinx/ext/mathbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,38 @@ class eqref(nodes.Inline, nodes.TextElement):


def wrap_displaymath(math, label, numbering):
parts = math.split('\n\n')
ret = []
for part in parts:
if not part.strip():
continue
ret.append('\\!\\begin{split}%s\\end{split}\\\\\n' % part)
if not ret:
def is_equation(part):
return part.strip()

if label is None:
labeldef = ''
else:
labeldef = r'\label{%s}' % label
numbering = True

parts = filter(is_equation, math.split('\n\n'))
equations = []
if len(parts) == 0:
return ''
if label is not None or numbering:
env_begin = r'\begin{align}'
if label is not None:
env_begin += r'\label{%s}' % label
env_end = r'\end{align}'
elif len(parts) == 1:
if numbering:
begin = r'\begin{equation}' + labeldef
end = r'\end{equation}'
else:
begin = r'\begin{equation*}' + labeldef
end = r'\end{equation*}'
equations.append('\\begin{split}%s\\end{split}\n' % parts[0])
else:
env_begin = r'\begin{align*}'
env_end = r'\end{align*}'
return '%s\\begin{aligned}\n%s\\end{aligned}%s' % (env_begin, ''.join(ret), env_end)
if numbering:
begin = r'\begin{align}%s\begin{aligned}' % labeldef
end = r'\end{aligned}\end{align}'
else:
begin = r'\begin{align*}%s\begin{aligned}' % labeldef
end = r'\end{aligned}\end{align*}'
for part in parts:
equations.append('\\!%s\\\\\n' % part)

This comment has been minimized.

Copy link
@jfbu

jfbu Apr 14, 2016

Contributor

rather than prepending each equation with \!, you could put a single \! in front of \begin{aligned}. I guess it does not matter... brief testing of LaTeX samples did not show result differences in output between the two ways.

This comment has been minimized.

Copy link
@tk0miya

tk0miya Apr 16, 2016

Author Member

Thanks, I moved it before \begin{aligned}!


return '%s\n%s%s' % (begin, ''.join(equations), end)


def math_role(role, rawtext, text, lineno, inliner, options={}, content=[]):
Expand Down
16 changes: 8 additions & 8 deletions tests/test_ext_math.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,21 +72,21 @@ def test_math_number_all_latex(app, status, warning):

content = (app.outdir / 'test.tex').text()
print content
macro = (r'\\begin{align\*}\\begin{aligned}\s*'
r'\\!\\begin{split}a\^2\+b\^2=c\^2\\end{split}\\\\\s*'
r'\\end{aligned}\\end{align\*}')
macro = (r'\\begin{equation\*}\s*'
r'\\begin{split}a\^2\+b\^2=c\^2\\end{split}\s*'
r'\\end{equation\*}')
assert re.search(macro, content, re.S)

macro = r'Inline \\\(E=mc\^2\\\)'
assert re.search(macro, content, re.S)

macro = (r'\\begin{align\*}\\begin{aligned}\s*'
r'\\!\\begin{split}e\^{i\\pi}\+1=0\\end{split}\\\\\s+'
r'\\end{aligned}\\end{align\*}')
macro = (r'\\begin{equation\*}\s*'
r'\\begin{split}e\^{i\\pi}\+1=0\\end{split}\s+'
r'\\end{equation\*}')
assert re.search(macro, content, re.S)

macro = (r'\\begin{align\*}\\begin{aligned}\s*'
r'\\!\\begin{split}S &= \\pi r\^2\\end{split}\\\\\s*'
r'\\!\\begin{split}V &= \\frac\{4}\{3} \\pi r\^3\\end{split}\\\\\s*'
r'\\!S &= \\pi r\^2\\\\\s*'
r'\\!V &= \\frac\{4}\{3} \\pi r\^3\\\\\s*'
r'\\end{aligned}\\end{align\*}')
assert re.search(macro, content, re.S)

0 comments on commit 3206ccb

Please sign in to comment.