简体   繁体   中英

How to show all DataFrame/ table columns for wide tables while exporting as pdf from Jupyter using nbconvert?

This question is linked to Jupyter nbconvert LaTex Export Theme

I am trying to export my Jupyter Notebook as a .pdf using nbconvert through the terminal (on Mac). I already created a .tplx template that will hide the code and show only outputs and markdowns.

# this is my jupyter notebook heading
pd.set_option('display.notebook_repr_html', True)
pd.set_option('display.expand_frame_repr', True)
pd.set_option("display.latex.repr", True)
pd.set_option("display.latex.longtable", True)
pd.set_option("display.latex.escape", True)
# this is the .tplx template for hiding the input and (preferably) setting the table width
# to fit the page width (including margins)
((*- extends 'article.tplx' -*))

((* block input_group *))
    ((*- if cell.metadata.get('nbconvert', {}).get('show_code', False) -*))
        ((( super() )))
    ((*- endif -*))
((* endblock input_group *))

((* block docclass *))   
\documentclass[8pt]{article}
\AtBeginDocument{
   \usepackage{graphicx}
   \resizebox{\textwidth}
   }
((* endblock docclass *))

What I get with this is a pdf with a table that has the proper style and continues the rows on the next page but it doesn't slice the columns to the next page. Instead, it just doesn't show them at all. Here is the screenshot. 在此处输入图片说明

I have checked previous posts on SO and I tried to edit the .tplx file using the available documentation, but I couldn't find a solution.

The article documentclass by default has a textwidth of 345pts, and the table you're attempting to include is just too wide for this. You essentially have two options: make the table smaller, or make the textwidth larger.

You could make the textwidth larger by decreasing the size of the margins: for example with \\usepackage{geometry}[left=0cm, right=0cm] . Another way you could increase the textwidth would be to use a landscape layout \\documentclass[8pt,landscape]{article} .

To decrease the size of the table you can look at decreasing the space between columns. You could try \\setlength{\\tabcolsep}{3pt} for example (the default separation is 6pt).

If appropriate I would also consider abbreviating the names of the countries in the column names. Eg "UK" takes up a lot less space than "United Kingdom". This would certainly help decrease the width of the table.

All of these options would be specified in the docclass block. Ie, to set up a landscape page, with no margins and 3pt between columns you would have.

((* block docclass *))   
\documentclass[8pt,landscape]{article}
\AtBeginDocument{
   \usepackage{graphicx}
   \resizebox{\textwidth}
   \setlength{\tabcolsep}{3pt}
   \usepackage{geometry}[left=0cm, right=0cm]
   }
((* endblock docclass *))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM