From ba67516ece9ca9ef8d972f38aa811819f7d259f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Achim=20G=C3=A4dke?= Date: Sat, 1 Apr 2023 13:53:33 +1300 Subject: [PATCH 1/3] make date configurable in latex/PDF - #1962 --- share/templates/latex/base.tex.j2 | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/share/templates/latex/base.tex.j2 b/share/templates/latex/base.tex.j2 index 73295c687..2225e9d74 100644 --- a/share/templates/latex/base.tex.j2 +++ b/share/templates/latex/base.tex.j2 @@ -179,7 +179,11 @@ override this.-=)) ((*- set nb_title = nb.metadata.get('title', '') or resources['metadata']['name'] -*)) \title{((( nb_title | escape_latex )))} ((*- endblock title *)) - ((* block date *))((* endblock date *)) + ((* block date *)) + ((* if 'date' in nb.metadata *)) + \date{((( nb.metadata.date | escape_latex )))} + ((* endif *)) + ((* endblock date *)) ((* block author *)) ((* if 'authors' in nb.metadata *)) \author{((( nb.metadata.authors | join(', ', attribute='name') )))} From dbd51832633e4ffc87ad3eb32a53a2d50ede06a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Achim=20G=C3=A4dke?= Date: Sat, 1 Apr 2023 14:51:20 +1300 Subject: [PATCH 2/3] LaTeX: ipynb metadata for authors, date, and title --- docs/source/usage.rst | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/docs/source/usage.rst b/docs/source/usage.rst index e09bde60e..963d87ba2 100644 --- a/docs/source/usage.rst +++ b/docs/source/usage.rst @@ -89,6 +89,29 @@ LaTeX Latex report, providing a table of contents and chapters. + Optionally you can specify ``authors``, ``title`` and ``date`` in the notebook's + metadata. These will be used to render the header of the LaTeX document. + + .. code-block:: json + + { + "authors": [ + { + "name": "Jane Doe" + }, + { + "name": "John Doe" + } + ], + "date": "January 2023", + "title": "Annual Data Report 2022", + "kernelspec": { }, + "language_info": { } + } + + If no date is specified, today's date will be used (i.e. the date when the + document is re/compiled). Use an empty string to suppress the date. + .. note:: nbconvert uses pandoc_ to convert between various markup languages, From f7f1b23d1e373a8c6069ef175c04c44aa0e5a09a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Achim=20G=C3=A4dke?= Date: Sat, 1 Apr 2023 20:31:37 +1300 Subject: [PATCH 3/3] latex: author/date/title via command line --- docs/source/usage.rst | 5 +++++ nbconvert/preprocessors/latex.py | 32 ++++++++++++++++++++++++++++++-- 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/docs/source/usage.rst b/docs/source/usage.rst index 963d87ba2..ef3197d74 100644 --- a/docs/source/usage.rst +++ b/docs/source/usage.rst @@ -112,6 +112,11 @@ LaTeX If no date is specified, today's date will be used (i.e. the date when the document is re/compiled). Use an empty string to suppress the date. + The values in the notebook can be overridden by the command line arguments + ``--LatexPreprocessor.title``, ``--LatexPreprocessor.date`` and + ``--LatexPreprocessor.author_names`` (specify this argument multiple times + for each individual author name). + .. note:: nbconvert uses pandoc_ to convert between various markup languages, diff --git a/nbconvert/preprocessors/latex.py b/nbconvert/preprocessors/latex.py index 009af4ecb..312fd13a3 100644 --- a/nbconvert/preprocessors/latex.py +++ b/nbconvert/preprocessors/latex.py @@ -14,7 +14,7 @@ # ----------------------------------------------------------------------------- -from traitlets import Unicode +from traitlets import List, Unicode from .base import Preprocessor @@ -26,10 +26,28 @@ class LatexPreprocessor(Preprocessor): """Preprocessor for latex destined documents. - Mainly populates the ``latex`` key in the resources dict, + Populates the ``latex`` key in the resources dict, adding definitions for pygments highlight styles. + + Sets the authors, date and title of the latex document, + overriding the values given in the metadata. """ + date = Unicode( + None, + help=("Date of the LaTeX document"), + allow_none=True, + ).tag(config=True) + + title = Unicode(None, help=("Title of the LaTeX document"), allow_none=True).tag(config=True) + + author_names = List( + Unicode(), + default_value=None, + help=("Author names to list in the LaTeX document"), + allow_none=True, + ).tag(config=True) + style = Unicode("default", help="Name of the pygments style to use").tag(config=True) def preprocess(self, nb, resources): @@ -51,4 +69,14 @@ def preprocess(self, nb, resources): "pygments_definitions", LatexFormatter(style=self.style).get_style_defs() ) resources["latex"].setdefault("pygments_style_name", self.style) + + if self.author_names is not None: + nb.metadata["authors"] = [{"name": author} for author in self.author_names] + + if self.date is not None: + nb.metadata["date"] = self.date + + if self.title is not None: + nb.metadata["title"] = self.title + return nb, resources