【问题标题】:How to remove date from pdf produced from ipynb with nbconvertHow to remove date from pdf produced from ipynb with nbconvert
【发布时间】:2022-12-28 09:20:45
【问题描述】:

I am using nbconvert programmatically to export a jupyter notebook file to pdf:

import nbformat
from nbconvert.exporters import PDFExporter
from nbconvert.preprocessors import TagRemovePreprocessor
from traitlets.config import Config

c = Config()
c.TagRemovePreprocessor.remove_cell_tags = ("remove_cell",)
c.PDFExporter.preprocessors = ["nbconvert.preprocessors.TagRemovePreprocessor"]
c.PDFExporter.exclude_input_prompt = True
c.PDFExporter.exclude_output_prompt = True
c.PDFExporter.exclude_input = True

exporter = PDFExporter(config=c)
exporter.register_preprocessor(TagRemovePreprocessor(config=c),True)

with open("example.ipynb") as f:
    nb = nbformat.read(f, as_version=4)

pdf_data, _ = exporter.from_notebook_node(nb)

with open("example.pdf", "wb") as f:
    f.write(pdf_data)

This works, however today's date gets inserted in the document under the title.

This date is misleading in the context of what I am producing.

My attempt at removing it involved editing share\jupyter\nbconvert\templates\latex\base.tex.j2:

\renewcommand{\TeX}{\textrm{\Oldtex}}
\renewcommand{\LaTeX}{\textrm{\Oldlatex}}
% Document parameters
% Document title
((* block title -*))
((*- set nb_title = nb.metadata.get('title', '') or resources['metadata']['name'] -*))
\title{((( nb_title | escape_latex )))}
((*- endblock title *))
((* block date *))((* endblock date *))
((* block author *))
((* if 'authors' in nb.metadata *))
\author{((( nb.metadata.authors | join(', ', attribute='name') )))}
((* endif *))
((* endblock author *))

and removing the line ((* block date *))((* endblock date *)) but this seems to have no effect.

I know that file is being used in the export process because if I insert jibberish into it then the export fails.

Any ideas where the date is coming from?

【问题讨论】:

  • Can you show the intermediate .tex file?
  • In normal tex documemts. I'd just add \date{}
  • thanks @samcarter_is_at_topanswers.xyz that did the trick

标签: python latex jinja2 nbconvert


【解决方案1】:

By default the date is set to date{ oday}, you can overwrite it by setting it to something else, e.g. with an empty argument:

date{}

【讨论】:

  • Where does someone need to add date{}?
  • @MaxHager Anywhere before maketitle should do
【解决方案2】:

Here's a solution to remove the date. Install the classic pdf template

https://stackoverflow.com/a/52575599/9161828

【讨论】:

    猜你喜欢
    • 2022-11-20
    • 2022-05-17
    • 2022-12-19
    • 2022-12-02
    • 2022-12-02
    • 2022-12-27
    • 2022-12-01
    • 2022-11-01
    • 2022-12-27
    相关资源
    最近更新 更多