Skip to content

Commit

Permalink
Close sphinx-doc#6239: latex: Support to build Chinese documents
Browse files Browse the repository at this point in the history
  • Loading branch information
tk0miya committed May 25, 2019
1 parent 6e795a0 commit 7aab14a
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 10 deletions.
2 changes: 2 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Incompatible changes
* #6230: The anchor of term in glossary directive is changed if it is consisted
by non-ASCII characters
* #4550: html: Centering tables by default using CSS
* #6239: latex: xelatex and xeCJK are used for Chinese documents by default

Deprecated
----------
Expand Down Expand Up @@ -105,6 +106,7 @@ Features added
full production rule
* #6373: autosectionlabel: Allow suppression of warnings
* coverage: Support a new ``coverage_ignore_pyobjects`` option
* #6239: latex: Support to build Chinese documents

Bugs fixed
----------
Expand Down
12 changes: 6 additions & 6 deletions sphinx/builders/latex/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,12 +180,10 @@ def init_context(self):
# Add special settings for latex_engine
self.context.update(ADDITIONAL_SETTINGS.get(self.config.latex_engine, {}))

# for xelatex+French, don't use polyglossia by default
if self.config.latex_engine == 'xelatex':
if self.config.language:
if self.config.language[:2] == 'fr':
self.context['polyglossia'] = ''
self.context['babel'] = r'\usepackage{babel}'
# Add special settings for (latex_engine, language_code)
if self.config.language:
key = (self.config.latex_engine, self.config.language[:2])
self.context.update(ADDITIONAL_SETTINGS.get(key, {}))

# Apply extension settings to context
self.context['packages'] = self.usepackages
Expand Down Expand Up @@ -441,6 +439,8 @@ def default_latex_engine(config):
""" Better default latex_engine settings for specific languages. """
if config.language == 'ja':
return 'platex'
elif (config.language or '').startswith('zh'):
return 'xelatex'
else:
return 'pdflatex'

Expand Down
8 changes: 5 additions & 3 deletions sphinx/builders/latex/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,13 @@ def language_name(self, language_code):
# polyglossia calls new orthography (Neue Rechtschreibung) as
# german (with new spelling option).
return 'german'
elif not language:
elif language:
return language
elif language_code.startswith('zh'):
return 'english' # fallback to english (behaves like supported)
else:
self.supported = False
return 'english' # fallback to english
else:
return language

def get_mainlanguage_options(self):
# type: () -> str
Expand Down
12 changes: 11 additions & 1 deletion sphinx/writers/latex.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,17 @@
'fncychap': '',
'geometry': '\\usepackage[dvipdfm]{geometry}',
},
} # type: Dict[str, Dict[str, Any]]

# special settings for latex_engine + country_code
('xelatex', 'fr'): {
# use babel instead of polyglossia by default
'polyglossia': '',
'babel': '\\usepackage{babel}',
},
('xelatex', 'zh'): {
'fontenc': '\\usepackage{xeCJK}',
},
} # type: Dict[Any, Dict[str, Any]]

EXTRA_RE = re.compile(r'^(.*\S)\s+\(([^()]*)\)\s*$')

Expand Down
10 changes: 10 additions & 0 deletions tests/test_build_latex.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,16 @@ def test_latex_basic(app, status, warning):
assert r'\renewcommand{\releasename}{}' in result


@pytest.mark.sphinx('latex', testroot='basic', confoverrides={'language': 'zh'})
def test_latex_additional_settings_for_language_code(app, status, warning):
app.builder.build_all()
result = (app.outdir / 'test.tex').text(encoding='utf8')
print(result)
print(status.getvalue())
print(warning.getvalue())
assert r'\usepackage{xeCJK}' in result


@pytest.mark.sphinx('latex', testroot='latex-title')
def test_latex_title_after_admonitions(app, status, warning):
app.builder.build_all()
Expand Down

0 comments on commit 7aab14a

Please sign in to comment.