8000 Merge pull request #9999 from jfbu/9985_latex_on_master · sphinx-doc/sphinx@468384f · GitHub
[go: up one dir, main page]

8000
Skip to content

Commit 468384f

Browse files
authored
Merge pull request #9999 from jfbu/9985_latex_on_master
LaTeX: separate terms from their definitions by a CR (close #9985)
2 parents 4e8bca2 + 559e4d3 commit 468384f

File tree

3 files changed

+74
-40
lines changed

3 files changed

+74
-40
lines changed

sphinx/texinputs/sphinxlatexlists.sty

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,47 @@
11
%% ALPHANUMERIC LIST ITEMS
22
%
33
% change this info string if making any custom modification
4-
\ProvidesFile{sphinxlatexlists.sty}[2021/01/27 lists]
4+
\ProvidesFile{sphinxlatexlists.sty}[2021/12/20 lists]
55

66
% Provides support for this output mark-up from Sphinx latex writer:
77
% - \sphinxsetlistlabels
8-
8+
% - \sphinxlineitem
9+
% and for the maxlistdepth key of sphinxsetup
910
% Dependencies: the \spx@opt@maxlistdepth from sphinx.sty
1011

12+
% We need some helpers macros
13+
\newtoks\spx@lineitemlabel
14+
\long\def\sphinx@gobto@sphinxlineitem#1\sphinxlineitem{}
15+
% TeX/LaTeX has no (easy to use) built-in "peek-ahead" mechanism, but
16+
% we would like to know if next token is another \sphinxlineitem (this
17+
% can happen in glossary entries with multiple terms for same definition)
18+
% so we simply grab next token (assuming it is not {tokens} originally)
19+
\newcommand\sphinxlineitem[2]{%
20+
% safe test of whether #2 is \sphinxlineitem
21+
\sphinx@gobto@sphinxlineitem#2\@gobbletwo\sphinxlineitem\unless
22+
\iftrue
23+
% case with sphinxlineitem immediately followed by another \sphinxlineitem:
24+
% accumulate successive terms until actual definition or sub-list is found
25+
\spx@lineitemlabel\expandafter{\the\spx@lineitemlabel\strut#1\\}%
26+
\else
27+
% now issue the \item command with possibly multi-line contents
28+
% these weird incantations with \kern are related to how LaTeX
29+
% handles \item generally
30+
\item[\kern\labelwidth\kern-\itemindent\kern-\leftmargin
31+
{\parbox[t]{\dimexpr\linewidth+\leftmargin\relax}{%
32+
\raggedright
33+
\the\spx@lineitemlabel% accumulated terms before this one, CR separated
34+
\strut#1}}% due to LaTeX internals no \par token allowed here,
35+
% but the \parbox will insert one tacitly at end
36+
\kern-\labelsep]%
37+
\spx@lineitemlabel{}%
38+
% this causes the label to be typeset (filling up the line), clearing up
39+
% things in case a nested list follows.
40+
\leavevmode
41+
\fi #2%
42+
}%
43+
44+
1145
\newcommand\sphinxsetlistlabels[5]
1246
{% #1 = style, #2 = enum, #3 = enumnext, #4 = prefix, #5 = suffix
1347
% #2 and #3 are counters used by enumerate environment e.g. enumi, enumii.

sphinx/writers/latex.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1092,8 +1092,8 @@ def visit_term(self, node: Element) -> None:
10921092
ctx = r'\phantomsection'
10931093
for node_id in node['ids']:
10941094
ctx += self.hypertarget(node_id, anchor=False)
1095-
ctx += r'}] \leavevmode'
1096-
self.body.append(r'\item[{')
1095+
ctx += r'}'
1096+
self.body.append(r'\sphinxlineitem{')
10971097
self.context.append(ctx)
10981098

10991099
def depart_term(self, node: Element) -> None:

tests/test_build_latex.py

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -834,18 +834,18 @@ def test_latex_show_urls_is_inline(app, status, warning):
834834
'Footnote inside footnote\n%\n\\end{footnotetext}\\ignorespaces') in result
835835
assert ('\\sphinxhref{http://sphinx-doc.org/~test/}{URL including tilde} '
836836
'(http://sphinx\\sphinxhyphen{}doc.org/\\textasciitilde{}test/)') in result
837-
assert ('\\item[{\\sphinxhref{http://sphinx-doc.org/}{URL in term} '
838-
'(http://sphinx\\sphinxhyphen{}doc.org/)}] '
839-
'\\leavevmode\n\\sphinxAtStartPar\nDescription' in result)
840-
assert ('\\item[{Footnote in term \\sphinxfootnotemark[6]}] '
841-
'\\leavevmode%\n\\begin{footnotetext}[6]'
837+
assert ('\\sphinxlineitem{\\sphinxhref{http://sphinx-doc.org/}{URL in term} '
838+
'(http://sphinx\\sphinxhyphen{}doc.org/)}'
839+
'\n\\sphinxAtStartPar\nDescription' in result)
840+
assert ('\\sphinxlineitem{Footnote in term \\sphinxfootnotemark[6]}'
841+
'%\n\\begin{footnotetext}[6]'
842842
'\\phantomsection\\label{\\thesphinxscope.6}%\n'
843843
'\\sphinxAtStartFootnote\n'
844844
'Footnote in term\n%\n\\end{footnotetext}\\ignorespaces '
845845
'\n\\sphinxAtStartPar\nDescription') in result
846-
assert ('\\item[{\\sphinxhref{http://sphinx-doc.org/}{Term in deflist} '
847-
'(http://sphinx\\sphinxhyphen{}doc.org/)}] '
848-
'\\leavevmode\n\\sphinxAtStartPar\nDescription') in result
846+
assert ('\\sphinxlineitem{\\sphinxhref{http://sphinx-doc.org/}{Term in deflist} '
847+
'(http://sphinx\\sphinxhyphen{}doc.org/)}'
848+
'\n\\sphinxAtStartPar\nDescription') in result
849849
assert '\\sphinxurl{https://github.com/sphinx-doc/sphinx}\n' in result
850850
assert ('\\sphinxhref{mailto:sphinx-dev@googlegroups.com}'
851851
'{sphinx\\sphinxhyphen{}dev@googlegroups.com}') in result
@@ -893,22 +893,22 @@ def test_latex_show_urls_is_footnote(app, status, warning): F438
893893
assert ('\\sphinxhref{http://sphinx-doc.org/~test/}{URL including tilde}'
894894
'%\n\\begin{footnote}[5]\\sphinxAtStartFootnote\n'
895895
'\\sphinxnolinkurl{http://sphinx-doc.org/~test/}\n%\n\\end{footnote}') in result
896-
assert ('\\item[{\\sphinxhref{http://sphinx-doc.org/}'
897-
'{URL in term}\\sphinxfootnotemark[9]}] '
898-
'\\leavevmode%\n\\begin{footnotetext}[9]'
896+
assert ('\\sphinxlineitem{\\sphinxhref{http://sphinx-doc.org/}'
897+
'{URL in term}\\sphinxfootnotemark[9]}'
898+
'%\n\\begin{footnotetext}[9]'
899899
'\\phantomsection\\label{\\thesphinxscope.9}%\n'
900900
'\\sphinxAtStartFootnote\n'
901901
'\\sphinxnolinkurl{http://sphinx-doc.org/}\n%\n'
902902
'\\end{footnotetext}\\ignorespaces \n\\sphinxAtStartPar\nDescription') in result
903-
assert ('\\item[{Footnote in term \\sphinxfootnotemark[11]}] '
904-
'\\leavevmode%\n\\begin{footnotetext}[11]'
903+
assert ('\\sphinxlineitem{Footnote in term \\sphinxfootnotemark[11]}'
904+
'%\n\\begin{footnotetext}[11]'
905905
'\\phantomsection\\label{\\thesphinxscope.11}%\n'
906906
'\\sphinxAtStartFootnote\n'
907907
'Footnote in term\n%\n\\end{footnotetext}\\ignorespaces '
908908
'\n\\sphinxAtStartPar\nDescription') in result
909-
assert ('\\item[{\\sphinxhref{http://sphinx-doc.org/}{Term in deflist}'
910-
'\\sphinxfootnotemark[10]}] '
911-
'\\leavevmode%\n\\begin{footnotetext}[10]'
909+
assert ('\\sphinxlineitem{\\sphinxhref{http://sphinx-doc.org/}{Term in deflist}'
910+
'\\sphinxfootnotemark[10]}'
911+
'%\n\\begin{footnotetext}[10]'
912912
'\\phantomsection\\label{\\thesphinxscope.10}%\n'
913913
'\\sphinxAtStartFootnote\n'
914914
'\\sphinxnolinkurl{http://sphinx-doc.org/}\n%\n'
@@ -955,16 +955,16 @@ def test_latex_show_urls_is_no(app, status, warning):
955955
'\\sphinxAtStartFootnote\n'
956956
'Footnote inside footnote\n%\n\\end{footnotetext}\\ignorespaces') in result
957957
assert '\\sphinxhref{http://sphinx-doc.org/~test/}{URL including tilde}' in result
958-
assert ('\\item[{\\sphinxhref{http://sphinx-doc.org/}{URL in term}}] '
959-
'\\leavevmode\n\\sphinxAtStartPar\nDescription') in result
960-
assert ('\\item[{Footnote in term \\sphinxfootnotemark[6]}] '
961-
'\\leavevmode%\n\\begin{footnotetext}[6]'
958+
assert ('\\sphinxlineitem{\\sphinxhref{http://sphinx-doc.org/}{URL in term}}'
959+
'\n\\sphinxAtStartPar\nDescription') in result
960+
assert ('\\sphinxlineitem{Footnote in term \\sphinxfootnotemark[6]}'
961+
'%\n\\begin{footnotetext}[6]'
962962
'\\phantomsection\\label{\\thesphinxscope.6}%\n'
963963
'\\sphinxAtStartFootnote\n'
964964
'Footnote in term\n%\n\\end{footnotetext}\\ignorespaces '
965965
'\n\\sphinxAtStartPar\nDescription') in result
966-
assert ('\\item[{\\sphinxhref{http://sphinx-doc.org/}{Term in deflist}}] '
967-
'\\leavevmode\n\\sphinxAtStartPar\nDescription') in result
966+
assert ('\\sphinxlineitem{\\sphinxhref{http://sphinx-doc.org/}{Term in deflist}}'
967+
'\n\\sphinxAtStartPar\nDescription') in result
968968
assert ('\\sphinxurl{https://github.com/sphinx-doc/sphinx}\n' in result)
969969
assert ('\\sphinxhref{mailto:sphinx-dev@googlegroups.com}'
970970
'{sphinx\\sphinxhyphen{}dev@googlegroups.com}\n') in result
@@ -1454,23 +1454,23 @@ def test_latex_glossary(app, status, warning):
14541454
app.builder.build_all()
14551455

14561456
result = (app.outdir / 'python.tex').read_text()
1457-
assert ('\\item[{ähnlich\\index{ähnlich@\\spxentry{ähnlich}|spxpagem}'
1457+
assert (r'\sphinxlineitem{ähnlich\index{ähnlich@\spxentry{ähnlich}|spxpagem}'
14581458
r'\phantomsection'
1459-
r'\label{\detokenize{index:term-ahnlich}}}] \leavevmode' in result)
1460-
assert (r'\item[{boson\index{boson@\spxentry{boson}|spxpagem}\phantomsection'
1461-
r'\label{\detokenize{index:term-boson}}}] \leavevmode' in result)
1462-
assert (r'\item[{\sphinxstyleemphasis{fermion}'
1459+
r'\label{\detokenize{index:term-ahnlich}}}' in result)
1460+
assert (r'\sphinxlineitem{boson\index{boson@\spxentry{boson}|spxpagem}\phantomsection'
1461+
r'\label{\detokenize{index:term-boson}}}' in result)
1462+
assert (r'\sphinxlineitem{\sphinxstyleemphasis{fermion}'
14631463
r'\index{fermion@\spxentry{fermion}|spxpagem}'
14641464
r'\phantomsection'
1465-
r'\label{\detokenize{index:term-fermion}}}] \leavevmode' in result)
1466-
assert (r'\item[{tauon\index{tauon@\spxentry{tauon}|spxpagem}\phantomsection'
1467-
r'\label{\detokenize{index:term-tauon}}}] \leavevmode'
1468-
r'\item[{myon\index{myon@\spxentry{myon}|spxpagem}\phantomsection'
1469-
r'\label{\detokenize{index:term-myon}}}] \leavevmode'
1470-
r'\item[{electron\index{electron@\spxentry{electron}|spxpagem}\phantomsection'
1471-
r'\label{\detokenize{index:term-electron}}}] \leavevmode' in result)
1472-
assert ('\\item[{über\\index{über@\\spxentry{über}|spxpagem}\\phantomsection'
1473-
r'\label{\detokenize{index:term-uber}}}] \leavevmode' in result)
1465+
r'\label{\detokenize{index:term-fermion}}}' in result)
1466+
assert (r'\sphinxlineitem{tauon\index{tauon@\spxentry{tauon}|spxpagem}\phantomsection'
1467+
r'\label{\detokenize{index:term-tauon}}}'
1468+
r'\sphinxlineitem{myon\index{myon@\spxentry{myon}|spxpagem}\phantomsection'
1469+
r'\label{\detokenize{index:term-myon}}}'
1470+
r'\sphinxlineitem{electron\index{electron@\spxentry{electron}|spxpagem}\phantomsection'
1471+
r'\label{\detokenize{index:term-electron}}}' in result)
1472+
assert (r'\sphinxlineitem{über\index{über@\spxentry{über}|spxpagem}\phantomsection'
1473+
r'\label{\detokenize{index:term-uber}}}' in result)
14741474

14751475

14761476
@pytest.mark.sphinx('latex', testroot='latex-labels')

0 commit comments

Comments
 (0)
0