Close #1431: latex: Add alphanumeric enumerated list support

This commit is contained in:
Takeshi KOMIYA 2018-05-14 20:09:45 +09:00
parent 157619c4e3
commit 600c948ff9
3 changed files with 40 additions and 7 deletions

View File

@ -116,6 +116,7 @@ Features added
* #4785: napoleon: Add strings to translation file for localisation * #4785: napoleon: Add strings to translation file for localisation
* #4927: Display a warning when invalid values are passed to linenothreshold * #4927: Display a warning when invalid values are passed to linenothreshold
option of highlight directive option of highlight directive
* #1431: latex: Add alphanumeric enumerated list support
Bugs fixed Bugs fixed
---------- ----------

View File

@ -59,6 +59,14 @@ HYPERLINK_SUPPORT_NODES = (
nodes.table, nodes.table,
nodes.section, nodes.section,
) )
ENUMERATE_LIST_STYLE = defaultdict(lambda: r'\arabic',
{
'arabic': r'\arabic',
'loweralpha': r'\alph',
'upperalpha': r'\Alph',
'lowerroman': r'\roman',
'upperroman': r'\Roman',
}) # type: Dict[unicode, unicode]
DEFAULT_SETTINGS = { DEFAULT_SETTINGS = {
'latex_engine': 'pdflatex', 'latex_engine': 'pdflatex',
@ -1498,6 +1506,15 @@ class LaTeXTranslator(nodes.NodeVisitor):
def visit_enumerated_list(self, node): def visit_enumerated_list(self, node):
# type: (nodes.Node) -> None # type: (nodes.Node) -> None
def get_enumtype(node):
# type: (nodes.Node) -> unicode
enumtype = node.get('enumtype', 'arabic')
if 'alpha' in enumtype and 26 < node.get('start', 0) + len(node):
# fallback to arabic if alphabet counter overflows
enumtype = 'arabic'
return enumtype
def get_nested_level(node): def get_nested_level(node):
# type: (nodes.Node) -> int # type: (nodes.Node) -> int
if node is None: if node is None:
@ -1507,10 +1524,15 @@ class LaTeXTranslator(nodes.NodeVisitor):
else: else:
return get_nested_level(node.parent) return get_nested_level(node.parent)
enum = "enum%s" % toRoman(get_nested_level(node)).lower()
style = ENUMERATE_LIST_STYLE.get(get_enumtype(node))
self.body.append('\\begin{enumerate}\n') self.body.append('\\begin{enumerate}\n')
self.body.append('\\renewcommand{\\the%s}{%s{%s}}\n' % (enum, style, enum))
self.body.append('\\makeatletter\\renewcommand{\\p@%s}{%s\\the%s%s}\\makeatother\n' %
(enum, node['prefix'], enum, node['suffix']))
if 'start' in node: if 'start' in node:
enum_depth = "enum%s" % toRoman(get_nested_level(node)).lower() self.body.append('\\setcounter{%s}{%d}\n' % (enum, node['start'] - 1))
self.body.append('\\setcounter{%s}{%d}\n' % (enum_depth, node['start'] - 1))
if self.table: if self.table:
self.table.has_problematic = True self.table.has_problematic = True

View File

@ -1229,11 +1229,21 @@ def test_latex_nested_enumerated_list(app, status, warning):
app.builder.build_all() app.builder.build_all()
result = (app.outdir / 'test.tex').text(encoding='utf8') result = (app.outdir / 'test.tex').text(encoding='utf8')
assert r'\setcounter{enumi}{4}' in result assert ('\\renewcommand{\\theenumi}{\\arabic{enumi}}\n'
assert r'\setcounter{enumii}{3}' in result '\\makeatletter\\renewcommand{\\p@enumi}{\\theenumi.}\\makeatother\n'
assert r'\setcounter{enumiii}{9}' in result '\\setcounter{enumi}{4}\n' in result)
assert r'\setcounter{enumiv}{23}' in result assert ('\\renewcommand{\\theenumii}{\\alph{enumii}}\n'
assert r'\setcounter{enumii}{2}' in result '\\makeatletter\\renewcommand{\\p@enumii}{\\theenumii.}\\makeatother\n'
'\\setcounter{enumii}{3}\n' in result)
assert ('\\renewcommand{\\theenumiii}{\\arabic{enumiii}}\n'
'\\makeatletter\\renewcommand{\\p@enumiii}{\\theenumiii)}\\makeatother\n'
'\\setcounter{enumiii}{9}\n' in result)
assert ('\\renewcommand{\\theenumiv}{\\arabic{enumiv}}\n'
'\\makeatletter\\renewcommand{\\p@enumiv}{(\\theenumiv)}\\makeatother\n'
'\\setcounter{enumiv}{23}\n' in result)
assert ('\\renewcommand{\\theenumii}{\\roman{enumii}}\n'
'\\makeatletter\\renewcommand{\\p@enumii}{\\theenumii.}\\makeatother\n'
'\\setcounter{enumii}{2}\n' in result)
@pytest.mark.sphinx('latex', testroot='footnotes') @pytest.mark.sphinx('latex', testroot='footnotes')