Skip to main content

Compile LaTeX documents with pytask.

Project description

pytask-latex

PyPI PyPI - Python Version image image PyPI - License image image pre-commit.ci status image


pytask-latex allows you to compile LaTeX documents with pytask

It also uses latex-dependency-scanner to automatically infer the dependencies of the LaTeX document such as images, bibliographies and other .tex files which are necessary to compile the LaTeX document.

Installation

pytask-latex is available on PyPI and Anaconda.org. Install it with

$ pip install pytask-latex

# or

$ conda install -c conda-forge pytask-latex

You also need to have latexmk installed which determines the necessary number of compilation steps (here is an explanation for what latexmk achieves). To test whether it is installed, type the following on the command line

$ latexmk --help

If an error is shown instead of a help page, you can install latexmk with one of the popular LaTeX distributions, like TeX Live, MiKTeX, MacTeX or others.

Usage

Compiling your PDF can be as simple as writing the following task.

from pathlib import Path
from pytask import mark


@mark.latex(script=Path("document.tex"), document=Path("document.pdf"))
def task_compile_latex_document():
    pass

Use @mark.latex to indicate that this task compiles a LaTeX document. The script and the document keywords provide absolute paths or paths relative to the task module to the LaTeX file and the compiled document.

Dependencies and Products

Dependencies and products can be added as usual. Read this tutorial.

For example, with the @task decorator. (The choice of the kwarg name, here path, is arbitrary.)

from pytask import mark
from pytask import task
from pathlib import Path


@task(kwargs={"path": Path("path_to_another_dependency.tex")})
@mark.latex(script=Path("document.tex"), document=Path("document.pdf"))
def task_compile_latex_document():
    pass

Customizing the compilation

pytask-latex uses latexmk by default to compile the document because it handles most use-cases automatically. The following is equivalent to a bare @mark.latex decorator.

@mark.latex(
    script=Path("document.tex"),
    document=Path("document.pdf"),
    compilation_steps="latexmk",
)
def task_compile_latex_document(): ...

The @mark.latex decorator has a keyword argument called compilation_steps which accepts which accepts strings or list of strings pointing to internally implemented compilation steps. Using strings will use the default configuration of this compilation step. It is equivalent to the following.

from pytask_latex import compilation_steps as cs


@mark.latex(
    script=Path("document.tex"),
    document=Path("document.pdf"),
    compilation_steps=cs.latexmk(
        options=("--pdf", "--interaction=nonstopmode", "--synctex=1", "--cd")
    ),
)
def task_compile_latex_document(): ...

In this example, compilation_steps.latexmk is a compilation step constructor which accepts a set of options and creates a compilation step function.

You can pass different options to change the compilation process with latexmk. Here is an example for generating a .dvi.

@mark.latex(
    script=Path("document.tex"),
    document=Path("document.pdf"),
    compilation_steps=cs.latexmk(
        options=("--dvi", "--interaction=nonstopmode", "--synctex=1", "--cd")
    ),
)
def task_compile_latex_document(): ...

compilation_step.latexmk(options) generates a compilation step which is a function with the following signature:

from pathlib import Path
import subprocess


def custom_compilation_step(path_to_tex: Path, path_to_document: Path) -> None:
    ...
    subproces.run(..., check=True)

You can also pass your custom compilation step with the same signature to the compilation_steps keyword argument of the decorator.

Each compilation step receives the path to the LaTeX source file and the path to the final document which it uses to call some program on the command line to run another step in the compilation process.

In the future, pytask-latex will provide more compilation steps for compiling bibliographies, glossaries and the like.

Repeating tasks with different scripts or inputs

You can compile multiple LaTeX documents as well as compiling a single .tex document with different command line arguments.

The following task compiles two latex documents.

for i in range(2):

    @task
    @mark.latex(script=Path(f"document_{i}.tex"), document=Path(f"document_{i}.pdf"))
    def task_compile_latex_document():
        pass

If you want to compile the same document with different command line options, you have to include the latex decorator in the parametrization. Pass a dictionary for possible compilation steps and their options.

for format_ in ("pdf", "dvi"):

    @task
    @mark.latex(
        script=Path("document.tex"),
        document=Path(f"document.{format_}"),
        compilation_steps=cs.latexmk(
            (f"--{format_}", "--interaction=nonstopmode", "--synctex=1", "--cd")
        ),
    )
    def task_compile_latex_document():
        pass

Configuration

infer_latex_dependencies

pytask-latex tries to scan your LaTeX document for included files with the help of latex-dependency-scanner if the following configuration value is true which is also the default.

[tool.pytask.ini_options]
infer_latex_dependencies = true

Since the package is in its early development phase and LaTeX provides a myriad of ways to include files as well as providing shortcuts for paths (e.g., \graphicspath), there are definitely some rough edges left. File an issue here or in the other project in case of a problem.

Changes

Consult the release notes to find out about what is new.

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

pytask_latex-0.4.3.tar.gz (8.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pytask_latex-0.4.3-py3-none-any.whl (11.3 kB view details)

Uploaded Python 3

File details

Details for the file pytask_latex-0.4.3.tar.gz.

File metadata

  • Download URL: pytask_latex-0.4.3.tar.gz
  • Upload date:
  • Size: 8.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for pytask_latex-0.4.3.tar.gz
Algorithm Hash digest
SHA256 6f7f0538d89070bd85e1b93a3aa6aa516ddc90df9a8df3968b12143ae38ac190
MD5 d1e7f1af3fb42fbe4bac87655923ea5e
BLAKE2b-256 250a86b66b60633f853c13855b367a04849883e6c793b7fe78780003a2306e23

See more details on using hashes here.

Provenance

The following attestation bundles were made for pytask_latex-0.4.3.tar.gz:

Publisher: publish-to-pypi.yml on pytask-dev/pytask-latex

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pytask_latex-0.4.3-py3-none-any.whl.

File metadata

  • Download URL: pytask_latex-0.4.3-py3-none-any.whl
  • Upload date:
  • Size: 11.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for pytask_latex-0.4.3-py3-none-any.whl
Algorithm Hash digest
SHA256 88ce2e1e9b8e5a2fddbba34238a39f46cb3d7b2b0440b99acd9ac63a90ed1f1a
MD5 5a518f36144130c120ceaf08734ca897
BLAKE2b-256 1ae1dc6ecf2642eb1e4735df18e699f3dff36b482365d683b6656e43cd8bc26a

See more details on using hashes here.

Provenance

The following attestation bundles were made for pytask_latex-0.4.3-py3-none-any.whl:

Publisher: publish-to-pypi.yml on pytask-dev/pytask-latex

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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