Fixes #4315: latex_toplevel_sectioning induced \chapter in article class

This commit is contained in:
jfbu 2017-12-18 00:26:36 +01:00
parent 46b1896228
commit af46ed16b0

View File

@ -48,6 +48,8 @@ BEGIN_DOC = r'''
URI_SCHEMES = ('mailto:', 'http:', 'https:', 'ftp:') URI_SCHEMES = ('mailto:', 'http:', 'https:', 'ftp:')
SECNUMDEPTH = 3 SECNUMDEPTH = 3
LATEXSECTIONNAMES = ["part", "chapter", "section", "subsection",
"subsubsection", "paragraph", "subparagraph"]
DEFAULT_SETTINGS = { DEFAULT_SETTINGS = {
'latex_engine': 'pdflatex', 'latex_engine': 'pdflatex',
@ -501,8 +503,6 @@ def rstdim_to_latexdim(width_str):
class LaTeXTranslator(nodes.NodeVisitor): class LaTeXTranslator(nodes.NodeVisitor):
sectionnames = ["part", "chapter", "section", "subsection",
"subsubsection", "paragraph", "subparagraph"]
ignore_missing_images = False ignore_missing_images = False
@ -532,16 +532,6 @@ class LaTeXTranslator(nodes.NodeVisitor):
self.compact_list = 0 self.compact_list = 0
self.first_param = 0 self.first_param = 0
# determine top section level
if builder.config.latex_toplevel_sectioning:
self.top_sectionlevel = \
self.sectionnames.index(builder.config.latex_toplevel_sectioning)
else:
if document.settings.docclass == 'howto':
self.top_sectionlevel = 2
else:
self.top_sectionlevel = 1
# sort out some elements # sort out some elements
self.elements = DEFAULT_SETTINGS.copy() self.elements = DEFAULT_SETTINGS.copy()
self.elements.update(ADDITIONAL_SETTINGS.get(builder.config.latex_engine, {})) self.elements.update(ADDITIONAL_SETTINGS.get(builder.config.latex_engine, {}))
@ -564,11 +554,30 @@ class LaTeXTranslator(nodes.NodeVisitor):
}) })
if builder.config.latex_keep_old_macro_names: if builder.config.latex_keep_old_macro_names:
self.elements['sphinxpkgoptions'] = '' self.elements['sphinxpkgoptions'] = ''
# we assume LaTeX class provides \chapter command except in case
# of non-Japanese 'howto' case
self.sectionnames = LATEXSECTIONNAMES[:]
if document.settings.docclass == 'howto': if document.settings.docclass == 'howto':
docclass = builder.config.latex_docclass.get('howto', 'article') docclass = builder.config.latex_docclass.get('howto', 'article')
if docclass[0] == 'j': # Japanese class...
pass
else:
self.sectionnames.remove('chapter')
else: else:
docclass = builder.config.latex_docclass.get('manual', 'report') docclass = builder.config.latex_docclass.get('manual', 'report')
self.elements['docclass'] = docclass self.elements['docclass'] = docclass
# determine top section level
self.top_sectionlevel = 1
if builder.config.latex_toplevel_sectioning:
try:
self.top_sectionlevel = \
self.sectionnames.index(builder.config.latex_toplevel_sectioning)
except ValueError:
logger.warning('unknown %r toplevel_sectioning for class %r' %
(builder.config.latex_toplevel_sectioning, docclass))
if builder.config.today: if builder.config.today:
self.elements['date'] = builder.config.today self.elements['date'] = builder.config.today
else: else:
@ -631,21 +640,23 @@ class LaTeXTranslator(nodes.NodeVisitor):
usepackages = (declare_package(*p) for p in builder.usepackages) usepackages = (declare_package(*p) for p in builder.usepackages)
self.elements['usepackages'] += "\n".join(usepackages) self.elements['usepackages'] += "\n".join(usepackages)
if document.get('tocdepth'): if document.get('tocdepth'):
# redece tocdepth if `part` or `chapter` is used for top_sectionlevel # reduce tocdepth if `part` or `chapter` is used for top_sectionlevel
# tocdepth = -1: show only parts # tocdepth = -1: show only parts
# tocdepth = 0: show parts and chapters # tocdepth = 0: show parts and chapters
# tocdepth = 1: show parts, chapters and sections # tocdepth = 1: show parts, chapters and sections
# tocdepth = 2: show parts, chapters, sections and subsections # tocdepth = 2: show parts, chapters, sections and subsections
# ... # ...
tocdepth = document['tocdepth'] + self.top_sectionlevel - 2 tocdepth = document['tocdepth'] + self.top_sectionlevel - 2
maxdepth = len(self.sectionnames) - self.top_sectionlevel if len(self.sectionnames) < 7 and self.top_sectionlevel > 0:
if tocdepth > maxdepth: tocdepth += 1 # because top_sectionlevel is shifted by -1
if tocdepth > 5: # 5 corresponds to subparagraph
logger.warning('too large :maxdepth:, ignored.') logger.warning('too large :maxdepth:, ignored.')
tocdepth = maxdepth tocdepth = 5
self.elements['tocdepth'] = '\\setcounter{tocdepth}{%d}' % tocdepth self.elements['tocdepth'] = '\\setcounter{tocdepth}{%d}' % tocdepth
if tocdepth >= SECNUMDEPTH: if tocdepth >= SECNUMDEPTH:
# Increase secnumdepth if tocdepth is depther than default SECNUMDEPTH # Increase secnumdepth if tocdepth is deeper than default SECNUMDEPTH
self.elements['secnumdepth'] = '\\setcounter{secnumdepth}{%d}' % tocdepth self.elements['secnumdepth'] = '\\setcounter{secnumdepth}{%d}' % tocdepth
if getattr(document.settings, 'contentsname', None): if getattr(document.settings, 'contentsname', None):