latex.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. """Module that allows latex output notebooks to be conditioned before
  2. they 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. # Imports
  13. #-----------------------------------------------------------------------------
  14. from __future__ import print_function, absolute_import
  15. from .base import Preprocessor
  16. from traitlets import Unicode
  17. #-----------------------------------------------------------------------------
  18. # Classes
  19. #-----------------------------------------------------------------------------
  20. class LatexPreprocessor(Preprocessor):
  21. """Preprocessor for latex destined documents.
  22. Mainly populates the `latex` key in the resources dict,
  23. adding definitions for pygments highlight styles.
  24. """
  25. style = Unicode('default',
  26. help='Name of the pygments style to use'
  27. ).tag(config=True)
  28. def preprocess(self, nb, resources):
  29. """Preprocessing to apply on each notebook.
  30. Parameters
  31. ----------
  32. nb : NotebookNode
  33. Notebook being converted
  34. resources : dictionary
  35. Additional resources used in the conversion process. Allows
  36. preprocessors to pass variables into the Jinja engine.
  37. """
  38. # Generate Pygments definitions for Latex
  39. from pygments.formatters import LatexFormatter
  40. resources.setdefault("latex", {})
  41. resources["latex"].setdefault("pygments_definitions", LatexFormatter(style=self.style).get_style_defs())
  42. resources["latex"].setdefault("pygments_style_name", self.style)
  43. return nb, resources