-
-
Notifications
You must be signed in to change notification settings - Fork 18k
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
ENH: Styler.to_latex() #21673
Comments
Would really appreciate this feature, thanks for making this issue |
Any updates? |
+1 for this feature |
+1 |
1 similar comment
+1 |
Does this enhancement aim to solve issues like this: |
Not sure what happens when you convert a notebook to pdf... but yes, it might be that |
+1 |
2 similar comments
+1 |
+1 |
Please, this isn't helpful - see here for how to contribute, else wait for someone else to do it, but carrying on commenting |
(and no more effective than adding a simple "thumbs up" on the first comment) |
That branch is currently 8352 commits behind. Maybe you should try to rebase and open a PR, to see if it catches the attention of the maintainers. |
Who's "you"? If it's me, well, I am a maintainer, and that branch already catched my attention, but as of now it didn't result in me doing anything more :-) Jokes apart, @cauebs feel free to try to rebase - not sure if it will help merging this eventually, but at least it will help making sure no changes in the last one and a half years broke my approach. In any case, as I wrote, that branch wasn't and wouldn't be ready for a PR. |
I would really like to see this added to Pandas! What are the minimum requirements to get this merged? What would a sensible test case look like? I don't think that it needs to support all of the current functionality before being merged, colored background and bold cells would be enough, imho. |
I think that more than any set of features, it is just doing it "right", which means
I think a PR satisfying these two points could be perfectly mergeable even with a really minimal set of supported formatting features. |
What is the problem with conversion to str? What happens in (Sorry if this is obvious from the code, I didn't have a look yet. I'm just trying to get a feeling for the complexity of the problem.) |
It's explained in my first comment ;-) In addition, it's an obvious code duplication, since the process of converting cells content to strings is already implemented for |
@toobaz Your branch also adds a |
If I understand correctly, this is what we currently have:
|
Yes, I think the technique to be used for |
@attack68 OK, I understand your points.
This looks really great! |
So this is what I did at first actually just following the outline, but I quickly realised that:
If you adopt the format I have provided it is easy to add in a patch that does what you want, i.e.:
where your function needs to convert say Edit: I would rather leave this out for a basic PR, and maybe add into the functionality afterwards as a separate component, |
For the defaults (bold, italics, colored cell) it is pretty straight forward. (italics is textit; textsl is slanted, emph is semantic markup that's commonly rendered as italic, so no problem here). Moreover, some commands always have to be applied in a certain order. I'm all for the possibility of using custom LaTex commands, but I don't think this is the right way. Maybe @toobaz can give his opinion about this? |
This could be a good idea. However, if it is released this way, some solutions are infeasible in the future, if we don't want to break the API. Would it be sensible to prefix latex-specific CSS attributes with a "vendor prefix", like Also, there has to be a better way to define how the markup is applied. |
I would love to! But I'm confused. My understanding is that you are discussing whether the API should accept formatting in the format of css or of (arbitrary) LaTeX command (and I think I favor the former, and consider the latter a welcome extension). But then
... suggests that @attack68's current code already accepts both? |
My code currently uses latex structured in Styler's CSS format, i.e ('attr','value') tuple, basically forming a ('command',options') latex pattern. It currently expects that all input is in latex format. However, if it were given in CSS an optional converter could then convert this to the latex format for post-processing.
This is a long way from release, but what solutions are infeasible? I believe that using a CSS map to Latex commands is far more restrictive and prohibitive, than what I have offered. For example,
I have provided two variants: More variants are easy to add in but at the risk of creating a parsing language. @moi90 how would you be accounting for these variants without introducting your own parsing language in a pure CSS transformation solution? |
Previous discussion about this considered making two separate classes: This PR is essentially creating a To provide a translator between a To allow a But this last step might be considered too esoteric and not worth inclusion by developers? maybe not. |
I'm really not happy with the weird syntax to squeeze LaTeX command into CSS... It gets worse, if a user wants to use Here, I made a demonstration which steps are required to format tables correctly when using
It would be really annoying to get this using the proposed CSS syntax.
While flexibility is great, I think it is more important to cover the common cases with ease (background-color, color, font-weight: bold, font-style: italic). (Also, it will not be very hard to extend the existing protected and unprotected formatting commands.) |
@moi90 allowing You should not eliminate all of this flexibility, for the sake of covering the common cases, which I have said are already very easy to translate into the LaTeX format. See this unpublished PR (not included in my original PR because this is an extension which just complicates the PR reviewers) So far the parameters that you have raised that have been solved are:
Very much this discussion has followed the line of you challenging my development's capabilities. This has been valuable since it has driven the development of these options. However, without a suitable challenger model it is difficult for me to question how you plan to deal with some of these items you have raised and others, if you still believe that working with a LateXFormatter is preferable (I don't)?
Here is an example of the extension module from the above PR: |
@attack68 I greatly appreciate your skills as a developer and am impressed that you have always skillfully addressed my "challenges". 👍 I'm sorry I haven't come up with a challenger model. I tried to dig into the existing code from multiple angles but I currently lack the time to get to a solution that would worth discussing. Also, you convinced me that using Jinja is not so bad after all.
I don't see how a translation between CSS properties and LaTeX commands would lead to more code duplication or less maintainability.
Maybe we can have an additional option for
Maybe we can drop
I answered here in detail. Basically: Always be compatible with
I would make this a problem. On the CSS side, there is large, x-large, xx-large, and xxx-large. On the LaTeX side, there is \large, \Large, \LARGE, \huge, \Huge. These can be matched nicely (when excluding \Huge). (Same for the small sizes.)
Cool! (Albeit not yet |
if anyone would like to review / try out / comment on the PR #40422 would be appreciated. |
As an end user, when I have a Styler object that renders fine while viewing in a notebook, but exported to PDF it becomes class display (....Styler), do I need to explicitly call .toLatex() or will that be done behind the scenes? Thank you for working on this issue! |
Could you please provide some more advanced examples in documentation of usage of to_latex()? |
what, specifically , would you like to see? |
Building more complex tables, with multicols and multirows, other Latex elements such as \cmidrule and if possible (can't understand from the documentation if that's possible or not) if there's way to include logic in the table building (e.g. include \midrule after a specific row, etc.). I have to publish lot's of tables in LateX, but I'm unable to explore all possibilities with the current examples. |
You need a MultiIndex and use the
No you cant add conditional out of cell logic. The only custom commands you can add are as described in the docs page, akin to the example for |
Thanks for your feedback. But for a novice user like me, it's been impossible to make it work from the examples (both in Styler.to_latex() and Dataframe.to_latex()). |
For that, I usually split the generated output into individual lines, insert the extra rules at pre-defined locations and concatenate the result. This breaks easily but it is better than nothing. |
|
(Sorry to put this here, but I'm finding several questions on StackOverflow (e.g. this or this) unanswered, and my time is running out, so I bring this question into where I know there is knowledge to solve this. Please excuse me) why Styler.to_latex() does not produce the same outputs (namely the My original dataframe after aggregating results (
Then I use the following code:
Output from Styler.to_latex() (Latex Code and Image)
Output from DataFrame.to_latex() (Latex Code and Image)
Questions:
I tried to do
but I was unsuccessful. Is there any way to accomplish this type of control (e.g. force \midrule between multirows)?
|
pandas is a volunteer library. cline is not (yet) implemented in Styler.to_latex, no one has volunteered the time to develop it. toprule, midrule, and bottomrule will not help you here. no, datacell merging is not possible. not sure what you mean by "two lines" but irrespective i am confident the fearures you are looking for have not been developed. |
@attack68: Just to be clear I know that pandas is a volunteer library and I'm extremely grateful for pandas!! I was just checking if there was any option to accomplish what I wanted. |
those lines are the toprule and midrule. they are visible in both DF and Styler version. In Styler version you can take them both away with hrules=False. you can also take just the midrule away by adding table styles for toprule and bottomrule and not including the midrule (and keeping Hrules=false) |
@attack68 : sorry, I misguided you: I understand what you explained about the lines, but what I meant was "is there any way I can put the header in one row (instead of 2 rows) without losing the ability of the multiindex grouping under the "dataset" column? |
I have created a branch adding a
to_latex
method toStyler
.It is nowhere near readiness, and in particular:
.astype(str)
, which is plain wrong because it discards features such as float formatting. For a good result, a better integration is needed with the code that currently does cell formatting inDataFrame.to_latex
.This said, if anyone feels like experimenting, it might be slightly better than starting from scratch.
One aspect which I think is useful, even aside from this branch, and might benefit some discussion, is the pair of methods
_latex_preserve
and_latex_restore
, which basically replace LaTeX commands so that they are not disturbed by escaping, and then restores them. There might be better way to code this, but I really think this is something we need to implement, and to offer to users who happen to nest LaTeX code in their cells.The text was updated successfully, but these errors were encountered: