Skip to content

Commit

Permalink
Make date configurable in latex/PDF (#1963)
Browse files Browse the repository at this point in the history
Co-authored-by: Achim Gädke <achimgaedke@users.noreply.github.com>
  • Loading branch information
achimgaedke and achimgaedke committed Apr 1, 2023
1 parent 9afa37f commit 056dc4e
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 3 deletions.
28 changes: 28 additions & 0 deletions docs/source/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
32 changes: 30 additions & 2 deletions nbconvert/preprocessors/latex.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
# -----------------------------------------------------------------------------


from traitlets import Unicode
from traitlets import List, Unicode

from .base import Preprocessor

Expand All @@ -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):
Expand All @@ -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
6 changes: 5 additions & 1 deletion share/templates/latex/base.tex.j2
Original file line number Diff line number Diff line change
Expand Up @@ -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') )))}
Expand Down

0 comments on commit 056dc4e

Please sign in to comment.