Repository navigation
Description
A figure with a caption inside an exercise or solution directive, including the gated solution-start / solution-end form, breaks the LaTeX (PDF) build. The HTML build is unaffected.
! LaTeX Error: Not in outer par mode.
l.107 \begin{figure}[htbp]
In Jupyter Book projects this usually comes from a code-cell with mystnb: figure: caption: metadata inside a solution. QuantEcon has hit it in several lecture PRs (tracked in QuantEcon/meta#297) and has been working around it by deleting the captions.
Cause
Sphinx's LaTeX writer increments self.no_latex_floats whenever it opens an admonition (visit_admonition, _visit_named_admonition, visit_seealso), and visit_figure then writes \begin{figure}[H] instead of using figure_align, so the figure stays inside the box. sphinx-exercise opens the same sphinxadmonition environment by appending LaTeX.visit_admonition() directly and never touches the counter, so a figure inside an exercise or solution is written as a floating \begin{figure}[htbp] inside the box, which LaTeX rejects. The code on main is the same as in 1.2.1.
Minimal reproduction
Sphinx 7.4.7 and sphinx-exercise 1.2.1, with extensions = ["sphinx_exercise"] in conf.py and any image saved as plot.png:
Repro
=====
.. note::
.. figure:: plot.png
Caption inside a note
.. exercise:: An exercise
:label: ex-1
.. figure:: plot.png
Caption inside an exercise
.. solution:: ex-1
.. figure:: plot.png
Caption inside a solution
.. exercise:: Another exercise
:label: ex-2
Plot something else.
.. solution-start:: ex-2
.. figure:: plot.png
Caption inside a gated solution
.. solution-end::Running sphinx-build -b latex writes \begin{figure}[H] for the figure in the note but \begin{figure}[htbp] for the other three, and make in the LaTeX build directory stops at the first of them with the error above.
Proposed fix
Do what Sphinx does in the three LaTeX visit/depart pairs in sphinx_exercise/nodes.py:
--- sphinx_exercise/nodes.py
+++ sphinx_exercise/nodes.py
@@ -117,6 +117,8 @@
) # TODO: Check this resolves.
self.body.append(label)
self.body.append(LaTeX.visit_admonition())
+ # Let figures inside the box use [H], as Sphinx does for admonitions
+ self.no_latex_floats += 1
else:
self.body.append(self.starttag(node, "div", CLASS="admonition"))
self.body.append("\n")
@@ -125,6 +127,7 @@
def depart_exercise_node(self, node: Node) -> None:
if isinstance(self, LaTeXTranslator):
self.body.append(LaTeX.depart_admonition())
+ self.no_latex_floats -= 1
else:
self.body.append("</div>")
@@ -140,6 +143,8 @@
)
self.body.append(label)
self.body.append(LaTeX.visit_admonition())
+ # Let figures inside the box use [H], as Sphinx does for admonitions
+ self.no_latex_floats += 1
else:
self.body.append(self.starttag(node, "div", CLASS="admonition"))
self.body.append("\n")
@@ -148,6 +153,7 @@
def depart_exercise_enumerable_node(self, node: Node) -> None:
if isinstance(self, LaTeXTranslator):
self.body.append(LaTeX.depart_admonition())
+ self.no_latex_floats -= 1
else:
self.body.append("</div>")
self.body.append("\n")
@@ -168,6 +174,8 @@
)
self.body.append(label)
self.body.append(LaTeX.visit_admonition())
+ # Let figures inside the box use [H], as Sphinx does for admonitions
+ self.no_latex_floats += 1
else:
self.body.append(self.starttag(node, "div", CLASS="admonition"))
self.body.append("\n")
@@ -176,6 +184,7 @@
def depart_solution_node(self, node: Node) -> None:
if isinstance(self, LaTeXTranslator):
self.body.append(LaTeX.depart_admonition())
+ self.no_latex_floats -= 1
else:
self.body.append("</div>")
self.body.append("\n")With this patch, all four figures in the reproduction are written as [H] and the PDF builds.
sphinx-proof appears to follow the same pattern (latex_admonition_start in sphinx_proof/nodes.py, with no no_latex_floats bookkeeping), so a figure inside a theorem or proof box probably fails the same way.
Activity
Upgrade sphinx-exercise to 1.2.2 and restore figure captions in solut…