1"""Module that allows latex output notebooks to be conditioned before
2they are converted.
3"""
4# -----------------------------------------------------------------------------
5# Copyright (c) 2013, the IPython Development Team.
6#
7# Distributed under the terms of the Modified BSD License.
8#
9# The full license is in the file COPYING.txt, distributed with this software.
10# -----------------------------------------------------------------------------
11
12# -----------------------------------------------------------------------------
13# Imports
14# -----------------------------------------------------------------------------
15
16from traitlets import List, Unicode
17
18from .base import Preprocessor
19
20# -----------------------------------------------------------------------------
21# Classes
22# -----------------------------------------------------------------------------
23
24
25class LatexPreprocessor(Preprocessor):
26 """Preprocessor for latex destined documents.
27
28 Populates the ``latex`` key in the resources dict,
29 adding definitions for pygments highlight styles.
30
31 Sets the authors, date and title of the latex document,
32 overriding the values given in the metadata.
33 """
34
35 date = Unicode(
36 None,
37 help=("Date of the LaTeX document"),
38 allow_none=True,
39 ).tag(config=True)
40
41 title = Unicode(None, help=("Title of the LaTeX document"), allow_none=True).tag(config=True)
42
43 author_names = List(
44 Unicode(),
45 default_value=None,
46 help=("Author names to list in the LaTeX document"),
47 allow_none=True,
48 ).tag(config=True)
49
50 style = Unicode("default", help="Name of the pygments style to use").tag(config=True)
51
52 def preprocess(self, nb, resources):
53 """Preprocessing to apply on each notebook.
54
55 Parameters
56 ----------
57 nb : NotebookNode
58 Notebook being converted
59 resources : dictionary
60 Additional resources used in the conversion process. Allows
61 preprocessors to pass variables into the Jinja engine.
62 """
63 # Generate Pygments definitions for Latex
64 from pygments.formatters import LatexFormatter
65
66 resources.setdefault("latex", {})
67 resources["latex"].setdefault(
68 "pygments_definitions", LatexFormatter(style=self.style).get_style_defs()
69 )
70 resources["latex"].setdefault("pygments_style_name", self.style)
71
72 if self.author_names is not None:
73 nb.metadata["authors"] = [{"name": author} for author in self.author_names]
74
75 if self.date is not None:
76 nb.metadata["date"] = self.date
77
78 if self.title is not None:
79 nb.metadata["title"] = self.title
80
81 return nb, resources