summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStefan Wallentowitz <stefan@wallentowitz.de>2019-05-14 12:40:37 +0200
committerStefan Wallentowitz <stefan@wallentowitz.de>2019-05-24 16:07:13 +0200
commitac0bb5132b4c7ea19f518687e02243dc56dd1870 (patch)
tree286afc842c422dfb50c9be46ad227e33243f61f9
parent1c45b0a1860bb0223984fac9499bb77e65c64c85 (diff)
downloadsphinx-git-ac0bb5132b4c7ea19f518687e02243dc56dd1870.tar.gz
Fix latex figure in admonition
Using figures in an admonition produces a LaTeX error ("Not in outer par mode."). This is because it generates a float in a float. This can be trivially fixed by overwriting the alignment to H, which is also what is most probably intended.
-rw-r--r--CHANGES1
-rw-r--r--sphinx/writers/latex.py11
-rw-r--r--tests/roots/test-latex-figure-in-admonition/conf.py1
-rw-r--r--tests/roots/test-latex-figure-in-admonition/img.pngbin0 -> 66247 bytes
-rw-r--r--tests/roots/test-latex-figure-in-admonition/index.rst9
-rw-r--r--tests/test_build_latex.py6
6 files changed, 26 insertions, 2 deletions
diff --git a/CHANGES b/CHANGES
index 11398e9fb..9f6f6507a 100644
--- a/CHANGES
+++ b/CHANGES
@@ -18,6 +18,7 @@ Bugs fixed
* #6286: C++, allow 8 and 9 in hexadecimal integer literals.
* #6305: Fix the string in quickstart for 'path' argument of parser
+* LaTeX: Figures in admonitions produced errors
Testing
--------
diff --git a/sphinx/writers/latex.py b/sphinx/writers/latex.py
index c5be44b18..dfffeed5d 100644
--- a/sphinx/writers/latex.py
+++ b/sphinx/writers/latex.py
@@ -473,6 +473,7 @@ class LaTeXTranslator(SphinxTranslator):
self.in_term = 0
self.needs_linetrimming = 0
self.in_minipage = 0
+ self.no_latex_floats = 0
self.first_document = 1
self.this_is_the_title = 1
self.literal_whitespace = 0
@@ -1614,6 +1615,9 @@ class LaTeXTranslator(SphinxTranslator):
def visit_figure(self, node):
# type: (nodes.Element) -> None
+ align = self.elements['figure_align']
+ if self.no_latex_floats:
+ align = "H"
if self.table:
# TODO: support align option
if 'width' in node:
@@ -1639,8 +1643,7 @@ class LaTeXTranslator(SphinxTranslator):
self.body.append('\n\\begin{center}')
self.context.append('\\end{center}\n')
else:
- self.body.append('\n\\begin{figure}[%s]\n\\centering\n' %
- self.elements['figure_align'])
+ self.body.append('\n\\begin{figure}[%s]\n\\centering\n' % align)
if any(isinstance(child, nodes.caption) for child in node):
self.body.append('\\capstart\n')
self.context.append('\\end{figure}\n')
@@ -1680,20 +1683,24 @@ class LaTeXTranslator(SphinxTranslator):
def visit_admonition(self, node):
# type: (nodes.Element) -> None
self.body.append('\n\\begin{sphinxadmonition}{note}')
+ self.no_latex_floats += 1
def depart_admonition(self, node):
# type: (nodes.Element) -> None
self.body.append('\\end{sphinxadmonition}\n')
+ self.no_latex_floats -= 1
def _visit_named_admonition(self, node):
# type: (nodes.Element) -> None
label = admonitionlabels[node.tagname]
self.body.append('\n\\begin{sphinxadmonition}{%s}{%s:}' %
(node.tagname, label))
+ self.no_latex_floats += 1
def _depart_named_admonition(self, node):
# type: (nodes.Element) -> None
self.body.append('\\end{sphinxadmonition}\n')
+ self.no_latex_floats -= 1
visit_attention = _visit_named_admonition
depart_attention = _depart_named_admonition
diff --git a/tests/roots/test-latex-figure-in-admonition/conf.py b/tests/roots/test-latex-figure-in-admonition/conf.py
new file mode 100644
index 000000000..a45d22e28
--- /dev/null
+++ b/tests/roots/test-latex-figure-in-admonition/conf.py
@@ -0,0 +1 @@
+exclude_patterns = ['_build']
diff --git a/tests/roots/test-latex-figure-in-admonition/img.png b/tests/roots/test-latex-figure-in-admonition/img.png
new file mode 100644
index 000000000..a97e86d66
--- /dev/null
+++ b/tests/roots/test-latex-figure-in-admonition/img.png
Binary files differ
diff --git a/tests/roots/test-latex-figure-in-admonition/index.rst b/tests/roots/test-latex-figure-in-admonition/index.rst
new file mode 100644
index 000000000..e3d39d3ee
--- /dev/null
+++ b/tests/roots/test-latex-figure-in-admonition/index.rst
@@ -0,0 +1,9 @@
+Test Figure in Admonition
+=========================
+
+.. caution::
+
+ This uses a figure in an admonition.
+
+ .. figure:: img.png
+
diff --git a/tests/test_build_latex.py b/tests/test_build_latex.py
index f02394cf1..fbc033b76 100644
--- a/tests/test_build_latex.py
+++ b/tests/test_build_latex.py
@@ -1389,6 +1389,12 @@ def test_latex_labels(app, status, warning):
assert result.count(r'\label{\detokenize{index:section1}}') == 1
+@pytest.mark.sphinx('latex', testroot='latex-figure-in-admonition')
+def test_latex_figure_in_admonition(app, status, warning):
+ app.builder.build_all()
+ result = (app.outdir / 'python.tex').text(encoding='utf8')
+ assert(r'\begin{figure}[H]' in result)
+
def test_default_latex_documents():
from sphinx.util import texescape
texescape.init()