Skip to main content

Add python calculations to your LaTeX and Markdown documents.

Project description

Compudoc

Add the power of python to your LaTeX, Markdown, and more...

Features

Compudoc is a project with similar goals to pythontex, Codebraid, pweave and pyptex. It is most similar to pyptex, and if I had found pyptex earlier, I may not have written Compudoc.

Features include:

  • Like pyptex, compudoc is a preprocessor. All Python code is executed and replaced before LaTeX, Pandoc, mdSlides, etc is ran. You could use it to render Python files if you wanted.
  • As a preprocessor, compudoc can be used with all your existing tooling. Just run compudoc to produce the source file that would normally go into your pipeline.
  • Unlike pyptex, compudoc is not specific to LaTeX. Any text file can be rendered. LaTeX, Markdown, ReStructuredText, etc. can be rendered with Compudoc.
  • Jinja2 is used for injecting values from Python into the source document. That means you can use Jinja2 filters to make common formatting task cleaner.
  • Python code is executed in a separate interactive Python instance and incrementally between chunks of document text. That means you can define a variable x in one block of Python code, use that value in a Jinja2 template in your document, change the value of x in a later code block, and use it again in the document. The value inserted into the document will be the value of x at the point it is inserted.
  • Python code is embedded in the comments of your source document, so you can still run the unrendered source file through your toolchain.
  • If the source file you are rending does not support comments (there is no standard way to put comments in Markdown), you can define your own comment line identifier and have Compudoc strip them during the render process. This means you can use Compudoc to render any plain text source file without the final tool knowing anything about it.

How it works

compudoc processes plain text sources files by breaking the file into "chunks" of document text and python code. For example, a document with the text

Some text
% {{{
% import os
% }}}
Some more text
% {{{
% CWD = os.getcwd()
% }}}
The current directory is {{ CWD }}.

would be split into 5 chunks. The first chunk is the document text 'Some text\n', the second chunk is python code and so on.

Examples

Python code is embedded in your document's comments. Code blocks within comment blocks are marked with a '{{{' and '}}}' line. Here is a LaTeX example.

% arara: pdflatex

% start with vim --server latex %
\documentclass[]{article}

\usepackage{siunitx}
\usepackage{physics}
\usepackage{graphicx}
\usepackage{fullpage}

\author{C.D. Clark III}
\title{On...}
\begin{document}
\maketitle

% {{{ {}
% import pint
% ureg = pint.UnitRegistry()
% Q_ = ureg.Quantity
% }}}

Laser exposures are characterized by a power ($\Phi$), energy ($Q$), radiant exposure ($H$),
or irradiance ($E$). Each of these four radiometric quantities are related to each other
through the exposure area and duration.

% {{{ {}
% power = Q_(100,'mW')
% duration = Q_(0.25,'s')
% energy = (power * duration).to("mJ")
% }}}

For example, if a laser outputs a power of {{'{:Lx}'.format(power)}} for a
duration of {{duration | fmt("Lx")}}, then the energy delivered during the
exposure will be {{energy | fmt("Lx")}}.

\end{document}

Save this to a file named main.tex and run

$ compudoc main.tex

This will create a file named main-rendered.tex with the following content

% arara: pdflatex

% start with vim --server latex %
\documentclass[]{article}

\usepackage{siunitx}
\usepackage{physics}
\usepackage{graphicx}
\usepackage{fullpage}

\author{C.D. Clark III}
\title{On...}
\begin{document}
\maketitle

% {{{ {}
% import pint
% ureg = pint.UnitRegistry()
% Q_ = ureg.Quantity
% }}}

Laser exposures are characterized by a power ($\Phi$), energy ($Q$), radiant exposure ($H$),
or irradiance ($E$). Each of these four radiometric quantities are related to each other
through the exposure area and duration.

% {{{ {}
% power = Q_(100,'mW')
% duration = Q_(0.25,'s')
% energy = (power * duration).to("mJ")
% }}}

For example, if a laser outputs a power of \SI[]{100}{\milli\watt} for a
duration of \SI[]{0.25}{\second}, then the energy delivered during the
exposure will be \SI[]{25.0}{\milli\joule}.

\end{document}

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

compudoc-0.5.1.tar.gz (305.5 kB view details)

Uploaded Source

Built Distribution

compudoc-0.5.1-py3-none-any.whl (8.4 kB view details)

Uploaded Python 3

File details

Details for the file compudoc-0.5.1.tar.gz.

File metadata

  • Download URL: compudoc-0.5.1.tar.gz
  • Upload date:
  • Size: 305.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.5

File hashes

Hashes for compudoc-0.5.1.tar.gz
Algorithm Hash digest
SHA256 01244e191ee08252a7233821db0f70998d52b29a87ffdbf6ebe56e583e0875ca
MD5 bb5182fcd6513ff660c10b8190f7dc69
BLAKE2b-256 48aa2bf8b9b0974311ee71263dbdc636b0465513aca7023e274327c978597713

See more details on using hashes here.

File details

Details for the file compudoc-0.5.1-py3-none-any.whl.

File metadata

  • Download URL: compudoc-0.5.1-py3-none-any.whl
  • Upload date:
  • Size: 8.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.5

File hashes

Hashes for compudoc-0.5.1-py3-none-any.whl
Algorithm Hash digest
SHA256 6e03243227a1c7e928bd423476e4cc13fd513cb040580f089b5def47643d941e
MD5 bde5d4cb34599efac7de17614af170c3
BLAKE2b-256 40eaac91c9e493cb56fe2d3e5cb1fec0ee550af255d37174f5aba8c28f987021

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