diff --git a/sphinx/writers/latex.py b/sphinx/writers/latex.py index ce27dd07b..49e4b5101 100644 --- a/sphinx/writers/latex.py +++ b/sphinx/writers/latex.py @@ -25,6 +25,7 @@ from sphinx import highlighting from sphinx.errors import SphinxError from sphinx.locale import admonitionlabels, _ from sphinx.util import split_into +from sphinx.util.nodes import clean_astext from sphinx.util.osutil import ustrftime from sphinx.util.texescape import tex_escape_map, tex_replace_map from sphinx.util.smartypants import educate_quotes_latex @@ -717,11 +718,15 @@ class LaTeXTranslator(nodes.NodeVisitor): self.this_is_the_title = 0 raise nodes.SkipNode elif isinstance(parent, nodes.section): + short = '' + if node.traverse(nodes.image): + short = '[%s]' % ' '.join(clean_astext(node).split()).translate(tex_escape_map) + try: - self.body.append(r'\%s{' % self.sectionnames[self.sectionlevel]) + self.body.append(r'\%s%s{' % (self.sectionnames[self.sectionlevel], short)) except IndexError: # just use "subparagraph", it's not numbered anyway - self.body.append(r'\%s{' % self.sectionnames[-1]) + self.body.append(r'\%s%s{' % (self.sectionnames[-1], short)) self.context.append('}\n') self.restrict_footnote(node) diff --git a/tests/roots/test-image-in-section/conf.py b/tests/roots/test-image-in-section/conf.py new file mode 100644 index 000000000..255345e45 --- /dev/null +++ b/tests/roots/test-image-in-section/conf.py @@ -0,0 +1,11 @@ +# -*- coding: utf-8 -*- + +master_doc = 'index' + +rst_epilog = ''' +.. |picture| image:: pic.png + :width: 15pt + :height: 15pt + :alt: alternative_text +''' + diff --git a/tests/roots/test-image-in-section/index.rst b/tests/roots/test-image-in-section/index.rst new file mode 100644 index 000000000..a69db0a77 --- /dev/null +++ b/tests/roots/test-image-in-section/index.rst @@ -0,0 +1,18 @@ +test-image-in-section +===================== +this is dummy content + + +|picture| Test section +---------------------- +blah blah blah + + +Another section +--------------- +another blah + + +Other [blah] |picture| section +------------------------------ +other blah diff --git a/tests/roots/test-image-in-section/pic.png b/tests/roots/test-image-in-section/pic.png new file mode 100644 index 000000000..1081dc143 Binary files /dev/null and b/tests/roots/test-image-in-section/pic.png differ diff --git a/tests/test_build_latex.py b/tests/test_build_latex.py index e86a794fa..d233c12b8 100644 --- a/tests/test_build_latex.py +++ b/tests/test_build_latex.py @@ -423,6 +423,19 @@ def test_latex_show_urls_is_no(app, status, warning): '{sphinx-dev@googlegroups.com}\n' in result) +@with_app(buildername='latex', testroot='image-in-section') +def test_image_in_section(app, status, warning): + app.builder.build_all() + result = (app.outdir / 'Python.tex').text(encoding='utf8') + print(result) + print(status.getvalue()) + print(warning.getvalue()) + assert ('\chapter[Test section]{\includegraphics[width=15pt,height=15pt]{{pic}.png} Test section}' + in result) + assert ('\chapter[Other {[}blah{]} section]{Other {[}blah{]} \includegraphics[width=15pt,height=15pt]{{pic}.png} section}' in result) + assert ('\chapter{Another section}' in result) + + @with_app(buildername='latex', confoverrides={'latex_logo': 'notfound.jpg'}) def test_latex_logo_if_not_found(app, status, warning): try: