Repository: sphinx-doc/sphinx

Issue:
Autonumbered list can not be configured to be alphanumeric in LaTeX since Sphinx 1.8.0
**Describe the bug**

Since #5001 was merged to fix #1431, Sphinx supports alphanumeric list items (a,b,c instead of 1,2,3) when this is explicitly present in the RST source.

If automatic numbering is used (with `#.`), lists are automatically numbered with 1,2,3.

Before 1.8.0 it was possible to change the automatic numbering using `enumitem` package. This is no longer possible because each `enumerate` environment defines its own `\theenumi`, `\theenumii` etc.

**To Reproduce**

A piece of RST:

```rst
This is a list with explicit numbers and letters

1.  Explicit number

    a. Explicit letter
    b. Explicit letter

2.  Explicit number

This is a list with implicit numbers and letters

#.  Implicit number

    #. Implicit number or letter?
    #. Implicit number or letter?

#.  Implicit number
```

With the following in the LaTeX preamble:

```latex
\usepackage{enumitem}
\setlist[enumerate,1]{label=\arabic*.}
\setlist[enumerate,2]{label=\alph*.}
```

Produces the following on Sphinx 1.8.0+:

<img width="241" alt="Screen Shot 2019-06-20 at 09 34 15" src="https://user-images.githubusercontent.com/3284111/59835867-cd230c80-934a-11e9-98b5-499916dbc6ca.png">

**Expected behavior**

On Sphinx 1.7.9 and lower, the result respects the numbering type set by `enumitems`:

![Screen Shot 2019-06-20 at 09 58 07](https://user-images.githubusercontent.com/3284111/59835929-e88e1780-934a-11e9-8f77-a7cc82ce64af.png)

Because `\theenumi` etc is set by each `enumerate`, this is hard to work around by hooking into an environment in the preamble.

It would be great if this could be styled using a package like `enumitem`, but a configuration option in Sphinx is also possible.

Discussion:
codecov[bot]: # [Codecov](https://codecov.io/gh/sphinx-doc/sphinx/pull/6533?src=pr&el=h1) Report
> Merging [#6533](https://codecov.io/gh/sphinx-doc/sphinx/pull/6533?src=pr&el=desc) into [2.1.3](https://codecov.io/gh/sphinx-doc/sphinx/commit/f443fb5579d64340b87c595e9cd87895bbc90f65?src=pr&el=desc) will **decrease** coverage by `<.01%`.
> The diff coverage is `100%`.

[![Impacted file tree graph](https://codecov.io/gh/sphinx-doc/sphinx/pull/6533/graphs/tree.svg?width=650&token=VFlFSELbkw&height=150&src=pr)](https://codecov.io/gh/sphinx-doc/sphinx/pull/6533?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##            2.1.3    #6533      +/-   ##
==========================================
- Coverage   83.68%   83.68%   -0.01%     
==========================================
  Files         278      278              
  Lines       40446    40444       -2     
  Branches     5965     5965              
==========================================
- Hits        33847    33845       -2     
  Misses       5277     5277              
  Partials     1322     1322
```


| [Impacted Files](https://codecov.io/gh/sphinx-doc/sphinx/pull/6533?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [tests/test\_build\_latex.py](https://codecov.io/gh/sphinx-doc/sphinx/pull/6533/diff?src=pr&el=tree#diff-dGVzdHMvdGVzdF9idWlsZF9sYXRleC5weQ==) | `95.33% <100%> (ø)` | :arrow_up: |
| [sphinx/writers/latex.py](https://codecov.io/gh/sphinx-doc/sphinx/pull/6533/diff?src=pr&el=tree#diff-c3BoaW54L3dyaXRlcnMvbGF0ZXgucHk=) | `81.92% <100%> (-0.03%)` | :arrow_down: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/sphinx-doc/sphinx/pull/6533?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/sphinx-doc/sphinx/pull/6533?src=pr&el=footer). Last update [f443fb5...4b497a7](https://codecov.io/gh/sphinx-doc/sphinx/pull/6533?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).

tk0miya: Note: 2.1.x is a branch for urgent releases. So normal bugs is better to merge 2.0 branch.

Patch:
diff --git a/sphinx/texinputs/sphinx.sty b/sphinx/texinputs/sphinx.sty
index 184b0d820..2eebbb914 100644
--- a/sphinx/texinputs/sphinx.sty
+++ b/sphinx/texinputs/sphinx.sty
@@ -413,6 +413,18 @@
 \newcommand\sphinxsetup[1]{\setkeys{sphinx}{#1}}
 
 
+%% ALPHANUMERIC LIST ITEMS
+\newcommand\sphinxsetlistlabels[5]
+{% #1 = style, #2 = enum, #3 = enumnext, #4 = prefix, #5 = suffix
+ % #2 and #3 are counters used by enumerate environement e.g. enumi, enumii.
+ % #1 is a macro such as \arabic or \alph
+ % prefix and suffix are strings (by default empty and a dot).
+ \@namedef{the#2}{#1{#2}}%
+ \@namedef{label#2}{#4\@nameuse{the#2}#5}%
+ \@namedef{p@#3}{\@nameuse{p@#2}#4\@nameuse{the#2}#5}%
+}%
+
+
 %% MAXLISTDEPTH
 %
 % remove LaTeX's cap on nesting depth if 'maxlistdepth' key used.
diff --git a/sphinx/writers/latex.py b/sphinx/writers/latex.py
index 24ec059e5..5fdcb5b93 100644
--- a/sphinx/writers/latex.py
+++ b/sphinx/writers/latex.py
@@ -1373,11 +1373,8 @@ def get_nested_level(node):
         suffix = node.get('suffix', '.')
 
         self.body.append('\\begin{enumerate}\n')
-        self.body.append('\\def\\the%s{%s{%s}}\n' % (enum, style, enum))
-        self.body.append('\\def\\label%s{%s\\the%s %s}\n' %
-                         (enum, prefix, enum, suffix))
-        self.body.append('\\makeatletter\\def\\p@%s{\\p@%s %s\\the%s %s}\\makeatother\n' %
-                         (enumnext, enum, prefix, enum, suffix))
+        self.body.append('\\sphinxsetlistlabels{%s}{%s}{%s}{%s}{%s}%%\n' %
+                         (style, enum, enumnext, prefix, suffix))
         if 'start' in node:
             self.body.append('\\setcounter{%s}{%d}\n' % (enum, node['start'] - 1))
         if self.table:


Test patch:
diff --git a/tests/test_build_latex.py b/tests/test_build_latex.py
index 56dfa7ca4..264f4e8b0 100644
--- a/tests/test_build_latex.py
+++ b/tests/test_build_latex.py
@@ -1292,25 +1292,15 @@ def test_latex_nested_enumerated_list(app, status, warning):
     app.builder.build_all()
 
     result = (app.outdir / 'python.tex').text(encoding='utf8')
-    assert ('\\def\\theenumi{\\arabic{enumi}}\n'
-            '\\def\\labelenumi{\\theenumi .}\n'
-            '\\makeatletter\\def\\p@enumii{\\p@enumi \\theenumi .}\\makeatother\n'
+    assert ('\\sphinxsetlistlabels{\\arabic}{enumi}{enumii}{}{.}%\n'
             '\\setcounter{enumi}{4}\n' in result)
-    assert ('\\def\\theenumii{\\alph{enumii}}\n'
-            '\\def\\labelenumii{\\theenumii .}\n'
-            '\\makeatletter\\def\\p@enumiii{\\p@enumii \\theenumii .}\\makeatother\n'
+    assert ('\\sphinxsetlistlabels{\\alph}{enumii}{enumiii}{}{.}%\n'
             '\\setcounter{enumii}{3}\n' in result)
-    assert ('\\def\\theenumiii{\\arabic{enumiii}}\n'
-            '\\def\\labelenumiii{\\theenumiii )}\n'
-            '\\makeatletter\\def\\p@enumiv{\\p@enumiii \\theenumiii )}\\makeatother\n'
+    assert ('\\sphinxsetlistlabels{\\arabic}{enumiii}{enumiv}{}{)}%\n'
             '\\setcounter{enumiii}{9}\n' in result)
-    assert ('\\def\\theenumiv{\\arabic{enumiv}}\n'
-            '\\def\\labelenumiv{(\\theenumiv )}\n'
-            '\\makeatletter\\def\\p@enumv{\\p@enumiv (\\theenumiv )}\\makeatother\n'
+    assert ('\\sphinxsetlistlabels{\\arabic}{enumiv}{enumv}{(}{)}%\n'
             '\\setcounter{enumiv}{23}\n' in result)
-    assert ('\\def\\theenumii{\\roman{enumii}}\n'
-            '\\def\\labelenumii{\\theenumii .}\n'
-            '\\makeatletter\\def\\p@enumiii{\\p@enumii \\theenumii .}\\makeatother\n'
+    assert ('\\sphinxsetlistlabels{\\roman}{enumii}{enumiii}{}{.}%\n'
             '\\setcounter{enumii}{2}\n' in result)