latex.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. """LaTeX Exporter class"""
  2. # Copyright (c) Jupyter Development Team.
  3. # Distributed under the terms of the Modified BSD License.
  4. import os
  5. from traitlets import Unicode, default
  6. from traitlets.config import Config
  7. from jupyter_core.paths import jupyter_path
  8. from nbconvert.filters.highlight import Highlight2Latex
  9. from nbconvert.filters.filter_links import resolve_references
  10. from .templateexporter import TemplateExporter
  11. class LatexExporter(TemplateExporter):
  12. """
  13. Exports to a Latex template. Inherit from this class if your template is
  14. LaTeX based and you need custom transformers/filters.
  15. If you don't need custom transformers/filters, just change the
  16. 'template_file' config option. Place your template in the special "/latex"
  17. subfolder of the "../templates" folder.
  18. """
  19. export_from_notebook = "LaTeX"
  20. @default('file_extension')
  21. def _file_extension_default(self):
  22. return '.tex'
  23. @default('template_file')
  24. def _template_file_default(self):
  25. return 'article.tplx'
  26. # Latex constants
  27. @default('default_template_path')
  28. def _default_template_path_default(self):
  29. return os.path.join("..", "templates", "latex")
  30. @default('template_skeleton_path')
  31. def _template_skeleton_path_default(self):
  32. return os.path.join("..", "templates", "latex", "skeleton")
  33. @default('template_data_paths')
  34. def _template_data_paths_default(self):
  35. return jupyter_path("nbconvert", "templates", "latex")
  36. #Extension that the template files use.
  37. template_extension = Unicode(".tplx").tag(config=True)
  38. output_mimetype = 'text/latex'
  39. def default_filters(self):
  40. for x in super(LatexExporter, self).default_filters():
  41. yield x
  42. yield ('resolve_references', resolve_references)
  43. @property
  44. def default_config(self):
  45. c = Config({
  46. 'NbConvertBase': {
  47. 'display_data_priority' : ['text/latex', 'application/pdf', 'image/png', 'image/jpeg', 'image/svg+xml', 'text/markdown', 'text/plain']
  48. },
  49. 'ExtractOutputPreprocessor': {
  50. 'enabled':True
  51. },
  52. 'SVG2PDFPreprocessor': {
  53. 'enabled':True
  54. },
  55. 'LatexPreprocessor': {
  56. 'enabled':True
  57. },
  58. 'SphinxPreprocessor': {
  59. 'enabled':True
  60. },
  61. 'HighlightMagicsPreprocessor': {
  62. 'enabled':True
  63. }
  64. })
  65. c.merge(super(LatexExporter,self).default_config)
  66. return c
  67. def from_notebook_node(self, nb, resources=None, **kw):
  68. langinfo = nb.metadata.get('language_info', {})
  69. lexer = langinfo.get('pygments_lexer', langinfo.get('name', None))
  70. highlight_code = self.filters.get('highlight_code', Highlight2Latex(pygments_lexer=lexer, parent=self))
  71. self.register_filter('highlight_code', highlight_code)
  72. return super(LatexExporter, self).from_notebook_node(nb, resources, **kw)
  73. def _create_environment(self):
  74. environment = super(LatexExporter, self)._create_environment()
  75. # Set special Jinja2 syntax that will not conflict with latex.
  76. environment.block_start_string = "((*"
  77. environment.block_end_string = "*))"
  78. environment.variable_start_string = "((("
  79. environment.variable_end_string = ")))"
  80. environment.comment_start_string = "((="
  81. environment.comment_end_string = "=))"
  82. return environment