From 96e30b0d0e042975a410f6939c414a1bc631d657 Mon Sep 17 00:00:00 2001 From: jfbu Date: Fri, 3 Jun 2016 18:03:00 +0200 Subject: [PATCH] Fix #2593 (continued): allow figure captions in LaTeX tabulary tables --- sphinx/texinputs/sphinx.sty | 14 +++++++++----- sphinx/writers/latex.py | 6 ++++-- tests/test_build_latex.py | 2 +- 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/sphinx/texinputs/sphinx.sty b/sphinx/texinputs/sphinx.sty index f167c5c8dc..6ee0334a8b 100644 --- a/sphinx/texinputs/sphinx.sty +++ b/sphinx/texinputs/sphinx.sty @@ -837,9 +837,13 @@ \newcommand{\literalblockwithoutcaptionneedspace}{1.5\baselineskip} % figure in table -\newenvironment{figure-in-table}[1][\linewidth]{ +\newenvironment{figure-in-table}[1][\linewidth]{% \def\@captype{figure}% - \minipage{#1}% -}{\endminipage} -% store \caption to use it in longtable -\let\figcaption\caption + \begin{minipage}{#1}% +}{\end{minipage}} +% store original \caption macro for figures in longtable and tabulary +\let\sphinxcaption\caption +\newcommand*\figcaption + {\ifx\equation$%$% this is trick to identify tabulary first pass + \firstchoice@false\else\firstchoice@true\fi + \sphinxcaption } diff --git a/sphinx/writers/latex.py b/sphinx/writers/latex.py index 490565605e..b5b03bac0b 100644 --- a/sphinx/writers/latex.py +++ b/sphinx/writers/latex.py @@ -1437,9 +1437,11 @@ class LaTeXTranslator(nodes.NodeVisitor): # TODO: support align option if 'width' in node: length = width_to_latex_length(node['width']) - self.body.append('\\begin{figure-in-table}[%s]\n\\centering' % length) + self.body.append('\\begin{figure-in-table}[%s]\n\\centering\n' % length) else: - self.body.append('\\begin{figure-in-table}\n\\centering') + self.body.append('\\begin{figure-in-table}\n\\centering\n') + if any(isinstance(child, nodes.caption) for child in node): + self.body.append('\\capstart') self.context.append(ids + '\\end{figure-in-table}\n') elif node.get('align', '') in ('left', 'right'): if 'width' in node: diff --git a/tests/test_build_latex.py b/tests/test_build_latex.py index 07f178fc5f..ed404360cf 100644 --- a/tests/test_build_latex.py +++ b/tests/test_build_latex.py @@ -111,7 +111,7 @@ def test_writer(app, status, warning): app.builder.build_all() result = (app.outdir / 'SphinxTests.tex').text(encoding='utf8') - assert ('\\begin{figure-in-table}\n\\centering\n' + assert ('\\begin{figure-in-table}\n\\centering\n\\capstart\n' '\\includegraphics{{img}.png}\n' '\\figcaption{figure in table}\\label{markup:id7}\\end{figure-in-table}' in result)