Let LaTeX obey :confval:math_numfig for equation numbering

This commit is contained in:
jfbu 2017-12-21 12:23:20 +01:00
parent 24c9103a2c
commit 3bb61e9094
5 changed files with 56 additions and 6 deletions

View File

@ -47,12 +47,16 @@ or use Python raw strings (``r"raw"``).
.. confval:: math_numfig .. confval:: math_numfig
If ``True``, displayed math equations are numbered across pages in html and If ``True``, displayed math equations are numbered across pages in html and
related (epub, ...) output when :confval:`numfig` is enabled. related (epub, ...) output (also with latex) when :confval:`numfig` is enabled.
:confval:`numfig_secnum_depth` is respected. The :rst:role:`eq` role must :confval:`numfig_secnum_depth` is respected. The :rst:role:`eq` role must
be used to reference equation numbers, not the :rst:role:`numref` role. be used to reference equation numbers, not the :rst:role:`numref` role.
Default is ``True``. Default is ``True``.
.. versionadded:: 1.7 .. versionadded:: 1.7
If ``False``, latex behaves as in former releases i.e. for ``'manual'``
docclass (and ``'howto'`` for Japanese) it resets by default the
equation numbers per each toplevel section, and for ``'howto'`` it uses
continuous numbering across entire PDF.
:mod:`.mathbase` defines these new markup elements: :mod:`.mathbase` defines these new markup elements:

View File

@ -249,6 +249,7 @@
\DeclareStringOption[0]{maxlistdepth}% \newcommand*\spx@opt@maxlistdepth{0} \DeclareStringOption[0]{maxlistdepth}% \newcommand*\spx@opt@maxlistdepth{0}
\DeclareStringOption[-1]{numfigreset} \DeclareStringOption[-1]{numfigreset}
\DeclareBoolOption[false]{nonumfigreset} \DeclareBoolOption[false]{nonumfigreset}
\DeclareBoolOption[false]{mathnumfig}
% \DeclareBoolOption[false]{usespart}% not used % \DeclareBoolOption[false]{usespart}% not used
% dimensions, we declare the \dimen registers here. % dimensions, we declare the \dimen registers here.
\newdimen\sphinxverbatimsep \newdimen\sphinxverbatimsep
@ -353,6 +354,7 @@
\DisableKeyvalOption{sphinx}{maxlistdepth} \DisableKeyvalOption{sphinx}{maxlistdepth}
\DisableKeyvalOption{sphinx}{numfigreset} \DisableKeyvalOption{sphinx}{numfigreset}
\DisableKeyvalOption{sphinx}{nonumfigreset} \DisableKeyvalOption{sphinx}{nonumfigreset}
\DisableKeyvalOption{sphinx}{mathnumfig}
% user interface: options can be changed midway in a document! % user interface: options can be changed midway in a document!
\newcommand\sphinxsetup[1]{\setkeys{sphinx}{#1}} \newcommand\sphinxsetup[1]{\setkeys{sphinx}{#1}}
@ -682,11 +684,16 @@
\@removefromreset{figure}{chapter}% \@removefromreset{figure}{chapter}%
\@removefromreset{table}{chapter}% \@removefromreset{table}{chapter}%
\@removefromreset{literalblock}{chapter}% \@removefromreset{literalblock}{chapter}%
\ifspx@opt@mathnumfig
\@removefromreset{equation}{chapter}%
\fi
}% }%
\def\thefigure{\arabic{figure}}% \def\thefigure{\arabic{figure}}%
\def\thetable {\arabic{table}}% \def\thetable {\arabic{table}}%
\def\theliteralblock{\arabic{literalblock}}% \def\theliteralblock{\arabic{literalblock}}%
%\let\theHliteralblock\theliteralblock \ifspx@opt@mathnumfig
\def\theequation{\arabic{equation}}%
\fi
\else \else
\let\spx@preAthefigure\@empty \let\spx@preAthefigure\@empty
\let\spx@preBthefigure\@empty \let\spx@preBthefigure\@empty
@ -709,6 +716,9 @@
\@addtoreset{figure}{section}% \@addtoreset{figure}{section}%
\@addtoreset{table}{section}% \@addtoreset{table}{section}%
\@addtoreset{literalblock}{section}% \@addtoreset{literalblock}{section}%
\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}.}%
\g@addto@macro\spx@preBthefigure{\fi}% \g@addto@macro\spx@preBthefigure{\fi}%
\fi \fi
@ -716,6 +726,9 @@
\@addtoreset{figure}{subsection}% \@addtoreset{figure}{subsection}%
\@addtoreset{table}{subsection}% \@addtoreset{table}{subsection}%
\@addtoreset{literalblock}{subsection}% \@addtoreset{literalblock}{subsection}%
\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}.}%
\g@addto@macro\spx@preBthefigure{\fi}% \g@addto@macro\spx@preBthefigure{\fi}%
\fi \fi
@ -723,6 +736,9 @@
\@addtoreset{figure}{subsubsection}% \@addtoreset{figure}{subsubsection}%
\@addtoreset{table}{subsubsection}% \@addtoreset{table}{subsubsection}%
\@addtoreset{literalblock}{subsubsection}% \@addtoreset{literalblock}{subsubsection}%
\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}.}%
\g@addto@macro\spx@preBthefigure{\fi}% \g@addto@macro\spx@preBthefigure{\fi}%
\fi \fi
@ -730,6 +746,9 @@
\@addtoreset{figure}{paragraph}% \@addtoreset{figure}{paragraph}%
\@addtoreset{table}{paragraph}% \@addtoreset{table}{paragraph}%
\@addtoreset{literalblock}{paragraph}% \@addtoreset{literalblock}{paragraph}%
\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}.}%
\g@addto@macro\spx@preBthefigure{\fi}% \g@addto@macro\spx@preBthefigure{\fi}%
\fi \fi
@ -737,6 +756,9 @@
\@addtoreset{figure}{subparagraph}% \@addtoreset{figure}{subparagraph}%
\@addtoreset{table}{subparagraph}% \@addtoreset{table}{subparagraph}%
\@addtoreset{literalblock}{subparagraph}% \@addtoreset{literalblock}{subparagraph}%
\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}.}%
\g@addto@macro\spx@preBthefigure{\fi}% \g@addto@macro\spx@preBthefigure{\fi}%
\fi \fi
@ -748,6 +770,10 @@
\g@addto@macro\thefigure{\arabic{figure}}% \g@addto@macro\thefigure{\arabic{figure}}%
\g@addto@macro\thetable{\arabic{table}}% \g@addto@macro\thetable{\arabic{table}}%
\g@addto@macro\theliteralblock{\arabic{literalblock}}% \g@addto@macro\theliteralblock{\arabic{literalblock}}%
\ifspx@opt@mathnumfig
\let\theequation\spx@preAthefigure
\g@addto@macro\theequation{\arabic{equation}}%
\fi
\fi \fi

