Skip to content

Instantly share code, notes, and snippets.

@moi90
Last active April 12, 2021 09:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save moi90/fe88ca891bcdc069044255425fea3078 to your computer and use it in GitHub Desktop.
Save moi90/fe88ca891bcdc069044255425fea3078 to your computer and use it in GitHub Desktop.
\documentclass{article}
\usepackage{siunitx}
\usepackage{booktabs}
\usepackage[table]{xcolor}
\usepackage{etoolbox}
% Individual \itshape or \bfseries work without this.
% Combinations of both need the robustified version
\robustify\itshape
\robustify\bfseries
\begin{document}
% Required so that siunitx can detect \bfseries, \itshape, etc
\sisetup{detect-all = true}
\begin{table}[t]
\centering
\caption{\texttt{siunitx S} columns require a special treatment to format them correctly.}
\begin{tabular}{SS}
\toprule
% Non-number headings require braces:
{Heading A} & {Heading B} \\
% Fails with an error: Heading A & Heading B \\
\midrule
1.00 & 2.00 \\
% This is the only way to correctly color and format cells for siunitx:
{\cellcolor[gray]{0.8}} \bfseries 10.0 & \bfseries\itshape 20.0\\
100.000 & 200.000 \\
% color
\color{red} 1000.00 & \color{green} \bfseries 2000.00\\
\multicolumn{2}{c}{(Cells aligned at decimal dot)}\\
\midrule
10.0 & 20.0 \\
% \cellcolor does not work without enclosing braces: Wrong alignment
\cellcolor[gray]{0.8} \textbf 10.0 & \bfseries \itshape 20.0 \\
\multicolumn{2}{c}{(Second 10.0 is wrongly aligned,}\\
\multicolumn{2}{c}{formatting is only applied to first digit.)}\\
\midrule
10.0 & 20.0 \\
% \textbf and \textit do not work: Wrong alignment
{\cellcolor[gray]{0.8}} \textbf{10.0} & \textit{20.0}\\
\multicolumn{2}{c}{(Second 10.0 and 20.0 are wrongly aligned)}\\
\bottomrule
\end{tabular}
\end{table}
\begin{table}[t]
\centering
\caption{Formatting also works with regular \texttt{c} columns}
\begin{tabular}{cc}
\toprule
% Non-number headings require braces:
{Heading A} & {Heading B} \\
% Fails with an error: Heading A & Heading B \\
\midrule
1.00 & 2.00 \\
% This is the only way to correctly color and format cells for siunitx:
{\cellcolor[gray]{0.8}} \bfseries 10.0 & \bfseries \itshape 20.0\\
100.000 & 200.000 \\
\color{red} 1000.00 & \color{green} \bfseries 2000.00\\
\bottomrule
\end{tabular}
\end{table}
\end{document}
@moi90
Copy link
Author

moi90 commented Mar 26, 2021

Preview

@attack68
Copy link

Questions:

  1. what is the purpose of this?
    \usepackage{etoolbox}

  2. Why are you using:

\robustify\itshape
\robustify\bfseries

why not adopt the textit and textbf? I am getting some info from https://tex.stackexchange.com/questions/8053/is-there-a-difference-between-textit-and-itshape#:~:text=%5Citshape%20is%20a%20declaration.,textit%20affects%20only%20its%20argument.

  1. (Perhaps this answers 2) Is the user expected to already be using this if they generate a table form LaTeX?
    \sisetup{detect-all = true}

  2. Are you saying that in all cases putting headings in curly braces allows them to display in properly, whereas this is a requirement for siunitx? I.e the conclusion is that headings should always be in curly braces?

% Non-number headings require braces:
        {Heading A} & {Heading B} \\
  1. Actually I think that placing braces around the items (which is how a part of my development works currently) still renders:
% This is the only way to correctly color and format cells for siunitx:
        {\cellcolor[gray]{0.8}} \bfseries 10.0 & \bfseries \itshape 20.0\\
        100.000 & 200.000 \\
        \color{red} 1000.00 & \color{green} \bfseries 2000.00\\

and

        {\cellcolor[gray]{0.8}} {\bfseries 10.0} & {\bfseries {\itshape 20.0}} \\
        {100.000} & {200.000} \\
        {\color{red} 1000.00} & {\color{green} {\bfseries 2000.00}}\\

are equivalent - do you agree?

@moi90
Copy link
Author

moi90 commented Mar 29, 2021

First rule for siunitx: Everything enclosed in braces (including parameters to commands) is treated as text (which leads to improper alignment of numbergs). This answers 2., 4. and 5.

  1. etoolbox provides robustify.
  2. itshape and bfseries have to be robustified so that they can be properly detected by siunitx. Without robustification, combinations of both lead to wrong results. A user has to do this by themself, preferrably in the preamble. (But it seems not to be harmful if this is repeated multiple times.)
  3. Yes. A user has to setup siunitx in a way that it is able to detect the formatting if they want to use formatted numbers.
  4. Exactly: It is required for siunitx and does no harm in other settings. Therefore, it should be the default.
  5. Yes, it renders, but no, they are not equivalent:
    Rendered LaTeX
    The braces lead siunitx to treat the numbers as text, therefore they are not aligned at the decimal point anymore.

@attack68
Copy link

@moi90 OK this is really helpful (and I an learning something).

In order for users to be able to use the df.to_latex render we should keep track of which packages are necessary, so that we can add it to properly document, here it seems like we need:

 \usepackage{siunitx}  % only needed if using 'S' columns
\usepackage{booktabs}  % for table rules?
\usepackage[table]{xcolor}  % for colors
\usepackage{etoolbox}  % for robustify
\robustify\itshape  % for use with siunitx
\robustify\bfseries  % ...
% Required so that siunitx can detect \bfseries, \itshape, etc
\sisetup{detect-all = true}

