diff --git a/CHANGES b/CHANGES index ee4c9f915..1b25e4b37 100644 --- a/CHANGES +++ b/CHANGES @@ -20,6 +20,7 @@ Bugs fixed * sphinx.testing uses deprecated pytest API; ``Node.get_marker(name)`` * #5016: crashed when recommonmark.AutoStrictify is enabled * #5022: latex: crashed with docutils package provided by Debian/Ubuntu +* #5009: latex: a label for table is vanished if table does not have a caption Testing -------- diff --git a/sphinx/templates/latex/longtable.tex_t b/sphinx/templates/latex/longtable.tex_t index b7310a780..9b8c46d81 100644 --- a/sphinx/templates/latex/longtable.tex_t +++ b/sphinx/templates/latex/longtable.tex_t @@ -9,8 +9,12 @@ <%= table.get_colspec() %> <%- if table.caption -%> \caption{<%= ''.join(table.caption) %>\strut}<%= labels %>\\*[\sphinxlongtablecapskipadjust] -<% endif -%> \hline +<% elif labels -%> +\hline\noalign{\phantomsection<%= labels %>}% +<% else -%> +\hline +<% endif -%> <%= ''.join(table.header) %> \endfirsthead diff --git a/sphinx/templates/latex/tabular.tex_t b/sphinx/templates/latex/tabular.tex_t index 3fd347e53..26e4ff107 100644 --- a/sphinx/templates/latex/tabular.tex_t +++ b/sphinx/templates/latex/tabular.tex_t @@ -14,6 +14,8 @@ \sphinxcapstartof{table} \sphinxcaption{<%= ''.join(table.caption) %>}<%= labels %> \sphinxaftercaption +<% elif labels -%> +\phantomsection<%= labels %> <% endif -%> \begin{tabular}[t]<%= table.get_colspec() -%> \hline diff --git a/sphinx/templates/latex/tabulary.tex_t b/sphinx/templates/latex/tabulary.tex_t index 16d15192b..385076529 100644 --- a/sphinx/templates/latex/tabulary.tex_t +++ b/sphinx/templates/latex/tabulary.tex_t @@ -14,6 +14,8 @@ \sphinxcapstartof{table} \sphinxcaption{<%= ''.join(table.caption) %>}<%= labels %> \sphinxaftercaption +<% elif labels -%> +\phantomsection<%= labels %> <% endif -%> \begin{tabulary}{\linewidth}[t]<%= table.get_colspec() -%> \hline diff --git a/tests/roots/test-latex-table/expects/longtable_having_widths.tex b/tests/roots/test-latex-table/expects/longtable_having_widths.tex index d41a87586..505ae4d70 100644 --- a/tests/roots/test-latex-table/expects/longtable_having_widths.tex +++ b/tests/roots/test-latex-table/expects/longtable_having_widths.tex @@ -1,7 +1,7 @@ \label{\detokenize{longtable:longtable-having-widths-option}} \begin{savenotes}\sphinxatlongtablestart\begin{longtable}{|\X{30}{100}|\X{70}{100}|} -\hline +\hline\noalign{\phantomsection\label{\detokenize{longtable:namedlongtable}}\label{\detokenize{longtable:mylongtable}}}% \sphinxstyletheadfamily header1 &\sphinxstyletheadfamily @@ -43,3 +43,5 @@ cell3-2 \\ \hline \end{longtable}\sphinxatlongtableend\end{savenotes} + +See {\hyperref[\detokenize{longtable:mylongtable}]{\sphinxcrossref{mylongtable}}}, same as {\hyperref[\detokenize{longtable:namedlongtable}]{\sphinxcrossref{\DUrole{std,std-ref}{this one}}}}. diff --git a/tests/roots/test-latex-table/expects/table_having_widths.tex b/tests/roots/test-latex-table/expects/table_having_widths.tex index 914793181..5a69d5b22 100644 --- a/tests/roots/test-latex-table/expects/table_having_widths.tex +++ b/tests/roots/test-latex-table/expects/table_having_widths.tex @@ -2,6 +2,7 @@ \begin{savenotes}\sphinxattablestart \centering +\phantomsection\label{\detokenize{tabular:namedtabular}}\label{\detokenize{tabular:mytabular}} \begin{tabular}[t]{|\X{30}{100}|\X{70}{100}|} \hline \sphinxstyletheadfamily @@ -28,3 +29,5 @@ cell3-2 \end{tabular} \par \sphinxattableend\end{savenotes} + +See {\hyperref[\detokenize{tabular:mytabular}]{\sphinxcrossref{\DUrole{std,std-ref}{this}}}}, same as {\hyperref[\detokenize{tabular:namedtabular}]{\sphinxcrossref{namedtabular}}}. diff --git a/tests/roots/test-latex-table/longtable.rst b/tests/roots/test-latex-table/longtable.rst index 370226f21..bace9d4d2 100644 --- a/tests/roots/test-latex-table/longtable.rst +++ b/tests/roots/test-latex-table/longtable.rst @@ -18,9 +18,12 @@ longtable longtable having :widths: option -------------------------------- +.. _mylongtable: + .. table:: :class: longtable :widths: 30,70 + :name: namedlongtable ======= ======= header1 header2 @@ -30,6 +33,8 @@ longtable having :widths: option cell3-1 cell3-2 ======= ======= +See mylongtable_, same as :ref:`this one `. + longtable having :align: option ------------------------------- diff --git a/tests/roots/test-latex-table/tabular.rst b/tests/roots/test-latex-table/tabular.rst index b28add3d3..7f0909540 100644 --- a/tests/roots/test-latex-table/tabular.rst +++ b/tests/roots/test-latex-table/tabular.rst @@ -1,4 +1,4 @@ -taburar and taburary +tabular and tabulary ==================== simple table @@ -15,8 +15,11 @@ cell3-1 cell3-2 table having :widths: option ---------------------------- +.. _mytabular: + .. table:: :widths: 30,70 + :name: namedtabular ======= ======= header1 header2 @@ -26,6 +29,8 @@ table having :widths: option cell3-1 cell3-2 ======= ======= +See :ref:`this `, same as namedtabular_. + table having :align: option (tabulary) --------------------------------------