Skip to main content

A jinja2 dialect for latex templating in python.

Project description

TempLatex

codecov

A jinja2 dialect for latex templating in python.

Quick Start

pip install templatex
from templatex import Environment
from jinja2 import FileSystemLoader

env = Environment(loader=FileSystemLoader('./template_folder'))
template = env.get_template("chapter.latex")
print(template.render(the="variables", go="here"))

Why not the default jinja2?

jinja2's default escape works great for HTML but is not user-friendly for latex because latex heavily uses curly brackets {} .

For example, if you want to produce the following latex code:

x = \frac{b}{c}

Where b and c will be replaced by an actual number, for example: {"b": 42, "c": 3}.

The jinja2 version will be:

from jinja2 import Template

template = """
x = \\frac{{{ b }}}{{"{"}}{{ c }}{{"}"}}
"""
# {{"}"}} will render to }

data = {
    'a': 13,
    'b': 42,
    'c': 3
}

j2_template = Template(template)
print(j2_template.render(data))

You will notice that because both latex and jinja2 heavily rely on {}, the template string will be populated with {}, make it hard to read and modify.

In templatex, we can write the following code:

from templatex import Template

template = """
x = \\frac{@= b =@}{@= c =@}
"""

data = {
    'a': 13,
    'b': 42,
    'c': 3
}

j2_template = Template(template)
print(j2_template.render(data))

What this package changes

Changed Default Jinja2 configuration.

Template changes jinja2's default configuration to the following:

Enviroment(
    loader,
    trim_blocks=True,
    block_start_string='@@',
    block_end_string='@@',
    variable_start_string='@=',
    variable_end_string='=@',
    autoescape=False,
    comment_start_string='\#{',
    comment_end_string='}',
    line_statement_prefix='%%',
    line_comment_prefix='%#',
)

As a result, you only need to adopt the following syntax:

  • variable render: @= var_name =@
  • block logic: @@ for my_item in my_collection @@
  • comment: \#{ your comment }

Add a latex escape filter

Jinja2, by default, will perform auto HTML escaping on rendered variables, but I couldn't figure out how to change its default escape filter implementation. As a result, we disabled this feature and provided a latex escape filter so template authors can manually escape string variables as needed.

For example:

When rendering @= my_var =@, and if my_var="$5.0", the resulting latex code cannot be compiled due to the unescaped $ character. You can use @= my_var | escape_latex =@, where the escape_latex filter will escape $ character to valid latex: \$.

Other features

Since this is still the same jinja2 engine at its core, users should refer to the jinja2 site for documentation on other features.

Inspiration

I adopted the solution from this answer on stackoverflow, and added some modifications.

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

templatex-0.0.2.tar.gz (4.8 kB view details)

Uploaded Source

Built Distribution

templatex-0.0.2-py3-none-any.whl (4.1 kB view details)

Uploaded Python 3

File details

Details for the file templatex-0.0.2.tar.gz.

File metadata

  • Download URL: templatex-0.0.2.tar.gz
  • Upload date:
  • Size: 4.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.16

File hashes

Hashes for templatex-0.0.2.tar.gz
Algorithm Hash digest
SHA256 6ee31d1cba503d66bdc58af9bf182901353943fb59717b154d2b4f49a40fd793
MD5 48ef8daaf441a8a675a22accb66cdab8
BLAKE2b-256 b17b18ef04d0ff3ccc695b8f310e4aae1e6bc86d3e61d8569e81f96864313184

See more details on using hashes here.

File details

Details for the file templatex-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: templatex-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 4.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.16

File hashes

Hashes for templatex-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 0b47c135e4fbe6e24786aa13f3232545e457cc30f72c7fae3463077921694ff2
MD5 010619cbb58085e018303bf4bb5067a0
BLAKE2b-256 8db6b89349638abf75f67f393437b9642a032a973a3e220492bcd284b2343f0c

See more details on using hashes here.

Supported by

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