Skip to main content

This is a recent effort at writing a very light Latex build wrapper for Python 3 using Jinja2 templates. It was inspired by the build philosophy found in http://pythonhosted.org/latex/ . However, the original latex library made it difficult to do complex, non-pdf builds (such as compiling to HTML). This repository creates simple components that can be used to build Latex from Jinja2 templates without losing control of the lower-level command line tools provided with latex (like pdflatex and htlatex). The usefulness of the library is split into two components:

  1. Rendering Jinja2 templates for Latex

  2. Compiling the rendered template with Latex

Currently, the following output formats are supported out of the box:

  1. .pdf

  2. .html

  3. .docx

That said, given the granular control offered by this package, as long as you stick with formats that are supported by build tools distributed with texlive, you should be able to easily write your own output format extensions using this package as a base.

Installation

This project is available at https://pypi.python.org/pypi/latexbuild and may be installed with pip. I strongly recommend the usage of a virtual environment.

# Create a virtual environment
virtualenv -p python3.4 venv

# Source the virtual environment
source venv/bin/activate

# Install latexbuild in the virtual environment
pip install latexbuild

Rendering Jinja2 templates for Latex

Latex is a great typesetting engine for large, complex projects. However, its native build tools lack a straightforward templating engine where multiple people can collaborate on a similar document. For example, imagine a scenario where five professors in the same department need to create a course syllabus. Grading requirements may be standardized at the department level, which individual lessons are up to the professors. If course syllabuses were created using Jinja2 templating and Latex, the department could create a base template that listed the department standards and ask the professors to extend the base template for each course’s syllabus. That way, the professors need not worry about cross-course content or department-wide formatting requirements: these would be handled by the base template.

Assume the department saves the following content in template.tex:

% FILENAME: template.tex
\documentclass[12pt]{article}

\title{Economics Department: \BLOCK{block title}\BLOCK{endblock}}
\author{\vspace{-5ex}}
\date{\vspace{-5ex}}

\begin{document}
\maketitle

\section{Department Introduction}
This is a standard message from the department
that will appear in every syllabus in exactly this place.
Professors shouldn't have to think about this.

\section{Greatness}
\BLOCK{block greatness}\BLOCK{endblock}

\section{Boredom}
\BLOCK{block boredom}\BLOCK{endblock}

\end{document}

Now that the template has been created, the accounting professor can create his syllabus in the following way:

%- extends "template.tex"

% FILENAME: accounting.tex

%- block title
Accounting
%- endblock

%- block greatness
This is a great, accounting-specific block
%- endblock

%- block boredom
This is a boring, accounting-specific block
%- endblock

Additionally, the statistics professor can create her syllabus as follows:

%- extends "template.tex"

% FILENAME: statistics.tex

%- block title
Statistics for juggernauts
%- endblock

%- block greatness
This is a great, statistics-specific block
%- endblock

%- block boredom
This is a boring, statistics-specific block
%- endblock

Most Jinja2 templating functionality is supported, using the same syntax alterations as the latex python package referenced in this README’s introduction. For example, if you would like to loop over values and place them in a Latex list, you may use the following code.

This snippet provides an ordered list displaying the
list variable passed from Python:

\begin{enumerate}
  %- for variable in variable_list
  \item \VAR{variable}
  %- endfor
\end{enumerate}

Alternatively, this snippet provides an unordered list
displaying the list variable passed from Python:

\begin{itemize}
  %- for variable in variable_list
  \item \VAR{variable}
  %- endfor
\end{itemize}

This section will continue being updated over time with more examples.

Building Latex Output

For the simplest project, you can build a Jinja2-templated latex source repository with the following code:

from latexbuild import build_pdf, build_html, build_docx, render_latex_template

PATH_JINJA2 = "/path/to/your/latex/jinja2/root"
PATH_TEMPLATE_RELATIVE_TO_PATH_JINJA2 = "template/filepath.tex"
PATH_OUTPUT_PDF = "/path/to/your/output/directory/MYOUTPUTFILE.pdf"
PATH_OUTPUT_HTML = "/path/to/your/output/directory/MYOUTPUTFILE.html"
PATH_OUTPUT_DOCX = "/path/to/your/output/directory/MYOUTPUTFILE.docx"

# Build Jinja2 template, compile result latex, move compiled file to output path,
# and clean up all intermediate files
build_pdf(PATH_JINJA2, PATH_TEMPLATE_RELATIVE_TO_PATH_JINJA2, PATH_OUTPUT_PDF)
build_html(PATH_JINJA2, PATH_TEMPLATE_RELATIVE_TO_PATH_JINJA2, PATH_OUTPUT_HTML)
build_docx(PATH_JINJA2, PATH_TEMPLATE_RELATIVE_TO_PATH_JINJA2, PATH_OUTPUT_DOCX)

# If you just want the rendered template's text in a python variable,
# do the following (assuming you have no variables to pass):
render_latex_template(PATH_JINJA2, PATH_TEMPLATE_RELATIVE_TO_PATH_JINJA2)

# If your template renders Jinja2 variables, most interfaces provide
# a dictionary parameter. See below for an example for simply
# rendering the template's text in Python
DICT_VALS = {
    'var1': 'my variable 1 value',
    'list_var': ['item 1 for analysis', 'item 2 for analysis']
    }
render_latex_template(
    PATH_JINJA2,
    PATH_TEMPLATE_RELATIVE_TO_PATH_JINJA2,
    DICT_VALS,
    )

For more complex builds, the system is designed to accept whatever command line arguments you wish to use. Please see the source file latexbuild/build.py and read the LatexBuild class’s documentation for more information.

Supported / tested systems

Python 3, Linux

Written by

Samuel Roeca

Release files for latexbuild 0.2.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for latexbuild 0.2.2
File Size Uploaded
latexbuild-0.2.2.tar.gz 9.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for latexbuild 0.2.2
File Interpreter ABI Platform
latexbuild-0.2.2-py3-none-any.whl Python 3 none any Details

Total release size: 24.7 kB

Release files / latexbuild-0.2.2.tar.gz

Download URL latexbuild-0.2.2.tar.gz
Size 9.9 kB
Tags Source
SHA-256 checksum
How to use checksums
db6458cdc073c23d142ccd9466f8127d5d9e3bb22c1df465f3c1bfa66756d343
BLAKE2b-256 checksum
How to use checksums
78863a700f1dfbed388b9a7f6cd57e0d43da42856aaefc67568a42c5c304a3e1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / latexbuild-0.2.2-py3-none-any.whl

Download URL latexbuild-0.2.2-py3-none-any.whl
Size 14.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8e12f423b54f04fa7cc20461598ab31d4314e63ec003fdebe4fb2b03ca0194ec
BLAKE2b-256 checksum
How to use checksums
8da9245729cb8da78211f3603926bc5ca31dbc6188b42746dcdd98344504b84e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.2.2 This release

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.1

2 release files

0.1.0

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page