I have also made reference to multirow and multicol if using a multindex, though your multicolumn works without an additional package.. Hmm I need to check this..

Ideally, I think it is best to aim for the minimal amount of preamble the user should add for compatible latex tables.

This leads to another question also. I interpret your push for the compatibility with siunitx package since it is probably quite a well adopted package commonly used. However, how do I know that this is true and how do I know that there are also not other packages which are equally commonly used that might require the latex structured in a slighlty different way to be compatible?

If we provide, perhaps, specific options for siunitx will there be other packages that users might be requesting specific compatability with? Should this be tolerated, or even a design objective?

@moi90
Copy link
Author

moi90 commented Mar 29, 2021

None of this is necessary, if a user is not using siunitx. booktabs is strongly recommended (e.g. here or here) in any case to make pretty tables and is already required by DataFrame.to_latex.

If Styler.to_latex gains a siunitx: bool flag to render numerical columns as S, this could also introduce a comment in the generated output to remind the user that they has to include these packages and and options.

I have also made reference to multirow and multicol if using a multindex, though your multicolumn works without an additional package.. Hmm I need to check this..

AFAIK, multicol has nothing to do with tables, it provides multiple text columns per page. multirow is required, see DataFrame.to_latex.

Ideally, I think it is best to aim for the minimal amount of preamble the user should add for compatible latex tables.

I agree.

This leads to another question also. I interpret your push for the compatibility with siunitx package since it is probably quite a well adopted package commonly used. However, how do I know that this is true and how do I know that there are also not other packages which are equally commonly used that might require the latex structured in a slighlty different way to be compatible?

To the best of my knowledge, siunitx is the de-facto standard to format numbers in LaTeX. A quick Google search yielded nothing comparable (except for the predecessor siunit). For all other commands that might be used in a table, it shouldn't make a difference if something is between braces or not.

Generally, if a user wants to use siunitx, they will know that they needs to use extra commands.

If we provide, perhaps, specific options for siunitx will there be other packages that users might be requesting specific compatability with? Should this be tolerated, or even a design objective?

I can't predict what might be requested :) tabularx/tabulary could be candidates (but I personally will not endorse them). booktabs and multirow are already required.

@attack68
Copy link

attack68 commented Mar 29, 2021

You made a comment about doing away with .render(latex=True) and just using to_latex. I think this has some merit so I will investigate that option - there might be some hidden consequences, though.

If Styler.to_latex gains a siunitx: bool flag to render numerical columns as S, this could also introduce a comment in the generated output to remind the user that they has to include these packages and and options.

I would prefer well documented treatment of siunitx=True option rather than to produce a permanent comment. I firmly believe in the user understanding the use of options, if they elect to turn them on.

AFAIK, multicol has nothing to do with tables, it provides multiple text columns per page. multirow is required, see DataFrame.to_latex.

I read somewhere it was required for something, but I can't quite remember exactly what for, need to test this out.

Ok so I now have the development goals:

  • adjust my original PR to expand the --wrap options so that 'the LaTeX Styler' has complete flexibility to render anything in any braces format with a LaTeX only command-options format (needed for structuring siunitx).
  • make the default for adding braces around headers in my original PR.
  • consider removing .render(latex=True) and relying solely on .to_latex()
  • analyse how to alter code for siunitx format with a boolean, in just the CSS conversion routine.

Question to you:

If I default CSS conversion to use \textbf and \textit in non-siunitx and use \itshape and \bfseries with siunitx is there any benefit? should I be aware of any differences between the two?

@attack68
Copy link

attack68 commented Mar 30, 2021

Screen Shot 2021-03-30 at 08 05 49

I needed to add \sisetup{detect-all = true}

@moi90
Copy link
Author

moi90 commented Mar 30, 2021

This looks really nice!

If I default CSS conversion to use \textbf and \textit in non-siunitx and use \itshape and \bfseries with siunitx is there any benefit? should I be aware of any differences between the two?

I don't think there would be any benefit. Apart from "italic correction" (done by \textit but not needed in the context of tables) there shouldn't be any difference (see e.g. here). As my example shows, the combination of bold and italic also works in both cases.

I needed to add \sisetup{detect-all = true}

Yes.

The only thing left is the automatic choice of the S column type. As far as I can see, the siunitx parameter influences nothing else apart from the column format.

@attack68
Copy link

I don't think there would be any benefit. Apart from "italic correction" (done by \textit but not needed in the context of tables) there shouldn't be any difference (see e.g. here). As my example shows, the combination of bold and italic also works in both cases.

OK so it seems like \itshape and \bfseries play nicely together in the default case, and with siunitx they need robusity preamble which needs to be documented.

I needed to add \sisetup{detect-all = true}

We can document this also.

The only thing left is the automatic choice of the S column type. As far as I can see, the siunitx parameter influences nothing else apart from the column format.

Yes it seems the same braces formatting that is compatible with siunitx is also capable of rendering without it, so this should be the default in the CSS converter.

@moi90
Copy link
Author

moi90 commented Mar 30, 2021

I agree 👍

@attack68
Copy link

attack68 commented Apr 1, 2021

Pull requests 40422 and 40721 are now actively submitted. feel free to review them and to encourage core devs to go through them to get them approved.

@moi90
Copy link
Author

moi90 commented Apr 6, 2021

Great!

#40721 is likely spelling mistake. Which one do you really mean?

@attack68
Copy link

attack68 commented Apr 6, 2021

40731

@moi90
Copy link
Author

moi90 commented Apr 12, 2021

Thanks! I subscribed to the pull requests. Is there anything I can do to speed up the review process?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment