diff --git a/docs/source/usage.rst b/docs/source/usage.rst index e09bde60e..ef3197d74 100644 --- a/docs/source/usage.rst +++ b/docs/source/usage.rst @@ -89,6 +89,34 @@ 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. + + 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 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') )))}