【问题标题】:How to show all DataFrame/ table columns for wide tables while exporting as pdf from Jupyter using nbconvert?使用 nbconvert 从 Jupyter 导出为 pdf 时,如何显示宽表的所有 DataFrame/表列?
【发布时间】:2020-06-28 23:21:35
【问题描述】:

这个问题链接到Jupyter nbconvert LaTex Export Theme

我正在尝试通过终端(在 Mac 上)使用 nbconvert 将我的 Jupyter Notebook 导出为 .pdf。我已经创建了一个 .tplx 模板,它将隐藏代码并仅显示输出和降价。

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

我得到的是一个 pdf,其中的表格具有适当的样式并继续下一页上的行,但它不会将列分割到下一页。相反,它根本没有显示它们。这是屏幕截图。

我查看了之前关于 SO 的帖子,并尝试使用可用文档编辑 .tplx 文件,但找不到解决方案。

【问题讨论】:

    标签: python pandas jupyter-notebook latex nbconvert


    【解决方案1】:

    默认情况下,article 文档类的文本宽度为 345pts,而您尝试包含的表格对于此而言太宽了。您基本上有两个选择:缩小表格,或扩大文本宽度。

    您可以通过减小边距来增大文本宽度:例如使用\usepackage{geometry}[left=0cm, right=0cm]。另一种增加文本宽度的方法是使用横向布局\documentclass[8pt,landscape]{article}

    要减小表格的大小,您可以考虑减小列之间的空间。例如,您可以尝试\setlength{\tabcolsep}{3pt}(默认分隔为 6pt)。

    如果合适,我还会考虑在列名中缩写国家/地区的名称。例如,“UK”占用的空间比“United Kingdom”少得多。这肯定有助于减小表格的宽度。

    所有这些选项都将在 docclass 块中指定。即,设置一个横向页面,没有边距,并且列之间有 3pt。

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

    【讨论】:

    • 感谢@Ralph 的这个建议,我已经尝试过了,但它不起作用:/ 它没有完成我的笔记本打印。问题是\resizebox{\textwidth} 行,因为当我删除它时,它会打印 pdf。横向选项的唯一问题是,这样它将整个文档设置为横向格式。有没有办法将其设置为仅适用于具有宽表格的页面的横向格式,并为其余页面保留纵向格式?
    • 我实际上并不确定\resizebox{\textwidth} 的用途,我只是把它留在了那里,就像它在你的问题中一样。我会进一步研究一下,我之前实际上没有使用 nbconvert 进行 pdf 输出。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多