-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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: support for booktabs-style and zebra-striped tables #10759
Conversation
Render tables without vertical rules and horizontal rules of varying thickness (with additional space above and below) using the booktabs package. This is a rebase of original commit which was on master branch prior to 2.1 release. In this rebase the \RequirePackage{booktabs} has been removed from sphinx.sty, as it has to be conditional on latex_booktabs (or whatever its name will be) configuration option, and will be repositioned in a subsequent commit.
Render tables with alternating background colors for even and odd rows (so called "zebra striping").
0561b76
to
fae68d2
Compare
In order to illustrate the effect of this PR I am adding here snapshots from our own document, using:
and the default colours activated by Now a long table with a tabularcolumns directive. on next page: The effect was obtained via this >{\sphinxcolorblend{!95!red}\centering\noindent\bfseries\color{red}}\Y{.12} as configuration for the third column. It works here because my TeX installation has the LaTeX xcolor.sty. On user devices with xcolor.sty not installed (I don't know actual situation but many years ago this was not automatic in Linux distros), the Now a complex grid table with a custom tabularcolumns (with no vertical lines) .. tabularcolumns:: >{\sphinxcolorblend{!90!blue}}TTT>{\columncolor{yellow}\sphinxnorowcolor}TT and booktabs style
It illustrates various features. The conf.py is:
where the colours have been chosen at random (and test the new acceptance of One sees in this table:
|
The latest (now squashed) commits in this PR fix some issues with merged cells which were not yet documented as such: when a tabularcolumns directive uses no |
again one of those transient test_build_linkcheck.py failures... https://github.com/sphinx-doc/sphinx/runs/7821615851?check_suite_focus=true which are not related to this PR... |
This is a combination of 28 commits... - Simplify a bit a conditional in the longtable template This also puts the target for a longtable with a label but no caption above the toprule for better hyperlinking (testing shows hyperlink target can not end up alone at bottom of previous page). - Refactor and trim doc about LaTeX tables... to make room for more, later - Enlarge allowed syntax for colour assignments via 'sphinxsetup' - latex_table_style new configuration value and coloured rows For the user interface tried to look for inspiration in https://docutils.sourceforge.io/docs/user/config.html#table-style which mentions booktabs and borderless. They also mention captionbelow which we can implement later, now that architecture is here. They don't mention coloured rows. - Test on our own document... looks fine! - Update LaTeX table tests and templates Modify longtable templates to put LaTeX macros each on its line Table body insertion without removing previous EOL may give output which contain some empty lines but longtable defines \par token to be same as \empty. - Work-around an incompatibility of \cline with row colours, improve docs - Reverse priority of classes to allow overruling booktabs by standard after parsing source but before letting LaTeX writer act - Closes sphinx-doc#8220 Commit sphinx-doc@bb859c6 already improved a bit, this finishes it (as :rst:dir:`rst-class` was actually not linking to anywhere). - Update CHANGES for PR sphinx-doc#10759 - Let booktabs style defaults to *not* using \cmidrule. They actually don't make much sense there, as all \hline's are removed. - Enhance customizability at LaTeX code level (via code executed prior to table rendering, e.g. from a container class environment). - Patch booktabs \cmidrule, as if it used via \sphinxcline, there is a vertical space problem in case of there are two in the same row due to booktabs \futurelet not knowing \sphinxcline - Add \sphinxnorowcolor which allows construct such as this one in a tabularcolumns directive: >{\columncolor{blue}\sphinxnorowcolor} else LaTeX always overrides column colour by row colour - Add TableMergeColorHeader, TableMergeColorOdd, TableMergeColorEven so single-row merged cells can be styled especially - Extend row colours to all header rows not only the first one (all header rows will share same colour settings) - Auto-adjust to a no '|'-colspec for optimal handling of merged cell - Add \sphinxcolorblend - Needed to also detect if a '|' is in tabularcolumns, tests updated - Fix refactoring in this series which broke table.colsep update - Add test which would have shown regression regarding table.colsep - Fix another regression regarding \sphinxcline + booktabs Can not add test for that, because it shows only after PDF build. - Final testing and code comments update
This is a combination of 28 commits... - Simplify a bit a conditional in the longtable template This also puts the target for a longtable with a label but no caption above the toprule for better hyperlinking (testing shows hyperlink target can not end up alone at bottom of previous page). - Refactor and trim doc about LaTeX tables... to make room for more, later - Enlarge allowed syntax for colour assignments via 'sphinxsetup' - latex_table_style new configuration value and coloured rows For the user interface tried to look for inspiration in https://docutils.sourceforge.io/docs/user/config.html#table-style which mentions booktabs and borderless. They also mention captionbelow which we can implement later, now that architecture is here. They don't mention coloured rows. - Test on our own document... looks fine! - Update LaTeX table tests and templates Modify longtable templates to put LaTeX macros each on its line Table body insertion without removing previous EOL may give output which contain some empty lines but longtable defines \par token to be same as \empty. - Work-around an incompatibility of \cline with row colours, improve docs - Reverse priority of classes to allow overruling booktabs by standard after parsing source but before letting LaTeX writer act - Closes sphinx-doc#8220 Commit sphinx-doc@bb859c6 already improved a bit, this finishes it (as :rst:dir:`rst-class` was actually not linking to anywhere). - Update CHANGES for PR sphinx-doc#10759 - Let booktabs style defaults to *not* using \cmidrule. They actually don't make much sense there, as all \hline's are removed. - Enhance customizability at LaTeX code level (via code executed prior to table rendering, e.g. from a container class environment). - Patch booktabs \cmidrule, as if it used via \sphinxcline, there is a vertical space problem in case of there are two in the same row due to booktabs \futurelet not knowing \sphinxcline - Add \sphinxnorowcolor which allows construct such as this one in a tabularcolumns directive: >{\columncolor{blue}\sphinxnorowcolor} else LaTeX always overrides column colour by row colour - Add TableMergeColorHeader, TableMergeColorOdd, TableMergeColorEven so single-row merged cells can be styled especially - Extend row colours to all header rows not only the first one (all header rows will share same colour settings) - Auto-adjust to a no '|'-colspec for optimal handling of merged cell - Add \sphinxcolorblend - Needed to also detect if a '|' is in tabularcolumns, tests updated - Fix refactoring in this series which broke table.colsep update - Add test which would have shown regression regarding table.colsep - Fix another regression regarding \sphinxcline + booktabs Can not add test for that, because it shows only after PDF build. - Final testing and code comments update
I have added in my clone at jfbu@abab0bf an extra commit in order for booktabs + colorrows to give tables with no "white gaps". Here are some snapshots of what it produces in our own document: edit: I took the snapshots at a time the "night-shift no blue waves" had already ticked-in... so this might change a bt the colours... This one has a problem because the small gap underneath the top rule is filled with the default colour for headers (slighly darker gray than the one for odd rows). To fix this is a bit tough on macro side because it will need Also this one has a problem, because we see that the column color tint does not appy. For it to apply we would have to use an altogether different approach than using the Besides globally on zooming on pictures I think I am seeing anti-aliasing which might be a sign there is some extra space I did not take into account, andI would need to dig into So this is bit problematic (when I started writing this comment and took the snapshots I was not aware of all problems). update 1: the problem with colour for tables with no header has been fixed at jfbu@765ccda. And regarding zooming on the pictures I should rather have zoomed on the PDFs and this looks fine although it seems either my eyes or the PDF viewer does anti-aliasing at intermediate zoom levels but this depends on colours. to be honest, zooming at high levels on tables reveals the effet of the One wonders after colorizing the gaps why keep the black rules at all... and why LaTeX produces generally so cramped tables. (LaTeX's update 2: I have continued at https://github.com/jfbu/sphinx/tree/latex_booktabs_nowhitegap to polish some details. However there is a problem for which I feel there will not be easy solution (probably needed to hack longtable): the |
This avoids at high level of zooming, depending on PDF viewers a line of non-coloured or anti-aliased pixels at the joining of two rectangles of same colour due to rounding effects. Nota Bene: the background colour panels extend a tiny bit beyond left and right limits of table due to another parameter used for similar reasons motivated by horizontally merged cells: \sphinxcolorpanelextraoverhang (also defaulting to 0.1pt) Relates sphinx-doc#10759 This is a combination of 4 commits - some code comments for future edit if this is to be used upstream - safer usage of \spx@table@crazyfork in case #1 turns out to be \par - add \sphinxifthistablewithcolorrowsTF to simplify coding - \sphinxbooktabsspecialruleoverhang for perfect same-colour-joins
edited (of course...) In one of the longtable snapshots of #10759 (comment), one sees that header colour is applied to the table continuation hints. But this was an oversight. In first phase of development of this PR I noticed things such as: With the extra patch now pushed one obtains for example on top of a continuation page in our own document: and at bottom of a page for the same long longtable of Deprecated APIs: About this question from earlier version of this comment:
It has to be not only the default, but with no added customizing option. |
One reason in particular is that the colour is applied only up to the long table width, but the continuation hints may be larger than this width (but this width is hidden to not influence the table cells layout).
This way, custom user replacement code for \sphinxbooktabs{top,mid,bottom}rule is relatively painless to inject in preamble or via container class. Also, it was needed to add some LaTeX code to the longtable template in order to avoid a colour-related problem with \sphinxbottomrule. The 'sphinxsetup' key now named booktabscolorgaps defaults to being true and is left only as a way to provide an easy "turn-off" if the issue with the longtable colours after the mid rule and above the bottom rule is blocking to some. But a priori I envision at 6.0.0 to simply remove this undocumented option as the said issue is not serious and more to be considered a fact-of-life. Besides using booktabs+colorrows is strictly opt-in at this time. I considered various LaTeX ways around this longtable thing (it constructs headers and footers once and for all, one can not let them depend on some "dynamic" context; there is no easy way for a body row to know it is first on the page after header, and anyway would require adding mark-up or considerably complicate the \everycr)) but none of them are even remotely reasonable. Also, a non-documented booktabsnogaps 'sphinxsetup' key has been added: it suppresses all the extra spacing booktabs injects around its top, mid, and bottom rules. Will solve the booktabs+colorrows+longtable conundrum from previous paragraph.
I have now merged the separate work on colorizing booktabs gaps mentioned in earlier comments so that it is done per default and tables look as in #10759 (comment). I consider now it is better indeed to colorize these gaps if booktabs+colorrows is chosen by user conf.py, and the longtable aspects should not be considered blocking. This brings to an end (? I was already saying PR was ready 10 days ago...) my work on this... as my Sphinx+LaTeX time will now be more scarce in coming months I wanted to finish a coherent whole. |
This is better not to overload our template. Besides it makes it easier for user to use another longtable template.
Two short notes:
|
@AA-Turner Although the various comments I added when I was actively working on this may give a frightening impression, this PR is completed and I am not planning any further change... it adds opt-in features only (some are tested in our own document via a slightly modified conf.py). We can wait to see if @tk0miya has comments. For 6.0.0 release I would like to make some configuration the default, so that tables look better in LaTeX. |
If no objection I will merge this in 5.x in a few days, so that it makes it to 5.3.0. The changes are opt-in only. For 6.0.0 I will propose probably to make some of it the new default for rendering tables in LaTeX. Edit: In view of #10896 I will merge this (if no objection) tomorrow October 6th, around 8PM European time ;-) Ah edit again, release is actually planned for the next week=end, so I will wait until Wednesday October 12th... |
Remove in passing a note from latex.rst which was of no real use to Sphinx LaTeX users.
now merging a few hours earlier than announced due to personal constraints alea jacta est ;-) real activation will be for 6.0 |
@jfbu Thank you very much for your hard work to bring these features upstream! :-) |
@sephalon sorry I messed up in the squashed merge and the git commit lost your co-authorship... and I can not force push to 5.x... |
This transfers here development from #6666 to continue it and merge with #6671.
latex-
prefixed names to dispel user expectation that it also applies to say html build.Actually using a container directive is possibly better as on LaTeX side it will wrap in a scope limiting environment. Maybe for first installment we can simply advice using some such recipe (container directive plus raw latex) which does not need extra code in Sphinx itself.finally I used classes with no
latex-
prefix even though they also may influence html output, but this sounds actually goodremoved longish paragraph which was in part obsoleted and too complex to edit; in brief all changes are opt-in except that
for merged cells in a single row the row colour (or the dedicated merge row colour) is now obeyed, so people who added manual usage of
\rowcolor
to their documents had only white merged cells but now the merged cell in single row (only) will react to row colour.for people who added manual usage of
\rowcolors
command to their document, the effect will be overwritten by this PR. Sphinx does not itself use\rowcolors
due to the latter limitations (and the fact that not all people have xcolor). Sphinx intervenes via its table templates and own code to influence the row colours, and this will overrule effect of manually inserted\rowcolors
. I don't know if this means the PR should go to master rather than 5.x.