From 711b8ecf70b613c8decabc9ab0d1895169f4b89c Mon Sep 17 00:00:00 2001 From: jfbu Date: Sun, 11 Mar 2018 11:36:16 +0100 Subject: [PATCH 1/2] Fix vertical space before equation in latex (closes: #4574) --- sphinx/writers/latex.py | 4 ++++ tests/roots/test-latex-equations/conf.py | 5 +++++ .../roots/test-latex-equations/equations.rst | 21 +++++++++++++++++++ .../expects/latex-equations.tex | 13 ++++++++++++ tests/test_build_latex.py | 10 +++++++++ 5 files changed, 53 insertions(+) create mode 100644 tests/roots/test-latex-equations/conf.py create mode 100644 tests/roots/test-latex-equations/equations.rst create mode 100644 tests/roots/test-latex-equations/expects/latex-equations.tex diff --git a/sphinx/writers/latex.py b/sphinx/writers/latex.py index ca410c4e9..bdb2a92b3 100644 --- a/sphinx/writers/latex.py +++ b/sphinx/writers/latex.py @@ -1959,6 +1959,10 @@ class LaTeXTranslator(nodes.NodeVisitor): if id.startswith('index-'): return + # equations also need no extra blank line nor hypertarget + if id.startswith('equation-'): + return + # insert blank line, if the target follows a paragraph node index = node.parent.index(node) if index > 0 and isinstance(node.parent[index - 1], nodes.paragraph): diff --git a/tests/roots/test-latex-equations/conf.py b/tests/roots/test-latex-equations/conf.py new file mode 100644 index 000000000..6122e9212 --- /dev/null +++ b/tests/roots/test-latex-equations/conf.py @@ -0,0 +1,5 @@ +# -*- coding: utf-8 -*- + +master_doc = 'equations' +extensions = ['sphinx.ext.imgmath'] + diff --git a/tests/roots/test-latex-equations/equations.rst b/tests/roots/test-latex-equations/equations.rst new file mode 100644 index 000000000..2eef2f2a4 --- /dev/null +++ b/tests/roots/test-latex-equations/equations.rst @@ -0,0 +1,21 @@ +test-latex-equation +=================== + +Equation without a label. + +.. math:: + + E = mc^2 + +Equation with label. + +.. math:: E = hv + :label: test + +Second equation without label. + +.. math:: + + c^2 = a^2 + b^2 + +Equation with label :eq:`test` is important. diff --git a/tests/roots/test-latex-equations/expects/latex-equations.tex b/tests/roots/test-latex-equations/expects/latex-equations.tex new file mode 100644 index 000000000..ce07a0128 --- /dev/null +++ b/tests/roots/test-latex-equations/expects/latex-equations.tex @@ -0,0 +1,13 @@ +Equation without a label. +\begin{equation*} +\begin{split}E = mc^2\end{split} +\end{equation*} +Equation with label. +\begin{equation}\label{equation:equations:test} +\begin{split}E = hv\end{split} +\end{equation} +Second equation without label. +\begin{equation*} +\begin{split}c^2 = a^2 + b^2\end{split} +\end{equation*} +Equation with label \eqref{equation:equations:test} is important. diff --git a/tests/test_build_latex.py b/tests/test_build_latex.py index 4b02b7c54..602429a96 100644 --- a/tests/test_build_latex.py +++ b/tests/test_build_latex.py @@ -1134,6 +1134,16 @@ def test_latex_index(app, status, warning): assert '\n\\index{Einstein}\\index{relativity}\\ignorespaces \nand' in result +@pytest.mark.sphinx('latex', testroot='latex-equations') +def test_latex_equations(app, status, warning): + app.builder.build_all() + + result = (app.outdir / 'Python.tex').text(encoding='utf8') + expected = (app.srcdir / 'expects' / 'latex-equations.tex').text().strip() + + assert expected in result + + @pytest.mark.sphinx('latex', testroot='image-in-parsed-literal') def test_latex_image_in_parsed_literal(app, status, warning): app.builder.build_all() From c4e12ff01231d468682264b5d856aa78c39eeb19 Mon Sep 17 00:00:00 2001 From: jfbu Date: Mon, 12 Mar 2018 17:00:01 +0100 Subject: [PATCH 2/2] Add TODO notes modified: sphinx/ext/mathbase.py modified: sphinx/writers/latex.py --- sphinx/ext/mathbase.py | 1 + sphinx/writers/latex.py | 1 + 2 files changed, 2 insertions(+) diff --git a/sphinx/ext/mathbase.py b/sphinx/ext/mathbase.py index 15218963f..513ddd62a 100644 --- a/sphinx/ext/mathbase.py +++ b/sphinx/ext/mathbase.py @@ -86,6 +86,7 @@ class MathDomain(Domain): newnode['target'] = target return newnode else: + # TODO: perhaps use rather a sphinx-core provided prefix here? node_id = make_id('equation-%s' % target) if env.config.math_numfig and env.config.numfig: if docname in env.toc_fignumbers: diff --git a/sphinx/writers/latex.py b/sphinx/writers/latex.py index bdb2a92b3..1558a29d7 100644 --- a/sphinx/writers/latex.py +++ b/sphinx/writers/latex.py @@ -1960,6 +1960,7 @@ class LaTeXTranslator(nodes.NodeVisitor): return # equations also need no extra blank line nor hypertarget + # TODO: fix this dependency on mathbase extension internals if id.startswith('equation-'): return