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.
This commit is contained in:
Stefan Wallentowitz 2019-05-14 12:40:37 +02:00
parent 1c45b0a186
commit ac0bb5132b
6 changed files with 26 additions and 2 deletions

View File

@ -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
--------

View File

@ -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

View File

@ -0,0 +1 @@
exclude_patterns = ['_build']

Binary file not shown.

After

Width:  |  Height:  |  Size: 65 KiB

View File

@ -0,0 +1,9 @@
Test Figure in Admonition
=========================
.. caution::
This uses a figure in an admonition.
.. figure:: img.png

View File

@ -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()