Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LaTeX: Add math_numsep support to latex builder #12652

Merged
merged 15 commits into from
Aug 18, 2024
Merged
6 changes: 4 additions & 2 deletions sphinx/texinputs/sphinx.sty
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@
\DeclareStringOption[-1]{numfigreset}
\DeclareBoolOption[false]{nonumfigreset}
\DeclareBoolOption[false]{mathnumfig}
\DeclareStringOption[.]{mathnumsep}
\define@key{sphinx}{bookmarksdepth}{\AtBeginDocument{\hypersetup{bookmarksdepth=#1}}}
\AtBeginDocument{\define@key{sphinx}{bookmarksdepth}{\hypersetup{bookmarksdepth=#1}}}
% \DeclareBoolOption[false]{usespart}% not used
Expand Down Expand Up @@ -731,7 +732,7 @@
\spx@tempa{div.important_} {important}
\spx@tempa{div.tip_} {tip}

% Add "legacy" hintTeXextras etc...
% Add "legacy" hintTeXextras etc...
\def\spx@tempa#1#2{% #1 = CSS like option prefix, #2 = legacy option prefix
\expandafter\let\csname KV@sphinx@#2TeXextras\expandafter\endcsname
\csname KV@sphinx@#1TeXextras\endcsname
Expand Down Expand Up @@ -807,7 +808,7 @@
% We try to
% - get Sphinx PDF builds to process fine in absence of fontawesome5
% - use fontawesome5 if present, but not if user prefers another package
% - provide an interface for using other LaTeX code for icons
% - provide an interface for using other LaTeX code for icons
% - provide an interface for using some other package than fontawesome5
% Indeed we can't load fontawesome5 unconditionally even if available,
% as it proves incompatible with fontawesome package.
Expand Down Expand Up @@ -898,6 +899,7 @@
\DisableKeyvalOption{sphinx}{numfigreset}
\DisableKeyvalOption{sphinx}{nonumfigreset}
\DisableKeyvalOption{sphinx}{mathnumfig}
\DisableKeyvalOption{sphinx}{mathnumsep}
jfbu marked this conversation as resolved.
Show resolved Hide resolved
\DisableKeyvalOption{sphinx}{booktabs}
\DisableKeyvalOption{sphinx}{borderless}
\DisableKeyvalOption{sphinx}{rowcolors}
Expand Down
31 changes: 22 additions & 9 deletions sphinx/texinputs/sphinxlatexnumfig.sty
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
%% NUMBERING OF FIGURES, TABLES, AND LITERAL BLOCKS
%
% change this info string if making any custom modification
\ProvidesFile{sphinxlatexnumfig.sty}[2021/01/27 numbering]
\ProvidesFile{sphinxlatexnumfig.sty}[v8.0 2024/07/23 numbering]

% Requires: remreset (old LaTeX only)
% relates to numfig and numfig_secnum_depth configuration variables
Expand Down Expand Up @@ -37,7 +37,11 @@
\def\theequation{\arabic{equation}}%
\fi
\else
\let\spx@preAthefigure\@empty
% See apologetic comments on TeX wizardry at bottom fo file.
% The reason for this one is to catch case where there will be only
% the number with no prefix from enclosing sectioning (can happen
% with latex_toplevel_section='part').
\def\spx@preAthefigure{\expandafter\spx@magicsep@s\romannumeral-`0}
\let\spx@preBthefigure\@empty
% \ifspx@opt@usespart % <-- LaTeX writer could pass such a 'usespart' boolean
% % as sphinx.sty package option
Expand All @@ -51,7 +55,7 @@
\ifnum\spx@opt@numfigreset>0
\ltx@ifundefined{c@chapter}
{}
{\g@addto@macro\spx@preAthefigure{\ifnum\c@chapter>\z@\arabic{chapter}.}%
{\g@addto@macro\spx@preAthefigure{\ifnum\c@chapter>\z@\arabic{chapter}\spx@magicsep}%
\g@addto@macro\spx@preBthefigure{\fi}}%
\fi
\ifnum\spx@opt@numfigreset>1
Expand All @@ -61,7 +65,7 @@
\ifspx@opt@mathnumfig
\@addtoreset{equation}{section}%
\fi%
\g@addto@macro\spx@preAthefigure{\ifnum\c@section>\z@\arabic{section}.}%
\g@addto@macro\spx@preAthefigure{\ifnum\c@section>\z@\arabic{section}\spx@magicsep}%
\g@addto@macro\spx@preBthefigure{\fi}%
\fi
\ifnum\spx@opt@numfigreset>2
Expand All @@ -71,7 +75,7 @@
\ifspx@opt@mathnumfig
\@addtoreset{equation}{subsection}%
\fi%
\g@addto@macro\spx@preAthefigure{\ifnum\c@subsection>\z@\arabic{subsection}.}%
\g@addto@macro\spx@preAthefigure{\ifnum\c@subsection>\z@\arabic{subsection}\spx@magicsep}%
\g@addto@macro\spx@preBthefigure{\fi}%
\fi
\ifnum\spx@opt@numfigreset>3
Expand All @@ -81,7 +85,7 @@
\ifspx@opt@mathnumfig
\@addtoreset{equation}{subsubsection}%
\fi%
\g@addto@macro\spx@preAthefigure{\ifnum\c@subsubsection>\z@\arabic{subsubsection}.}%
\g@addto@macro\spx@preAthefigure{\ifnum\c@subsubsection>\z@\arabic{subsubsection}\spx@magicsep}%
\g@addto@macro\spx@preBthefigure{\fi}%
\fi
\ifnum\spx@opt@numfigreset>4
Expand All @@ -91,7 +95,7 @@
\ifspx@opt@mathnumfig
\@addtoreset{equation}{paragraph}%
\fi%
\g@addto@macro\spx@preAthefigure{\ifnum\c@subparagraph>\z@\arabic{subparagraph}.}%
\g@addto@macro\spx@preAthefigure{\ifnum\c@subparagraph>\z@\arabic{subparagraph}\spx@magicsep}%
\g@addto@macro\spx@preBthefigure{\fi}%
\fi
\ifnum\spx@opt@numfigreset>5
Expand All @@ -101,7 +105,7 @@
\ifspx@opt@mathnumfig
\@addtoreset{equation}{subparagraph}%
\fi%
\g@addto@macro\spx@preAthefigure{\ifnum\c@subsubparagraph>\z@\arabic{subsubparagraph}.}%
\g@addto@macro\spx@preAthefigure{\ifnum\c@subsubparagraph>\z@\arabic{subsubparagraph}\spx@magicsep}%
\g@addto@macro\spx@preBthefigure{\fi}%
\fi
\expandafter\g@addto@macro
Expand All @@ -114,9 +118,18 @@
\g@addto@macro\theliteralblock{\arabic{literalblock}}%
\ifspx@opt@mathnumfig
\let\theequation\spx@preAthefigure
\g@addto@macro\theequation{\arabic{equation}}%
\g@addto@macro\theequation{E}%
\fi
\fi
}% end of big \AtBeginDocument

% Sorry for TeX wizardry here. We need to keep expandability. Explaining
% the mechanism is not really feasible to non-TeX experts, but the idea
% is to force next `\ifnum` conditional so we can check what comes next.
% All cases are accounted for (i.e. not an equation, or an equation at top
% level, or an equation in some section at some depth).
\def\spx@magicsep{\expandafter\spx@magicsep@i\romannumeral-`0}
\def\spx@magicsep@i#1{\if#1E\spx@opt@mathnumsep\arabic{equation}\else.#1\fi}
%
\def\spx@magicsep@s#1{\if#1E\arabic{equation}\else#1\fi}
\endinput
5 changes: 4 additions & 1 deletion sphinx/writers/latex.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,10 @@ def __init__(self, document: nodes.document, builder: LaTeXBuilder,
sphinxpkgoptions.append('nonumfigreset')

if self.config.numfig and self.config.math_numfig:
sphinxpkgoptions.append('mathnumfig')
sphinxpkgoptions.extend([
'mathnumfig',
'mathnumsep={%s}' % self.config.math_numsep,
])

if (self.config.language not in {'en', 'ja'} and
'fncychap' not in self.config.latex_elements):
Expand Down
10 changes: 5 additions & 5 deletions tests/test_builders/test_build_latex.py
Original file line number Diff line number Diff line change
Expand Up @@ -511,23 +511,23 @@ def test_latex_obey_numfig_secnum_depth_is_zero(app, status, warning):
app.build(force_all=True)

result = (app.outdir / 'SphinxManual.tex').read_text(encoding='utf8')
assert '\\usepackage[,nonumfigreset,mathnumfig]{sphinx}' in result
assert '\\usepackage[,nonumfigreset,mathnumfig,mathnumsep={.}]{sphinx}' in result

result = (app.outdir / 'SphinxHowTo.tex').read_text(encoding='utf8')
assert '\\usepackage[,nonumfigreset,mathnumfig]{sphinx}' in result
assert '\\usepackage[,nonumfigreset,mathnumfig,mathnumsep={.}]{sphinx}' in result


@pytest.mark.sphinx(
'latex', testroot='latex-numfig',
confoverrides={'numfig': True, 'numfig_secnum_depth': 2})
confoverrides={'numfig': True, 'numfig_secnum_depth': 2, 'math_numsep': "-"})
def test_latex_obey_numfig_secnum_depth_is_two(app, status, warning):
app.build(force_all=True)

result = (app.outdir / 'SphinxManual.tex').read_text(encoding='utf8')
assert '\\usepackage[,numfigreset=2,mathnumfig]{sphinx}' in result
assert '\\usepackage[,numfigreset=2,mathnumfig,mathnumsep={-}]{sphinx}' in result

result = (app.outdir / 'SphinxHowTo.tex').read_text(encoding='utf8')
assert '\\usepackage[,numfigreset=3,mathnumfig]{sphinx}' in result
assert '\\usepackage[,numfigreset=3,mathnumfig,mathnumsep={-}]{sphinx}' in result


@pytest.mark.sphinx(
Expand Down