Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/nbconvert/exporters/latex.py: 42%
40 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-03 06:10 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-03 06:10 +0000
1"""LaTeX Exporter class"""
3# Copyright (c) Jupyter Development Team.
4# Distributed under the terms of the Modified BSD License.
7from traitlets import default
8from traitlets.config import Config
10from nbconvert.filters.filter_links import resolve_references
11from nbconvert.filters.highlight import Highlight2Latex
13from .templateexporter import TemplateExporter
16class LatexExporter(TemplateExporter):
17 """
18 Exports to a Latex template. Inherit from this class if your template is
19 LaTeX based and you need custom transformers/filters.
20 If you don't need custom transformers/filters, just change the
21 'template_file' config option. Place your template in the special "/latex"
22 subfolder of the "../templates" folder.
23 """
25 export_from_notebook = "LaTeX"
27 @default("file_extension")
28 def _file_extension_default(self):
29 return ".tex"
31 @default("template_name")
32 def _template_name_default(self):
33 return "latex"
35 output_mimetype = "text/latex"
37 def default_filters(self):
38 """Get the default filters."""
39 yield from super().default_filters()
40 yield ("resolve_references", resolve_references)
42 @property
43 def default_config(self):
44 c = Config(
45 {
46 "NbConvertBase": {
47 "display_data_priority": [
48 "text/latex",
49 "application/pdf",
50 "image/png",
51 "image/jpeg",
52 "image/svg+xml",
53 "text/markdown",
54 "text/plain",
55 ]
56 },
57 "ExtractAttachmentsPreprocessor": {"enabled": True},
58 "ExtractOutputPreprocessor": {"enabled": True},
59 "SVG2PDFPreprocessor": {"enabled": True},
60 "LatexPreprocessor": {"enabled": True},
61 "SphinxPreprocessor": {"enabled": True},
62 "HighlightMagicsPreprocessor": {"enabled": True},
63 }
64 )
65 if super().default_config:
66 c2 = super().default_config.copy()
67 c2.merge(c)
68 c = c2
69 return c
71 def from_notebook_node(self, nb, resources=None, **kw):
72 """Convert from notebook node."""
73 langinfo = nb.metadata.get("language_info", {})
74 lexer = langinfo.get("pygments_lexer", langinfo.get("name", None))
75 highlight_code = self.filters.get(
76 "highlight_code", Highlight2Latex(pygments_lexer=lexer, parent=self)
77 )
78 self.register_filter("highlight_code", highlight_code)
80 return super().from_notebook_node(nb, resources, **kw)
82 def _create_environment(self):
83 environment = super()._create_environment()
85 # Set special Jinja2 syntax that will not conflict with latex.
86 environment.block_start_string = "((*"
87 environment.block_end_string = "*))"
88 environment.variable_start_string = "((("
89 environment.variable_end_string = ")))"
90 environment.comment_start_string = "((="
91 environment.comment_end_string = "=))"
93 return environment