Skip to content

Commit 82736e0

Browse files
committed
LaTeX: fix #13635 (cell containing table turns row colors off)
1 parent 8eaa0ab commit 82736e0

File tree

1 file changed

+77
-14
lines changed

1 file changed

+77
-14
lines changed

sphinx/texinputs/sphinxlatextables.sty

Lines changed: 77 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
%% TABLES (WITH SUPPORT FOR MERGED CELLS OF GENERAL CONTENTS)
22
%
33
% change this info string if making any custom modification
4-
\ProvidesPackage{sphinxlatextables}[2024/07/01 v7.4.0 tables]%
4+
\ProvidesPackage{sphinxlatextables}[2025/06/09 v8.3.0 tables]%
55

66
% Provides support for this output mark-up from Sphinx latex writer
77
% and table templates:
@@ -42,6 +42,11 @@
4242
% - \sphinxthistablewithnocolorrowsstyle
4343
% - \sphinxthistablewithvlinesstyle
4444
% - \sphinxthistablewithnovlinesstyle
45+
% These conditionals added at 8.3.0 for nested tables not to break row colors
46+
% (#13635). Nested tables are only partially supported by Sphinx LaTeX.
47+
% The method here is with no changes to neither writer nor templates.
48+
\newif\ifspx@intable
49+
\newif\ifspx@thistableisnested
4550
%
4651
% Also provides user command (see docs)
4752
% - \sphixncolorblend
@@ -62,8 +67,8 @@
6267
\RequirePackage{tabulary}
6368
% tabulary has a bug with its re-definition of \multicolumn in its first pass
6469
% which is not \long. But now Sphinx does not use LaTeX's \multicolumn but its
65-
% own macro. Hence we don't even need to patch tabulary. See
66-
% sphinxpackagemulticell.sty
70+
% own macro. Hence we don't even need to patch tabulary.
71+
%
6772
% X or S (Sphinx) may have meanings if some table package is loaded hence
6873
% \X was chosen to avoid possibility of conflict
6974
\newcolumntype{\X}[2]{p{\dimexpr
@@ -109,7 +114,8 @@
109114
\LTpre\z@skip\LTpost\z@skip % set to zero longtable's own skips
110115
\edef\sphinxbaselineskip{\dimexpr\the\dimexpr\baselineskip\relax\relax}%
111116
\spx@inframedtrue % message to sphinxheavybox
112-
}%
117+
\spx@table@setnestedflags
118+
}
113119
% Compatibility with caption package
114120
\def\sphinxthelongtablecaptionisattop{%
115121
\spx@ifcaptionpackage{\noalign{\vskip-\belowcaptionskip}}{}%
@@ -124,7 +130,18 @@
124130
% B. Table with tabular or tabulary
125131
\def\sphinxattablestart{\par\vskip\dimexpr\sphinxtablepre\relax
126132
\spx@inframedtrue % message to sphinxheavybox
133+
\spx@table@setnestedflags
127134
}%
135+
% MEMO: this happens inside a savenotes environment and hence flags
136+
% are reset on exit of it.
137+
\def\spx@table@setnestedflags{% Issue #13635
138+
\ifspx@intable
139+
\let\spx@table@resetcolortbl\spx@nestedtable@resetcolortbl
140+
\spx@thistableisnestedtrue
141+
\else
142+
\spx@intabletrue
143+
\fi
144+
}%
128145
\let\sphinxattableend\sphinxatlongtableend
129146
% This is used by tabular and tabulary templates
130147
\newcommand*\sphinxcapstartof[1]{%
@@ -270,6 +287,9 @@
270287
% cells (the code does inserts & tokens, see TN1b). It was decided to keep it
271288
% simple with \sphinxstartmulticolumn...\sphinxstopmulticolumn.
272289
%
290+
% **** ATTENTION: Sphinx does generate at least some nested tables in LaTeX
291+
% **** TODO: clarify if next paragraph means we must raise an
292+
% **** if LaTeX writer detects a merged cell inside nested table.
273293
% MEMO about nesting: if sphinxmulticolumn is encountered in a nested tabular
274294
% inside a tabulary it will think to be at top level in the tabulary. But
275295
% Sphinx generates no nested tables, and if some LaTeX macro uses internally a
@@ -857,7 +877,32 @@
857877
}%
858878
\the\everycr
859879
}%
860-
\global\rownum\@ne % is done from inside table so ok with tabulary two passes
880+
\ifspx@thistableisnested
881+
% Attention that tabulary does two passes so we need to push the
882+
% initial rownum and, after the first pass, we must reset it!
883+
% Fortunately Sphinx LaTeX writer makes parent table tabular or
884+
% longtable if a nested table is a tabulary. So we don't need to
885+
% worry about distinguishing if this or parent is tabulary.
886+
\ifx\TY@final\@undefined % tabular
887+
\spx@gpush@rownum
888+
\else
889+
\ifx\equation$% tabulary, first pass
890+
\spx@gpush@rownum
891+
\else % tabulary, second pass
892+
\spx@gpop@rownum % reset \rownum
893+
\spx@gpush@rownum% and push it again.
894+
\fi
895+
\fi
896+
% To make nested tables stand out in a color row, we toggle the parity.
897+
% TODO: Double-check if compatible with method for color of header
898+
% row.
899+
% TODO: Perhaps better to use specific colors for nested tables?
900+
% This would mean though adding new sphinxsetup parameters
901+
% and extending the documentation...
902+
\ifodd\rownum\global\rownum\z@\else\global\rownum\@ne\fi
903+
\else
904+
\global\rownum\@ne
905+
\fi
861906
\sphinxSwitchCaseRowColor\rownum % set up color for the first body row
862907
\sphinxrowcolorON % has been done from \sphinxtoprule location but let's do
863908
% it again in case \sphinxtabletoprulehook has been used
@@ -883,20 +928,34 @@
883928
\let\sphinxtabledecrementrownum\@empty
884929
885930
% \sphinxtableafterendhook will be modified by colorrows class to execute
886-
% this after the table
931+
% this after the table.
887932
\def\spx@table@resetcolortbl{%
888933
\sphinxrowcolorOFF
889-
\spx@table@reset@CTeverycr
934+
\spx@table@reset@CT@everycr
890935
% this last bit is done in order for the \sphinxbottomrule from the "foot"
891-
% longtable template to be able to use same code as the \sphinxbottomrule
892-
% at end of table body; see \sphinxbooktabsspecialbottomrule code
936+
% part of the longtable template to be able to use same code as the
937+
% \sphinxbottomrule at end of table body; see \sphinxbooktabsspecialbottomrule.
893938
\global\rownum\z@
939+
\global\let\spx@rownum@stack\@empty
940+
}
941+
% Most of \spx@table@resetcolortbl must be avoided if the table is nested.
942+
% Besides the sphinxTableRowColor must be reset because it has been
943+
% redefined by the cells of the nested table. So this is the alternative
944+
% macro which is executed on exit of nested table.
945+
\def\spx@nestedtable@resetcolortbl{%
946+
\spx@gpop@rownum
947+
\sphinxSwitchCaseRowColor\rownum
894948
}
895-
\def\spx@table@reset@CTeverycr{%
949+
\def\spx@table@reset@CT@everycr{%
896950
% we should probably be more cautious and not hard-code here the colortbl
897-
% set-up; so the macro is defined without @ to fac
951+
% set-up.
898952
\global\CT@everycr{\noalign{\global\let\CT@row@color\relax}\the\everycr}%
899953
}
954+
\let\spx@rownum@stack\@empty
955+
\def\spx@gpush@rownum{\xdef\spx@rownum@stack{\the\rownum.\spx@rownum@stack}}%
956+
\def\spx@gpop@rownum{\afterassignment\spx@gpop@rownum@i
957+
\global\rownum=\spx@rownum@stack\relax}
958+
\def\spx@gpop@rownum@i.#1\relax{\gdef\spx@rownum@stack{#1}}
900959
901960
% At last the style macros \sphinxthistablewithstandardstyle etc...
902961
@@ -1047,10 +1106,13 @@ local use of booktabs table style}%
10471106
%
10481107
% this one is not set to \@empty by nocolorrows, because it looks harmless
10491108
% to execute it always, as it simply resets to standard colortbl state after
1050-
% the table; so we don't need an @@ version for this one
1109+
% the table [^1]; so we don't need an @@ version for this one.
1110+
% .. [1]: which is bad if nested in another table. This is taken care of
1111+
% at level of \sphinxattablestart and \sphinxatlongtablestart.
10511112
\spx@prepend\spx@table@resetcolortbl\to\sphinxtableafterendhook
10521113
}
10531114
\def\spx@prepend#1\to#2{% attention about using this only with #2 "storage macro"
1115+
% MEMO: #1 is prepended with no expansion, i.e. "as is".
10541116
\toks@{#1}%
10551117
\toks@\expandafter\expandafter\expandafter{\expandafter\the\expandafter\toks@#2}%
10561118
\edef#2{\the\toks@}%
@@ -1064,9 +1126,10 @@ local use of booktabs table style}%
10641126
\let\spx@table@startbodycolorrows\@empty
10651127
\let\sphinxtabledecrementrownum \@empty
10661128
% we don't worry about \sphinxtableafterendhook as the \spx@table@resetcolortbl
1067-
% done at end can not do harm; and we could also have not bothered with the
1129+
% done at end can not do harm [^1]; and we could also have not bothered with the
10681130
% \sphinxtabledecrementrownum as its \rownum decrement, if active, is harmless
1069-
% in non-colorrows context
1131+
% in non-colorrows context.
1132+
% .. [1]: if nested in another table it is modified to do no harm.
10701133
}
10711134
10721135
% (not so easy) implementation of the booktabscolorgaps option. This option

0 commit comments

Comments
 (0)