From 18bf90822c9ad390d94cf26d4b0b7bcc109b5a4e Mon Sep 17 00:00:00 2001 From: jfbu Date: Fri, 4 Jan 2019 15:16:32 +0100 Subject: [PATCH 1/4] Fix LaTeX writer handling of user custom numfig_format --- sphinx/writers/latex.py | 4 ++-- tests/test_build_latex.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/sphinx/writers/latex.py b/sphinx/writers/latex.py index 63b180f34..8de1acff9 100644 --- a/sphinx/writers/latex.py +++ b/sphinx/writers/latex.py @@ -796,7 +796,7 @@ class LaTeXTranslator(nodes.NodeVisitor): ret.append(self.babel_renewcommand('\\figurename', definition)) if figure[1]: ret.append('\\makeatletter\n') - ret.append('\\def\\fnum@figure{\\figurename\\thefigure%s}\n' % + ret.append('\\def\\fnum@figure{\\figurename\\thefigure{}%s}\n' % text_type(figure[1]).strip().translate(tex_escape_map)) ret.append('\\makeatother\n') @@ -809,7 +809,7 @@ class LaTeXTranslator(nodes.NodeVisitor): ret.append(self.babel_renewcommand('\\tablename', definition)) if table[1]: ret.append('\\makeatletter\n') - ret.append('\\def\\fnum@table{\\tablename\\thetable%s}\n' % + ret.append('\\def\\fnum@table{\\tablename\\thetable{}%s}\n' % text_type(table[1]).strip().translate(tex_escape_map)) ret.append('\\makeatother\n') diff --git a/tests/test_build_latex.py b/tests/test_build_latex.py index 3103e832c..f5eff6996 100644 --- a/tests/test_build_latex.py +++ b/tests/test_build_latex.py @@ -289,9 +289,9 @@ def test_numref_with_prefix2(app, status, warning): print(status.getvalue()) print(warning.getvalue()) assert '\\addto\\captionsenglish{\\renewcommand{\\figurename}{Figure:}}' in result - assert '\\def\\fnum@figure{\\figurename\\thefigure.}' in result + assert '\\def\\fnum@figure{\\figurename\\thefigure{}.}' in result assert '\\addto\\captionsenglish{\\renewcommand{\\tablename}{Tab\\_}}' in result - assert '\\def\\fnum@table{\\tablename\\thetable:}' in result + assert '\\def\\fnum@table{\\tablename\\thetable{}:}' in result assert '\\addto\\captionsenglish{\\renewcommand{\\literalblockname}{Code-}}' in result assert ('\\hyperref[\\detokenize{index:fig1}]' '{Figure:\\ref{\\detokenize{index:fig1}}.\\@}') in result From 37f1aacd1f4dd806234b48ab8c3f3ab0ac52c6bd Mon Sep 17 00:00:00 2001 From: jfbu Date: Fri, 4 Jan 2019 23:44:20 +0100 Subject: [PATCH 2/4] LaTeX do not strip spaces from numfig_format split at %s Only for figure and table, currently. Code-bloks have some FIXME code to still be handled. PDF output should remain unchanged so this is backwards compatible. The `\nobreakspace` from LaTeX default `\fnum@figure` is removed by a normal space. If figname is an abbreviation, the dot is followed by a `\@` to avoid double space. At the location where the `\@` is inserted the `@` has catcode non-letter so a space after it is not swallowed. MEMO: when this is will be moved to sphinxmessages.sty on master branch it is needed to use `\@{}` or to modify locally catcode of `@` to avoid space swallowing.. Closes: #5889 --- sphinx/writers/latex.py | 22 +++++++++----------- tests/test_build_latex.py | 44 +++++++++++++++++++-------------------- 2 files changed, 32 insertions(+), 34 deletions(-) diff --git a/sphinx/writers/latex.py b/sphinx/writers/latex.py index 8de1acff9..5fbece6a6 100644 --- a/sphinx/writers/latex.py +++ b/sphinx/writers/latex.py @@ -792,26 +792,24 @@ class LaTeXTranslator(nodes.NodeVisitor): ret.append('\\def\\fnum@figure{%s}\n' % text_type(figure[0]).strip().translate(tex_escape_map)) else: - definition = text_type(figure[0]).strip().translate(tex_escape_map) + definition = escape_abbr(text_type(figure[0]).translate(tex_escape_map)) ret.append(self.babel_renewcommand('\\figurename', definition)) - if figure[1]: - ret.append('\\makeatletter\n') - ret.append('\\def\\fnum@figure{\\figurename\\thefigure{}%s}\n' % - text_type(figure[1]).strip().translate(tex_escape_map)) - ret.append('\\makeatother\n') + ret.append('\\makeatletter\n') + ret.append('\\def\\fnum@figure{\\figurename \\thefigure{}%s}\n' % + text_type(figure[1]).translate(tex_escape_map)) + ret.append('\\makeatother\n') table = self.builder.config.numfig_format['table'].split('%s', 1) if len(table) == 1: ret.append('\\def\\fnum@table{%s}\n' % text_type(table[0]).strip().translate(tex_escape_map)) else: - definition = text_type(table[0]).strip().translate(tex_escape_map) + definition = escape_abbr(text_type(table[0]).translate(tex_escape_map)) ret.append(self.babel_renewcommand('\\tablename', definition)) - if table[1]: - ret.append('\\makeatletter\n') - ret.append('\\def\\fnum@table{\\tablename\\thetable{}%s}\n' % - text_type(table[1]).strip().translate(tex_escape_map)) - ret.append('\\makeatother\n') + ret.append('\\makeatletter\n') + ret.append('\\def\\fnum@table{\\tablename \\thetable{}%s}\n' % + text_type(table[1]).translate(tex_escape_map)) + ret.append('\\makeatother\n') codeblock = self.builder.config.numfig_format['code-block'].split('%s', 1) if len(codeblock) == 1: diff --git a/tests/test_build_latex.py b/tests/test_build_latex.py index f5eff6996..f45f4b9ae 100644 --- a/tests/test_build_latex.py +++ b/tests/test_build_latex.py @@ -206,8 +206,8 @@ def test_numref(app, status, warning): print(result) print(status.getvalue()) print(warning.getvalue()) - assert '\\addto\\captionsenglish{\\renewcommand{\\figurename}{Fig.}}' in result - assert '\\addto\\captionsenglish{\\renewcommand{\\tablename}{Table}}' in result + assert '\\addto\\captionsenglish{\\renewcommand{\\figurename}{Fig.\\@ }}' in result + assert '\\addto\\captionsenglish{\\renewcommand{\\tablename}{Table }}' in result assert '\\addto\\captionsenglish{\\renewcommand{\\literalblockname}{Listing}}' in result assert ('\\hyperref[\\detokenize{index:fig1}]' '{Fig.\\@ \\ref{\\detokenize{index:fig1}}}') in result @@ -289,9 +289,9 @@ def test_numref_with_prefix2(app, status, warning): print(status.getvalue()) print(warning.getvalue()) assert '\\addto\\captionsenglish{\\renewcommand{\\figurename}{Figure:}}' in result - assert '\\def\\fnum@figure{\\figurename\\thefigure{}.}' in result + assert '\\def\\fnum@figure{\\figurename \\thefigure{}.}' in result assert '\\addto\\captionsenglish{\\renewcommand{\\tablename}{Tab\\_}}' in result - assert '\\def\\fnum@table{\\tablename\\thetable{}:}' in result + assert '\\def\\fnum@table{\\tablename \\thetable{}:}' in result assert '\\addto\\captionsenglish{\\renewcommand{\\literalblockname}{Code-}}' in result assert ('\\hyperref[\\detokenize{index:fig1}]' '{Figure:\\ref{\\detokenize{index:fig1}}.\\@}') in result @@ -324,8 +324,8 @@ def test_numref_with_language_ja(app, status, warning): print(result) print(status.getvalue()) print(warning.getvalue()) - assert u'\\renewcommand{\\figurename}{\u56f3}' in result # 図 - assert u'\\renewcommand{\\tablename}{\u8868}' in result # 表 + assert u'\\renewcommand{\\figurename}{\u56f3 }' in result # 図 + assert u'\\renewcommand{\\tablename}{\u8868 }' in result # 表 assert u'\\renewcommand{\\literalblockname}{\u30ea\u30b9\u30c8}' in result # リスト assert (u'\\hyperref[\\detokenize{index:fig1}]' u'{\u56f3 \\ref{\\detokenize{index:fig1}}}') in result @@ -422,8 +422,8 @@ def test_babel_with_no_language_settings(app, status, warning): assert '\\usepackage[Bjarne]{fncychap}' in result assert ('\\addto\\captionsenglish{\\renewcommand{\\contentsname}{Table of content}}\n' in result) - assert '\\addto\\captionsenglish{\\renewcommand{\\figurename}{Fig.}}\n' in result - assert '\\addto\\captionsenglish{\\renewcommand{\\tablename}{Table.}}\n' in result + assert '\\addto\\captionsenglish{\\renewcommand{\\figurename}{Fig.\\@ }}\n' in result + assert '\\addto\\captionsenglish{\\renewcommand{\\tablename}{Table.\\@ }}\n' in result assert '\\addto\\extrasenglish{\\def\\pageautorefname{page}}\n' in result assert '\\shorthandoff' not in result @@ -443,8 +443,8 @@ def test_babel_with_language_de(app, status, warning): assert '\\usepackage[Sonny]{fncychap}' in result assert ('\\addto\\captionsngerman{\\renewcommand{\\contentsname}{Table of content}}\n' in result) - assert '\\addto\\captionsngerman{\\renewcommand{\\figurename}{Fig.}}\n' in result - assert '\\addto\\captionsngerman{\\renewcommand{\\tablename}{Table.}}\n' in result + assert '\\addto\\captionsngerman{\\renewcommand{\\figurename}{Fig.\\@ }}\n' in result + assert '\\addto\\captionsngerman{\\renewcommand{\\tablename}{Table.\\@ }}\n' in result assert '\\addto\\extrasngerman{\\def\\pageautorefname{Seite}}\n' in result assert '\\shorthandoff{"}' in result @@ -464,8 +464,8 @@ def test_babel_with_language_ru(app, status, warning): assert '\\usepackage[Sonny]{fncychap}' in result assert ('\\addto\\captionsrussian{\\renewcommand{\\contentsname}{Table of content}}\n' in result) - assert '\\addto\\captionsrussian{\\renewcommand{\\figurename}{Fig.}}\n' in result - assert '\\addto\\captionsrussian{\\renewcommand{\\tablename}{Table.}}\n' in result + assert '\\addto\\captionsrussian{\\renewcommand{\\figurename}{Fig.\\@ }}\n' in result + assert '\\addto\\captionsrussian{\\renewcommand{\\tablename}{Table.\\@ }}\n' in result assert (u'\\addto\\extrasrussian{\\def\\pageautorefname' u'{\u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0430}}\n' in result) assert '\\shorthandoff{"}' in result @@ -486,8 +486,8 @@ def test_babel_with_language_tr(app, status, warning): assert '\\usepackage[Sonny]{fncychap}' in result assert ('\\addto\\captionsturkish{\\renewcommand{\\contentsname}{Table of content}}\n' in result) - assert '\\addto\\captionsturkish{\\renewcommand{\\figurename}{Fig.}}\n' in result - assert '\\addto\\captionsturkish{\\renewcommand{\\tablename}{Table.}}\n' in result + assert '\\addto\\captionsturkish{\\renewcommand{\\figurename}{Fig.\\@ }}\n' in result + assert '\\addto\\captionsturkish{\\renewcommand{\\tablename}{Table.\\@ }}\n' in result assert '\\addto\\extrasturkish{\\def\\pageautorefname{sayfa}}\n' in result assert '\\shorthandoff{=}' in result @@ -506,8 +506,8 @@ def test_babel_with_language_ja(app, status, warning): assert '\\usepackage{times}' in result assert '\\usepackage[Sonny]{fncychap}' not in result assert '\\renewcommand{\\contentsname}{Table of content}\n' in result - assert '\\renewcommand{\\figurename}{Fig.}\n' in result - assert '\\renewcommand{\\tablename}{Table.}\n' in result + assert '\\renewcommand{\\figurename}{Fig.\\@ }\n' in result + assert '\\renewcommand{\\tablename}{Table.\\@ }\n' in result assert u'\\def\\pageautorefname{ページ}\n' in result assert '\\shorthandoff' not in result @@ -527,8 +527,8 @@ def test_babel_with_unknown_language(app, status, warning): assert '\\usepackage[Sonny]{fncychap}' in result assert ('\\addto\\captionsenglish{\\renewcommand{\\contentsname}{Table of content}}\n' in result) - assert '\\addto\\captionsenglish{\\renewcommand{\\figurename}{Fig.}}\n' in result - assert '\\addto\\captionsenglish{\\renewcommand{\\tablename}{Table.}}\n' in result + assert '\\addto\\captionsenglish{\\renewcommand{\\figurename}{Fig.\\@ }}\n' in result + assert '\\addto\\captionsenglish{\\renewcommand{\\tablename}{Table.\\@ }}\n' in result assert '\\addto\\extrasenglish{\\def\\pageautorefname{page}}\n' in result assert '\\shorthandoff' in result @@ -551,8 +551,8 @@ def test_polyglossia_with_language_de(app, status, warning): assert '\\usepackage[Sonny]{fncychap}' in result assert ('\\addto\\captionsgerman{\\renewcommand{\\contentsname}{Table of content}}\n' in result) - assert '\\addto\\captionsgerman{\\renewcommand{\\figurename}{Fig.}}\n' in result - assert '\\addto\\captionsgerman{\\renewcommand{\\tablename}{Table.}}\n' in result + assert '\\addto\\captionsgerman{\\renewcommand{\\figurename}{Fig.\\@ }}\n' in result + assert '\\addto\\captionsgerman{\\renewcommand{\\tablename}{Table.\\@ }}\n' in result assert '\\def\\pageautorefname{Seite}\n' in result assert '\\shorthandoff' not in result @@ -573,8 +573,8 @@ def test_polyglossia_with_language_de_1901(app, status, warning): assert '\\usepackage[Sonny]{fncychap}' in result assert ('\\addto\\captionsgerman{\\renewcommand{\\contentsname}{Table of content}}\n' in result) - assert '\\addto\\captionsgerman{\\renewcommand{\\figurename}{Fig.}}\n' in result - assert '\\addto\\captionsgerman{\\renewcommand{\\tablename}{Table.}}\n' in result + assert '\\addto\\captionsgerman{\\renewcommand{\\figurename}{Fig.\\@ }}\n' in result + assert '\\addto\\captionsgerman{\\renewcommand{\\tablename}{Table.\\@ }}\n' in result assert '\\def\\pageautorefname{page}\n' in result assert '\\shorthandoff' not in result From 2259466d4b8ca90248aaeba6e9630aa78f4b4509 Mon Sep 17 00:00:00 2001 From: jfbu Date: Sat, 5 Jan 2019 00:14:45 +0100 Subject: [PATCH 3/4] Remove some spaces added in last commit, which are ignored anyhow --- sphinx/writers/latex.py | 4 ++-- tests/test_build_latex.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/sphinx/writers/latex.py b/sphinx/writers/latex.py index 5fbece6a6..33cdb3d23 100644 --- a/sphinx/writers/latex.py +++ b/sphinx/writers/latex.py @@ -795,7 +795,7 @@ class LaTeXTranslator(nodes.NodeVisitor): definition = escape_abbr(text_type(figure[0]).translate(tex_escape_map)) ret.append(self.babel_renewcommand('\\figurename', definition)) ret.append('\\makeatletter\n') - ret.append('\\def\\fnum@figure{\\figurename \\thefigure{}%s}\n' % + ret.append('\\def\\fnum@figure{\\figurename\\thefigure{}%s}\n' % text_type(figure[1]).translate(tex_escape_map)) ret.append('\\makeatother\n') @@ -807,7 +807,7 @@ class LaTeXTranslator(nodes.NodeVisitor): definition = escape_abbr(text_type(table[0]).translate(tex_escape_map)) ret.append(self.babel_renewcommand('\\tablename', definition)) ret.append('\\makeatletter\n') - ret.append('\\def\\fnum@table{\\tablename \\thetable{}%s}\n' % + ret.append('\\def\\fnum@table{\\tablename\\thetable{}%s}\n' % text_type(table[1]).translate(tex_escape_map)) ret.append('\\makeatother\n') diff --git a/tests/test_build_latex.py b/tests/test_build_latex.py index f45f4b9ae..443f6ea27 100644 --- a/tests/test_build_latex.py +++ b/tests/test_build_latex.py @@ -289,9 +289,9 @@ def test_numref_with_prefix2(app, status, warning): print(status.getvalue()) print(warning.getvalue()) assert '\\addto\\captionsenglish{\\renewcommand{\\figurename}{Figure:}}' in result - assert '\\def\\fnum@figure{\\figurename \\thefigure{}.}' in result + assert '\\def\\fnum@figure{\\figurename\\thefigure{}.}' in result assert '\\addto\\captionsenglish{\\renewcommand{\\tablename}{Tab\\_}}' in result - assert '\\def\\fnum@table{\\tablename \\thetable{}:}' in result + assert '\\def\\fnum@table{\\tablename\\thetable{}:}' in result assert '\\addto\\captionsenglish{\\renewcommand{\\literalblockname}{Code-}}' in result assert ('\\hyperref[\\detokenize{index:fig1}]' '{Figure:\\ref{\\detokenize{index:fig1}}.\\@}') in result From f42e406af45ab8876d413620cbe4f663ebed888e Mon Sep 17 00:00:00 2001 From: jfbu Date: Sat, 5 Jan 2019 10:02:00 +0100 Subject: [PATCH 4/4] Update CHANGES for PR #5891 --- CHANGES | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGES b/CHANGES index 8eef87716..d0aa89d50 100644 --- a/CHANGES +++ b/CHANGES @@ -18,6 +18,8 @@ Bugs fixed * #3707: latex: no bold checkmark (✔) available. * #5605: with the documentation language set to Chinese, English words could not be searched. +* #5889: LaTeX: user ``numfig_format`` is stripped of spaces and may cause + build failure Testing --------