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

↘锁芯ラ 提交于 2021-01-29 08:34:39

问题


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.


回答1:


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 *))


来源:https://stackoverflow.com/questions/60729821/how-to-show-all-dataframe-table-columns-for-wide-tables-while-exporting-as-pdf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!