mirror of
https://github.com/sphinx-doc/sphinx.git
synced 2025-02-25 18:55:22 -06:00
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:
parent
1c45b0a186
commit
ac0bb5132b
1
CHANGES
1
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
|
||||
--------
|
||||
|
@ -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
|
||||
|
1
tests/roots/test-latex-figure-in-admonition/conf.py
Normal file
1
tests/roots/test-latex-figure-in-admonition/conf.py
Normal file
@ -0,0 +1 @@
|
||||
exclude_patterns = ['_build']
|
BIN
tests/roots/test-latex-figure-in-admonition/img.png
Normal file
BIN
tests/roots/test-latex-figure-in-admonition/img.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 65 KiB |
9
tests/roots/test-latex-figure-in-admonition/index.rst
Normal file
9
tests/roots/test-latex-figure-in-admonition/index.rst
Normal file
@ -0,0 +1,9 @@
|
||||
Test Figure in Admonition
|
||||
=========================
|
||||
|
||||
.. caution::
|
||||
|
||||
This uses a figure in an admonition.
|
||||
|
||||
.. figure:: img.png
|
||||
|
@ -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()
|
||||
|
Loading…
Reference in New Issue
Block a user