View File

@ -601,6 +601,11 @@ class LaTeXTranslator(nodes.NodeVisitor):
(',numfigreset=%s' % self.numfig_secnum_depth) (',numfigreset=%s' % self.numfig_secnum_depth)
else: else:
self.elements['sphinxpkgoptions'] += ',nonumfigreset' self.elements['sphinxpkgoptions'] += ',nonumfigreset'
try:
if builder.config.math_numfig:
self.elements['sphinxpkgoptions'] += ',mathnumfig'
except:
pass
if builder.config.latex_logo: if builder.config.latex_logo:
# no need for \\noindent here, used in flushright # no need for \\noindent here, used in flushright

View File

@ -2,6 +2,8 @@
master_doc = 'index' master_doc = 'index'
extensions = ['sphinx.ext.imgmath'] # for math_numfig
latex_documents = [ latex_documents = [
('indexmanual', 'SphinxManual.tex', 'Test numfig manual', ('indexmanual', 'SphinxManual.tex', 'Test numfig manual',
'Sphinx', 'manual'), 'Sphinx', 'manual'),

View File

@ -353,10 +353,10 @@ def test_latex_obey_numfig_secnum_depth_is_zero(app, status, warning):
app.builder.build_all() app.builder.build_all()
result = (app.outdir / 'SphinxManual.tex').text(encoding='utf8') result = (app.outdir / 'SphinxManual.tex').text(encoding='utf8')
assert '\\usepackage[,nonumfigreset]{sphinx}' in result assert '\\usepackage[,nonumfigreset,mathnumfig]{sphinx}' in result
result = (app.outdir / 'SphinxHowTo.tex').text(encoding='utf8') result = (app.outdir / 'SphinxHowTo.tex').text(encoding='utf8')
assert '\\usepackage[,nonumfigreset]{sphinx}' in result assert '\\usepackage[,nonumfigreset,mathnumfig]{sphinx}' in result
@pytest.mark.sphinx( @pytest.mark.sphinx(
@ -366,10 +366,23 @@ def test_latex_obey_numfig_secnum_depth_is_two(app, status, warning):
app.builder.build_all() app.builder.build_all()
result = (app.outdir / 'SphinxManual.tex').text(encoding='utf8') result = (app.outdir / 'SphinxManual.tex').text(encoding='utf8')
assert '\\usepackage[,numfigreset=2]{sphinx}' in result assert '\\usepackage[,numfigreset=2,mathnumfig]{sphinx}' in result
result = (app.outdir / 'SphinxHowTo.tex').text(encoding='utf8') result = (app.outdir / 'SphinxHowTo.tex').text(encoding='utf8')
assert '\\usepackage[,numfigreset=3]{sphinx}' in result assert '\\usepackage[,numfigreset=3,mathnumfig]{sphinx}' in result
@pytest.mark.sphinx(
'latex', testroot='latex-numfig',
confoverrides={'numfig': True, 'math_numfig': False})
def test_latex_obey_numfig_but_math_numfig_false(app, status, warning):
app.builder.build_all()
result = (app.outdir / 'SphinxManual.tex').text(encoding='utf8')
assert '\\usepackage[,numfigreset=1]{sphinx}' in result
result = (app.outdir / 'SphinxHowTo.tex').text(encoding='utf8')
assert '\\usepackage[,numfigreset=2]{sphinx}' in result
@pytest.mark.sphinx('latex') @pytest.mark.sphinx('latex')