Skip to content

Missing _repr_latex_ method for latex/pdf conversion in jupyter notebooks #11778

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

Merged
merged 1 commit into from
Dec 19, 2015

Conversation

bkasel
Copy link
Contributor

@bkasel bkasel commented Dec 6, 2015

Hello, this is my very first pull request on github. I added latex/pdf conversion capabilities to the dataframe when used in a Jupyter/Ipython notebook. The method simply uses the existing to_latex method of the Dataframe class. I believe teh following Issues or PR have already attempted this, but they seem closed: #11399 and #9821

The repr_latex method looks for newly created options:
- display.latex_escape
- display.latex_longtable
which are passed as arguments to the to_latex method.

a simple test has been added to check if the conversion works. However, if the
implementation of the to_latex method changes, the test function would also have to be updated.

@@ -1556,6 +1565,7 @@ def to_latex(self, buf=None, columns=None, col_space=None, colSpace=None,
Use a longtable environment instead of tabular. Requires adding
a \\usepackage{longtable} to your LaTeX preamble.
escape : boolean, default True
escape : boolean, default True
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you are duping here

@jreback jreback added the IO LaTeX to_latex label Dec 7, 2015
@jreback jreback added this to the 0.18.0 milestone Dec 7, 2015
@jreback
Copy link
Contributor

jreback commented Dec 7, 2015

pls add a whatsnew note, otherwise looks pretty good.

@bkasel
Copy link
Contributor Author

bkasel commented Dec 7, 2015

I will make the changes this evening. However, I do not know exactly where to add the whatsnew note. Do I simply edit the pandas/doc/source/whatsnew/v0.18.0.txt file?

@jreback
Copy link
Contributor

jreback commented Dec 7, 2015

yep

@@ -20,7 +20,9 @@ Check the :ref:`API Changes <whatsnew_0180.api>` and :ref:`deprecations <whatsne
New features
~~~~~~~~~~~~


- The Dataframe class got a _repr_latex_ method in order to allow for automatic conversion to latex in
a ipython/jupyter notebook using nbconvert. Options `display.latex.escape` and `display.latex.longtable`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use double backticks around display.latex.escape (and also around DataFrame._repr_latex_)

@bkasel
Copy link
Contributor Author

bkasel commented Dec 11, 2015

I made the changes. I did not add example code for the Jupyter notebook, because the changes are only visible when the notebook is converted to latex.

Returns a LaTeX representation for a particular Dataframe.
Mainly for use with nbconvert (jupyter notebook conversion to pdf).
"""
is_escaped = get_option("display.latex.escape")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these options should instead be in .to_latex (replace the current defaults with None, then use these)
The user can override or use the default options as the defaults.

update the .to_latex doc-string as well

@jreback
Copy link
Contributor

jreback commented Dec 16, 2015

@bkasel can you update according to comments

@bkasel
Copy link
Contributor Author

bkasel commented Dec 16, 2015

Yes, I will do asap. I don't have a lot of free time right now. (Hopefully the WE will be better)

@bkasel
Copy link
Contributor Author

bkasel commented Dec 19, 2015

OK, I think I did the adjustments the way I understood your comments. However there seem to be some conflicts. Is there a way for me to see what the conflicts are?

@jorisvandenbossche
Copy link
Member

@bkasel rebasing on latest master should let you solve the conflicts:

git fetch upstream
git rebase upstream/master
git push -f

@bkasel
Copy link
Contributor Author

bkasel commented Dec 19, 2015

I get an error:

git fetch upstream
fatal: 'upstream' does not appear to be a git repository
fatal: Could not read from remote repository.

@@ -33,6 +33,7 @@ Other enhancements

- Handle truncated floats in SAS xport files (:issue:`11713`)
- ``read_excel`` now supports s3 urls of the format ``s3://bucketname/filename`` (:issue:`11447`)
- - ``Dataframe`` has gained a ``_repr_latex_`` method in order to allow for automatic conversion to latex in a ipython/jupyter notebook using nbconvert. Options ``display.latex.escape`` and ``display.latex.longtable`` have been added to the configuration and are used automatically by the ``to_latex`` method.(:issue:`11778`)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A - too much at the beginning of the line

@jorisvandenbossche
Copy link
Member

you should then replace upstream with how you called the pydata/pandas git repository ('upstream' is the canonical name for this). Do git remote -v to see a list of those.

@bkasel
Copy link
Contributor Author

bkasel commented Dec 19, 2015

I didn't call it anything. I forked the repo on github and the cloned from the forked repo to my laptop.
git remote -v only shows my repo. (Sorry, complete git newbie). I managed to fetch using

git fetch https://www.github.com/pydata/pandas

but I can't rebase

@jorisvandenbossche
Copy link
Member

Ah, yes, you have to add this yourself, see also our contributing guide where this is explained: http://pandas.pydata.org/pandas-docs/stable/contributing.html#forking

git remote add upstream https://github.com/pydata/pandas.git

If you need more help, we also have a dev chat channel: https://gitter.im/pydata/pandas that is sometimes easier for quick help on these things

@bkasel
Copy link
Contributor Author

bkasel commented Dec 19, 2015

OK, the rebase was a bit messy, since it is the first one I have done, so I had to fix it. Seems to be OK now. Thanks for your help.

@jorisvandenbossche
Copy link
Member

@bkasel rebase did indeed work! We also ask to squash the commits to have a cleaner history, see http://pandas.pydata.org/pandas-docs/stable/contributing.html#combining-commits

@bkasel
Copy link
Contributor Author

bkasel commented Dec 19, 2015

I think I did the rebase and squashing succesfully.

@jreback
Copy link
Contributor

jreback commented Dec 19, 2015

squashing should yield 1 commit. you need to push with a -f as well.

… the

Jupyter/Ipython notebook. The method simply uses the existing to_latex method
of the Dataframe class.

The _repr_latex_ method looks for newly created options:
    - display.latex_escape
    - display.latex_longtable
which are passed as arguments to the to_latex method.

a simple test has been added to check if the conversion works. However, if the
implementation of the to_latex method changes, the test function would also have to be updated.

- fixed style problems
- renamed display.latex options
- remove duplicate line
- added whatsnew note

Merged during rebase

- fixed spelling mistake in config_init.py

- the defaults for the display.latex.escape option has been changed to the defaults of the to_latex method
- the to_latex method now gets these defaults from the configuration
- the _repr_latex_ method now simply calls the to latex method without any options
- documentation has been changed accordingly

- removed one``-`` from documentation

fixed bad merge during rebase

- fixed test_repr_latex using new defaults
@bkasel
Copy link
Contributor Author

bkasel commented Dec 19, 2015

Yes, I had some trouble getting the squashing done. Should only be a single commit now.

@jreback
Copy link
Contributor

jreback commented Dec 19, 2015

excellent! ping when green.

@bkasel
Copy link
Contributor Author

bkasel commented Dec 19, 2015

@jreback ping

jreback added a commit that referenced this pull request Dec 19, 2015
Missing _repr_latex_ method for latex/pdf conversion in jupyter notebooks
@jreback jreback merged commit b56b1e4 into pandas-dev:master Dec 19, 2015
@jreback
Copy link
Contributor

jreback commented Dec 19, 2015

thanks!

@bkasel
Copy link
Contributor Author

bkasel commented Dec 19, 2015

Nice! Thanks for the help!

@AlJohri
Copy link

AlJohri commented Feb 24, 2018

@bkasel do you know if there's an easy way to add borders to the output latex table?

@bkasel
Copy link
Contributor Author

bkasel commented Jun 19, 2018

@AlJohri I don't have an easy way to do this, since it would require \hline commands in the table which would require a modification of the Dataframe.to_latex method.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
IO LaTeX to_latex
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants