diff options
Diffstat (limited to 'sphinx/writers')
-rw-r--r-- | sphinx/writers/latex.py | 214 |
1 files changed, 21 insertions, 193 deletions
diff --git a/sphinx/writers/latex.py b/sphinx/writers/latex.py index 616d786e3..16501472f 100644 --- a/sphinx/writers/latex.py +++ b/sphinx/writers/latex.py @@ -65,182 +65,6 @@ ENUMERATE_LIST_STYLE = defaultdict(lambda: r'\arabic', 'lowerroman': r'\roman', 'upperroman': r'\Roman', }) -PDFLATEX_DEFAULT_FONTPKG = r''' -\usepackage{times} -\expandafter\ifx\csname T@LGR\endcsname\relax -\else -% LGR was declared as font encoding - \substitutefont{LGR}{\rmdefault}{cmr} - \substitutefont{LGR}{\sfdefault}{cmss} - \substitutefont{LGR}{\ttdefault}{cmtt} -\fi -\expandafter\ifx\csname T@X2\endcsname\relax - \expandafter\ifx\csname T@T2A\endcsname\relax - \else - % T2A was declared as font encoding - \substitutefont{T2A}{\rmdefault}{cmr} - \substitutefont{T2A}{\sfdefault}{cmss} - \substitutefont{T2A}{\ttdefault}{cmtt} - \fi -\else -% X2 was declared as font encoding - \substitutefont{X2}{\rmdefault}{cmr} - \substitutefont{X2}{\sfdefault}{cmss} - \substitutefont{X2}{\ttdefault}{cmtt} -\fi -''' -XELATEX_DEFAULT_FONTPKG = r''' -\setmainfont{FreeSerif}[ - Extension = .otf, - UprightFont = *, - ItalicFont = *Italic, - BoldFont = *Bold, - BoldItalicFont = *BoldItalic -] -\setsansfont{FreeSans}[ - Extension = .otf, - UprightFont = *, - ItalicFont = *Oblique, - BoldFont = *Bold, - BoldItalicFont = *BoldOblique, -] -\setmonofont{FreeMono}[ - Extension = .otf, - UprightFont = *, - ItalicFont = *Oblique, - BoldFont = *Bold, - BoldItalicFont = *BoldOblique, -] -''' -XELATEX_GREEK_DEFAULT_FONTPKG = (XELATEX_DEFAULT_FONTPKG + - '\n\\newfontfamily\\greekfont{FreeSerif}' + - '\n\\newfontfamily\\greekfontsf{FreeSans}' + - '\n\\newfontfamily\\greekfonttt{FreeMono}') -LUALATEX_DEFAULT_FONTPKG = XELATEX_DEFAULT_FONTPKG - -DEFAULT_SETTINGS = { - 'latex_engine': 'pdflatex', - 'papersize': 'letterpaper', - 'pointsize': '10pt', - 'pxunit': '.75bp', - 'classoptions': '', - 'extraclassoptions': '', - 'maxlistdepth': '', - 'sphinxpkgoptions': '', - 'sphinxsetup': '', - 'fvset': '\\fvset{fontsize=\\small}', - 'passoptionstopackages': '', - 'geometry': '\\usepackage{geometry}', - 'inputenc': '', - 'utf8extra': '', - 'cmappkg': '\\usepackage{cmap}', - 'fontenc': '\\usepackage[T1]{fontenc}', - 'amsmath': '\\usepackage{amsmath,amssymb,amstext}', - 'multilingual': '', - 'babel': '\\usepackage{babel}', - 'polyglossia': '', - 'fontpkg': PDFLATEX_DEFAULT_FONTPKG, - 'substitutefont': '', - 'textcyrillic': '', - 'textgreek': '\\usepackage{textalpha}', - 'fncychap': '\\usepackage[Bjarne]{fncychap}', - 'hyperref': ('% Include hyperref last.\n' - '\\usepackage{hyperref}\n' - '% Fix anchor placement for figures with captions.\n' - '\\usepackage{hypcap}% it must be loaded after hyperref.\n' - '% Set up styles of URL: it should be placed after hyperref.\n' - '\\urlstyle{same}'), - 'contentsname': '', - 'extrapackages': '', - 'preamble': '', - 'title': '', - 'release': '', - 'author': '', - 'releasename': '', - 'makeindex': '\\makeindex', - 'shorthandoff': '', - 'maketitle': '\\sphinxmaketitle', - 'tableofcontents': '\\sphinxtableofcontents', - 'atendofbody': '', - 'printindex': '\\printindex', - 'transition': '\n\n\\bigskip\\hrule\\bigskip\n\n', - 'figure_align': 'htbp', - 'tocdepth': '', - 'secnumdepth': '', -} # type: Dict[str, Any] - -ADDITIONAL_SETTINGS = { - 'pdflatex': { - 'inputenc': '\\usepackage[utf8]{inputenc}', - 'utf8extra': ('\\ifdefined\\DeclareUnicodeCharacter\n' - '% support both utf8 and utf8x syntaxes\n' - ' \\ifdefined\\DeclareUnicodeCharacterAsOptional\n' - ' \\def\\sphinxDUC#1{\\DeclareUnicodeCharacter{"#1}}\n' - ' \\else\n' - ' \\let\\sphinxDUC\\DeclareUnicodeCharacter\n' - ' \\fi\n' - ' \\sphinxDUC{00A0}{\\nobreakspace}\n' - ' \\sphinxDUC{2500}{\\sphinxunichar{2500}}\n' - ' \\sphinxDUC{2502}{\\sphinxunichar{2502}}\n' - ' \\sphinxDUC{2514}{\\sphinxunichar{2514}}\n' - ' \\sphinxDUC{251C}{\\sphinxunichar{251C}}\n' - ' \\sphinxDUC{2572}{\\textbackslash}\n' - '\\fi'), - }, - 'xelatex': { - 'latex_engine': 'xelatex', - 'polyglossia': '\\usepackage{polyglossia}', - 'babel': '', - 'fontenc': ('\\usepackage{fontspec}\n' - '\\defaultfontfeatures[\\rmfamily,\\sffamily,\\ttfamily]{}'), - 'fontpkg': XELATEX_DEFAULT_FONTPKG, - 'textgreek': '', - 'utf8extra': ('\\catcode`^^^^00a0\\active\\protected\\def^^^^00a0' - '{\\leavevmode\\nobreak\\ }'), - }, - 'lualatex': { - 'latex_engine': 'lualatex', - 'polyglossia': '\\usepackage{polyglossia}', - 'babel': '', - 'fontenc': ('\\usepackage{fontspec}\n' - '\\defaultfontfeatures[\\rmfamily,\\sffamily,\\ttfamily]{}'), - 'fontpkg': LUALATEX_DEFAULT_FONTPKG, - 'textgreek': '', - 'utf8extra': ('\\catcode`^^^^00a0\\active\\protected\\def^^^^00a0' - '{\\leavevmode\\nobreak\\ }'), - }, - 'platex': { - 'latex_engine': 'platex', - 'babel': '', - 'classoptions': ',dvipdfmx', - 'fontpkg': '\\usepackage{times}', - 'textgreek': '', - 'fncychap': '', - 'geometry': '\\usepackage[dvipdfm]{geometry}', - }, - 'uplatex': { - 'latex_engine': 'uplatex', - 'babel': '', - 'classoptions': ',dvipdfmx', - 'fontpkg': '\\usepackage{times}', - 'textgreek': '', - 'fncychap': '', - 'geometry': '\\usepackage[dvipdfm]{geometry}', - }, - - # special settings for latex_engine + language_code - ('xelatex', 'fr'): { - # use babel instead of polyglossia by default - 'polyglossia': '', - 'babel': '\\usepackage{babel}', - }, - ('xelatex', 'zh'): { - 'fontenc': '\\usepackage{xeCJK}', - }, - ('xelatex', 'el'): { - 'fontpkg': XELATEX_GREEK_DEFAULT_FONTPKG, - }, -} # type: Dict[Any, Dict[str, Any]] EXTRA_RE = re.compile(r'^(.*\S)\s+\(([^()]*)\)\s*$') @@ -477,16 +301,18 @@ class LaTeXTranslator(SphinxTranslator): self.compact_list = 0 self.first_param = 0 + sphinxpkgoptions = [] + # sort out some elements self.elements = self.builder.context.copy() # but some have other interface in config file - self.elements['wrapperclass'] = self.format_docclass(self.settings.docclass) + self.elements['wrapperclass'] = self.format_docclass(document.get('docclass')) # we assume LaTeX class provides \chapter command except in case # of non-Japanese 'howto' case self.sectionnames = LATEXSECTIONNAMES[:] - if self.settings.docclass == 'howto': + if document.get('docclass') == 'howto': docclass = self.config.latex_docclass.get('howto', 'article') if docclass[0] == 'j': # Japanese class... pass @@ -520,15 +346,12 @@ class LaTeXTranslator(SphinxTranslator): self.numfig_secnum_depth = min(self.numfig_secnum_depth, len(LATEXSECTIONNAMES) - 1) # if passed key value is < 1 LaTeX will act as if 0; see sphinx.sty - self.elements['sphinxpkgoptions'] += \ - (',numfigreset=%s' % self.numfig_secnum_depth) + sphinxpkgoptions.append('numfigreset=%s' % self.numfig_secnum_depth) else: - self.elements['sphinxpkgoptions'] += ',nonumfigreset' - try: - if self.config.math_numfig: - self.elements['sphinxpkgoptions'] += ',mathnumfig' - except AttributeError: - pass + sphinxpkgoptions.append('nonumfigreset') + + if self.config.numfig and self.config.math_numfig: + sphinxpkgoptions.append('mathnumfig') if (self.config.language not in {None, 'en', 'ja'} and 'fncychap' not in self.config.latex_elements): @@ -591,7 +414,7 @@ class LaTeXTranslator(SphinxTranslator): # tocdepth = 1: show parts, chapters and sections # tocdepth = 2: show parts, chapters, sections and subsections # ... - tocdepth = self.document['tocdepth'] + self.top_sectionlevel - 2 + tocdepth = self.document.get('tocdepth', 999) + self.top_sectionlevel - 2 if len(self.sectionnames) < len(LATEXSECTIONNAMES) and \ self.top_sectionlevel > 0: tocdepth += 1 # because top_sectionlevel is shifted by -1 @@ -609,17 +432,15 @@ class LaTeXTranslator(SphinxTranslator): self.elements['secnumdepth'] = '\\setcounter{secnumdepth}{%d}' %\ minsecnumdepth - contentsname = self.settings.contentsname + contentsname = document.get('contentsname') if contentsname: self.elements['contentsname'] = self.babel_renewcommand('\\contentsname', contentsname) if self.elements['maxlistdepth']: - self.elements['sphinxpkgoptions'] += (',maxlistdepth=%s' % - self.elements['maxlistdepth']) - if self.elements['sphinxpkgoptions']: - self.elements['sphinxpkgoptions'] = ('[%s]' % - self.elements['sphinxpkgoptions']) + sphinxpkgoptions.append('maxlistdepth=%s' % self.elements['maxlistdepth']) + if sphinxpkgoptions: + self.elements['sphinxpkgoptions'] = '[,%s]' % ','.join(sphinxpkgoptions) if self.elements['sphinxsetup']: self.elements['sphinxsetup'] = ('\\sphinxsetup{%s}' % self.elements['sphinxsetup']) @@ -2313,11 +2134,18 @@ class LaTeXTranslator(SphinxTranslator): # Import old modules here for compatibility +from sphinx.builders.latex import constants # NOQA from sphinx.builders.latex.util import ExtBabel # NOQA deprecated_alias('sphinx.writers.latex', { + 'ADDITIONAL_SETTINGS': constants.ADDITIONAL_SETTINGS, + 'DEFAULT_SETTINGS': constants.DEFAULT_SETTINGS, + 'LUALATEX_DEFAULT_FONTPKG': constants.LUALATEX_DEFAULT_FONTPKG, + 'PDFLATEX_DEFAULT_FONTPKG': constants.PDFLATEX_DEFAULT_FONTPKG, + 'XELATEX_DEFAULT_FONTPKG': constants.XELATEX_DEFAULT_FONTPKG, + 'XELATEX_GREEK_DEFAULT_FONTPKG': constants.XELATEX_GREEK_DEFAULT_FONTPKG, 'ExtBabel': ExtBabel, }, RemovedInSphinx40Warning) |