latex.py
116 lines
| 4.5 KiB
| text/x-python
|
PythonLexer
|
r10677 | """ | |
Exporter that allows Latex Jinja templates to work. Contains logic to | |||
appropriately prepare IPYNB files for export to LaTeX. Including but | |||
not limited to escaping LaTeX, fixing math region tags, using special | |||
tags to circumvent Jinja/Latex syntax conflicts. | |||
""" | |||
|
r10479 | #----------------------------------------------------------------------------- | |
# Copyright (c) 2013, the IPython Development Team. | |||
# | |||
# Distributed under the terms of the Modified BSD License. | |||
# | |||
# The full license is in the file COPYING.txt, distributed with this software. | |||
#----------------------------------------------------------------------------- | |||
#----------------------------------------------------------------------------- | |||
# Imports | |||
#----------------------------------------------------------------------------- | |||
|
r10585 | ||
# Stdlib imports | |||
import os | |||
# IPython imports | |||
|
r10624 | from IPython.utils.traitlets import Unicode | |
|
r10585 | ||
# other libs/dependencies | |||
from jinja2 import Environment, FileSystemLoader | |||
|
r10626 | import nbconvert.filters.latex | |
|
r10630 | import nbconvert.filters.highlight | |
|
r10626 | from nbconvert.transformers.latex import LatexTransformer | |
|
r10677 | ||
# local import | |||
import exporter | |||
|
r10479 | #----------------------------------------------------------------------------- | |
# Globals and constants | |||
#----------------------------------------------------------------------------- | |||
#Latex Jinja2 constants | |||
|
r10630 | LATEX_TEMPLATE_PATH = "/../templates/latex/" | |
LATEX_TEMPLATE_SKELETON_PATH = "/../templates/latex/skeleton/" | |||
|
r10479 | ||
#Special Jinja2 syntax that will not conflict when exporting latex. | |||
LATEX_JINJA_COMMENT_BLOCK = ["((=", "=))"] | |||
LATEX_JINJA_VARIABLE_BLOCK = ["(((", ")))"] | |||
LATEX_JINJA_LOGIC_BLOCK = ["((*", "*))"] | |||
#----------------------------------------------------------------------------- | |||
# Classes and functions | |||
#----------------------------------------------------------------------------- | |||
|
r10677 | class LatexExporter(exporter.Exporter): | |
""" | |||
Exports to a Latex template. Inherit from this class if your template is | |||
LaTeX based and you need custom tranformers/filters. Inherit from it if | |||
you are writing your own HTML template and need custom tranformers/filters. | |||
If you don't need custom tranformers/filters, just change the | |||
'template_file' config option. Place your template in the special "/latex" | |||
subfolder of the "../templates" folder. | |||
""" | |||
|
r10624 | #Extension that the template files use. | |
template_extension = ".tplx" | |||
file_extension = Unicode( | |||
'tex', config=True, | |||
help="Extension of the file that should be written to disk") | |||
|
r10479 | ||
|
r10624 | template_file = Unicode( | |
|
r10630 | 'base', config=True, | |
|
r10624 | help="Name of the template file to use") | |
|
r10677 | def __init__(self, transformers=None, filters=None, config=None, **kw): | |
|
r10588 | ||
#Call base class constructor. | |||
|
r10677 | super(LatexExporter, self).__init__(transformers, filters, config, **kw) | |
|
r10624 | ||
self.extract_figure_transformer.display_data_priority = ['latex', 'svg', 'png', 'jpg', 'jpeg' , 'text'] | |||
|
r10588 | self.extract_figure_transformer.extra_ext_map={'svg':'pdf'} | |
|
r10578 | def _init_environment(self): | |
|
r10587 | self.environment = Environment( | |
|
r10578 | loader=FileSystemLoader([ | |
os.path.dirname(os.path.realpath(__file__)) + LATEX_TEMPLATE_PATH, | |||
os.path.dirname(os.path.realpath(__file__)) + LATEX_TEMPLATE_SKELETON_PATH, | |||
]), | |||
|
r10587 | extensions=exporter.JINJA_EXTENSIONS | |
|
r10578 | ) | |
#Set special Jinja2 syntax that will not conflict with latex. | |||
|
r10587 | self.environment.block_start_string = LATEX_JINJA_LOGIC_BLOCK[0] | |
self.environment.block_end_string = LATEX_JINJA_LOGIC_BLOCK[1] | |||
self.environment.variable_start_string = LATEX_JINJA_VARIABLE_BLOCK[0] | |||
self.environment.variable_end_string = LATEX_JINJA_VARIABLE_BLOCK[1] | |||
self.environment.comment_start_string = LATEX_JINJA_COMMENT_BLOCK[0] | |||
self.environment.comment_end_string = LATEX_JINJA_COMMENT_BLOCK[1] | |||
def _register_filters(self): | |||
#Register the filters of the base class. | |||
|
r10630 | super(LatexExporter, self)._register_filters() | |
|
r10587 | ||
#Add latex filters to the Jinja2 environment | |||
|
r10677 | self.register_filter('escape_tex', nbconvert.filters.latex.escape_latex) | |
|
r10630 | self.register_filter('highlight', nbconvert.filters.highlight.highlight2latex) | |
|
r10587 | ||
|
r10677 | ||
|
r10587 | def _register_transformers(self): | |
#Register the transformers of the base class. | |||
|
r10630 | super(LatexExporter, self)._register_transformers() | |
|
r10587 | ||
#Register latex transformer | |||
self.register_transformer(LatexTransformer) | |||