Skip to main content

A wrapper to easily use Jinja2 for tex-templates.

Project description

A wrapper of Jinja2 for easy rendering of .tex files of all sorts.

Installation

pip install jinjatex

Usage

The main Jinjatex class provides wrappers to compile tex templates with jinja. Tex is not included, so make sure the binaries are available.

Template Syntax

The jinja template syntax is changed a little to not interference with latex commands:

((( This is a variable )))
((= This is a comment. =))
((* This is a block *))

((= Example =))
((* for value in somelist *))
    ((( value )))
((* endfor *))

Python Bindings

from jinjatex import Jinjatex

from textwrap import dedent

# Example 1: Compile string templates

template_string = dedent(r"""
    % Example Template
    \documentclass{article}

    \begin{document}
    Hi ((( name )))!

    The document will be compiled multiple times, until
    all references etc. are resolved, like this one: \ref{somelabel}

    \section{Somesection}
    \label{somelabel}

    Some more content.

    \end{document}
    """)

tex = Jinjatex()

# Return rendered template only
tex.render(template_string)
# Compile .tex and return content of .pdf, default engine is pdflatex
with open('result.pdf', 'wb') as file:
    file.write(tex.compile(template_string))


# Example 2: Jinja options and other tex compilers
# Assuming a template 'mytemplate.tex' exists in mypackage/tex_templates

from jinja2 import PackageLoader, StrictUndefined

tex = Jinjatex(tex_engine='xelatex',
               loader=PackageLoader('mypackage', 'tex_templates'),
               trim_blocks=True,
               undefined=StrictUndefined)

tex.render_template('mytemplate.tex')
with open('result.pdf', 'wb') as file:
    file.write(tex.compile_template('mytemplate.tex'))

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

jinjatex-0.2.tar.gz (3.6 kB view details)

Uploaded Source

File details

Details for the file jinjatex-0.2.tar.gz.

File metadata

  • Download URL: jinjatex-0.2.tar.gz
  • Upload date:
  • Size: 3.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for jinjatex-0.2.tar.gz
Algorithm Hash digest
SHA256 242723331cdfc8280343aed3d146754feeee487a14f1e4ee853ebf1610cda5b4
MD5 51e67692b197936943218ea84173c344
BLAKE2b-256 794224d0e4b53fc7e2490e2ec7ceac0f77b4ad27dc02f0276e033e00a9020a06

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page