コード例 #1
0
def export_tex(nbnode, outfile="export_tex_out", template="classicm"):
    latex_exporter = LatexExporter()
    latex_exporter.template_file = template
    (body, resources) = latex_exporter.from_notebook_node(nbnode)
    writer = FilesWriter()
    writer.write(body, resources,
                 notebook_name=outfile)  # will end up with .tex extension
コード例 #2
0
def strip_latex(notebook, replacements=None):
    replacements = REPLACEMENTS if replacements is None else replacements
    latex_exp = LatexExporter(config={})
    latex = latex_exp.from_notebook_node(notebook)
    latex_code = latex[0]
    start_str = r"\begin{document}"
    start = latex_code.find(start_str) + len(start_str)
    end = latex_code.find(r"\end{document}")
    to_export = latex_code[start:end].strip()
    for orig, repl in replacements.items():
        to_export = to_export.replace(orig, repl)
    return to_export
コード例 #3
0
def test_svg2pdf_preprocessor():
    """ Test svg2pdf preprocessor for markdown cell images """
    nb_name = 'tests/data/svg2pdf.ipynb'
    pdf_file = 'tests/data/test.pdf'
    with open(nb_name, 'r') as f:
        notebook_json = f.read()
    notebook = nbformat.reads(notebook_json, as_version=4)
    c.LatexExporter.preprocessors = ["pre_svg2pdf.SVG2PDFPreprocessor"]
    c.NbConvertApp.export_format = 'latex'
    latex_exporter = LatexExporter(config=c)
    body = latex_exporter.from_notebook_node(notebook)
    assert os.path.isfile(pdf_file)
    os.remove(pdf_file)
    assert 'test.pdf' in body[0]
コード例 #4
0
def test_svg2pdf_preprocessor():
    """ Test svg2pdf preprocessor for markdown cell images """
    nb_name='tests/data/svg2pdf.ipynb'
    pdf_file='tests/data/test.pdf'
    with open(nb_name, 'r') as f:
        notebook_json = f.read()
    notebook = nbformat.reads(notebook_json, as_version=4)
    c.LatexExporter.preprocessors = ["pre_svg2pdf.SVG2PDFPreprocessor"]
    c.NbConvertApp.export_format = 'latex'
    latex_exporter = LatexExporter(config=c)
    body = latex_exporter.from_notebook_node(notebook)
    assert os.path.isfile(pdf_file) 
    os.remove(pdf_file)
    assert 'test.pdf' in body[0]
コード例 #5
0
def convertNotebooktoLaTeX(notebookPath,
                           outfilePath="latex_out1",
                           template="classicm"):
    REG_nb = re.compile(r"(\d\d)\.(\d\d)-(.*)\.ipynb")
    base_nb_filename = os.path.basename(notebookPath)
    if REG_nb.match(base_nb_filename):
        with open(notebookPath) as fh:
            nbnode = nbformat.read(fh, as_version=4)

        exporter = LatexExporter()
        exporter.template_file = template  # classicm style if not specified
        exporter.file_extension = ".tex"
        body, resources = exporter.from_notebook_node(nbnode)
        writer = FilesWriter()
        writer.write(
            body, resources,
            notebook_name=outfilePath)  # will end up with .tex extension
コード例 #6
0
    def __init__(self, builder):
        self.pdfdir = builder.outdir + "/pdf"  #pdf directory
        self.texdir = builder.outdir + "/executed"  #latex directory
        self.texbookdir = builder.outdir + "/texbook"  # tex files for book pdf

        for path in [self.pdfdir, self.texdir]:
            ensuredir(path)

        self.pdf_exporter = PDFExporter()
        self.tex_exporter = LatexExporter()
        self.index_book = builder.config['tojupyter_pdf_book_index']
コード例 #7
0
 def export_latex(self,
                  filename,
                  engine='pdflatex',
                  interaction='-interaction batchmode'):
     r""" exports to latex """
     lyx.latex()
     lyx.markdown(False)
     bi.__formatter__ = Latex
     bi.__need_latex__ = True
     self.c.LatexExporter.preprocessors = \
         ['nbconvert.preprocessors.ExecutePreprocessor']
     with open('nilf.tplx', 'w') as f:
         f.write(self.template)
     latex_exporter = LatexExporter(config=self.c,
                                    template_file='nilf.tplx')
     (body, resources) = latex_exporter.from_notebook_node(self.nb)
     with open('./' + filename + '.tex', 'w') as f:
         f.write(body)
     #interaction = '-interaction batchmode'
     #interaction = ''
     os.system('{engine} {int} ./'.format(engine=engine, int=interaction) +
               filename + '.tex')
     os.system('bibtex ./' + filename)
     os.system('bibtex ./' + filename + '.aux')
     os.system('{engine} {int} ./'.format(engine=engine, int=interaction) +
               filename + '.tex')
     os.system('{engine} {int} ./'.format(engine=engine, int=interaction) +
               filename + '.tex')
     os.system('{engine} {int} ./'.format(engine=engine, int=interaction) +
               filename + '.tex')
     os.system(
         'makeindex ./{fname}.nlo -s nomencl.ist -o {fname}.nls'.format(
             fname=filename))
     os.system('{engine} {int} ./'.format(engine=engine, int=interaction) +
               filename + '.tex')
     display(FileLink('./' + filename + '.pdf'))
     display(FileLink('./' + filename + '.tex'))
     #lyx.latex(False)
     os.remove('temp_notebook.ipynb')
コード例 #8
0
def convertNotebooktoLaTeX(notebookPath, outfilePath='latex_out1', template='classicm'):
    REG_nb = re.compile(r'(\d\d)\.(\d\d)-(.*)\.ipynb')
    base_nb_filename = os.path.basename(notebookPath)
    if REG_nb.match(base_nb_filename):
        with open(notebookPath) as fh:
            nbnode = nbformat.read(fh, as_version=4)

        exporter = LatexExporter()
        exporter.template_file = template  # classicm style if not specified
        exporter.file_extension = '.tex'
        exporter.register_preprocessor(RegexRemovePreprocessor, enabled=False)  ###########################
        body, resources = exporter.from_notebook_node(nbnode)
        writer = FilesWriter()
        writer.write(body, resources, notebook_name=outfilePath)  # will end up with .tex extension
コード例 #9
0
    replacements = []
    for cell in nb["cells"]:
        if isinstance(cell, MutableMapping):
            if cell["cell_type"] == "code" and "# Setup" not in cell["source"]:
                continue
            if "metadata" in cell and "pycharm" in cell["metadata"]:
                del cell["metadata"]["pycharm"]
            replacements.append(cell)
    nb["cells"] = replacements
    executed = pre.execute.executenb(nb, cwd=source_dir)
    cop = pre.ClearOutputPreprocessor()
    nb, _ = cop.preprocess(executed, {})

    _, base = os.path.split(nb_file)
    out = os.path.abspath(os.path.join("..", base))
    latex_exp = LatexExporter(config={})
    latex = latex_exp.from_notebook_node(nb)
    base, _ = os.path.splitext(base)
    latex_code = latex[0]
    start_str = r"\begin{document}"
    start = latex_code.find(start_str) + len(start_str)
    end = latex_code.find(r"\end{document}")
    to_export = latex_code[start:end].strip()
    replacements = {
        r"\maketitle": "",
        r"\section{": r"\chapter{",
        r"\subsection{": r"\section{",
        r"\subsubsection{": r"\subsection{",
        r"\section{Problem": r"\subsection*{Problem",
        r"\section{\texorpdfstring": r"\subsection*{\texorpdfstring",
        r"\section{Exercises": r"\section*{Exercises",