C/C++

mLaTeX's C++ interface, latex::Session, and its C library, latex_ffi, with the one function it exports.

mLaTeX gives programs two interfaces. C++ programs use mLaTeX's own header, latex.hpp, and link the core library. Every other language goes through the C library, latex_ffi, whose header is plain C; Python is one of them.

InterfaceHeaderLinkFor
C++latex.hppcoreC++ programs built with mLaTeX
Cffi.hpplatex_ffiC, and any language with a foreign-function interface

C++

The library

mLaTeX is the core library, and its interface is latex.hpp, in the namespace latex. Link core from the mLaTeX build, as the mlatex program and every test do.

A session

#include "latex.hpp"

#include <iostream>

int main(int, char* arguments[]) {
    latex::Session session(latex::locate(arguments[0]));

    session.set("customer", "Acme Corporation");
    session.define({.name = "balance", .arity = 1, .handler = [](auto values) {
        return values[0] == "A-1042" ? std::string("12.50") : std::string("0.00");
    }});
    session.provide("letterhead/main.mtex", "\\newcommand{\\letterhead}{\\textbf{Acme}}");

    const bool clean = session.typeset(R"(\documentclass{article}
\usepackage{letterhead}
\begin{document}
\letterhead\par
Billed to \variable{customer}. Balance: \$\balance{A-1042}.
\end{document})");

    if (!clean) std::cerr << session.error;
    // session.pdf holds the PDF's bytes; session.pages each page's text.
}

Session(assets)

A session over an assets folder: the fonts, and the hyphenation patterns not built in. See Finding the assets.

set and unset

set(name, value) gives a value the document reads with \variable{name}, and unset(name) takes it back.

define and forget

define(command) gives a command the program implements, as a latex::Command:

FieldWhat it is
nameThe command's name, with or without its backslash.
arityHow many brace groups it reads, up to nine.
handlerCalled with the arguments' text; returns the text the command stands for.

forget(name) takes a command back.

provide and withdraw

provide(name, bytes) hands in a file from memory, under the name the document uses, and withdraw(name) takes it back.

typeset

typeset(document) makes a PDF and returns true when nothing was reported.

snippet

snippet(text) makes a PDF of a piece of LaTeX with no preamble, on a page cropped to it, and returns as typeset does. See snippet.

session.snippet(R"(\[ e^{i\pi} + 1 = 0 \])");
// session.pdf is one formula, on a page its size.

pdf, error and pages

MemberWhat it is
pdfThe last PDF made, or empty when none could be.
errorWhat went wrong last time, one error per line.
pagesEach page's text, for checking or indexing.

host

host is everything the next document will be handed: variables, commands, files, directories and offline. The operations above change it, and it can be changed directly too:

session.host.directories.push_back("styles");   // read packages and inputs from here too
session.host.offline = true;                     // refuse every network request

Finding the assets

latex::locate

latex::locate(binary) walks up from a path to the first folder holding assets/fonts, the way the mlatex program finds its own fonts. Pass it your program's path, or name the assets folder yourself.

Without a session

For one document at a time, two functions do the same work:

// A document on disk into a PDF on disk, as the command line does.
bool clean = latex::compose(assets, "paper.tex", "paper.pdf", host, &std::cout, std::cerr);

// A document in memory into a PDF in memory, quietly.
std::string pdf;
bool clean = latex::typeset(assets, document, pdf, host, std::cerr);

latex::compose

compose reads a document from disk and writes its PDF to disk. It takes where its report goes (nullptr for none) and where errors go; an empty destination makes a draft, which writes no PDF.

latex::typeset

typeset makes a PDF from a document in memory into a string, reporting only errors.

Both take an optional last argument that receives each page's text.


C

The files

The build leaves a shared library beside the program:

SystemLibrary
Windowslatex_ffi.dll
Linuxliblatex_ffi.so
macOSliblatex_ffi.dylib

Its header is ffi.hpp, plain C as well as C++.

engine()

The library exports exactly one function:

const Latex* engine(void);

It returns a struct of function pointers, the same operations as the C++ session under the same names.

Strings and types

Every string is UTF-8 and zero-terminated, paths included, and every function returns a plain int or pointer, so the library can be bound from Python's ctypes, C#'s P/Invoke, Rust's extern "C", Go's cgo or anything else that can call C.

The operations

compose and dispose

compose(assets) makes a session. Pass NULL to find the fonts beside the library itself; it returns NULL if they cannot be found. dispose(session) ends a session, and ignores NULL.

set and unset

set(session, name, value) gives a value read with \variable{name}, replacing one set before. unset(session, name) takes it back.

define and forget

define(session, name, arity, handler, data) gives a command the program implements, with up to nine arguments. forget(session, name) takes it back.

A handler is called with what was given as data, the arguments' text and their count, and where to write what the command stands for:

typedef void (*Handler)(void* data, const char* const* arguments, size_t count, Output* output);

The arguments are valid for the length of the call.

write

write(output, text, length) is called by a handler to write what its command stands for. Each call adds to what came before, and writing nothing makes the command stand for nothing.

provide and withdraw

provide(session, name, bytes, length) hands in a file from memory; the bytes are copied. withdraw(session, name) takes it back.

typeset

typeset(session, document, length, &pdf, &size) makes a PDF and returns 1 when nothing was reported. The PDF belongs to the session and stays valid until it next typesets or is disposed of.

error

error(session) is what went wrong last time, one error per line. It is never NULL.

offline

offline(session, on) keeps the session's documents off the network when on is 1.

snippet

snippet(session, text, length, &pdf, &size) makes a PDF of a piece of LaTeX with no preamble, on a page cropped to it. It returns and hands back the PDF as typeset does. It is the struct's last field, after offline.

Return values

Functions that change a session return 1 on success and 0 when an argument was missing.

An example

#include "ffi.hpp"
#include <stdio.h>
#include <string.h>

static void balance(void* data, const char* const* arguments, size_t count, Output* output) {
    char text[32];
    int length = snprintf(text, sizeof text, "%.2f", strcmp(arguments[0], "A-1042") == 0 ? 12.5 : 0.0);
    engine()->write(output, text, (size_t)length);
}

int main(void) {
    const Latex* latex = engine();
    Session* session = latex->compose(NULL);
    latex->set(session, "customer", "Acme Corporation");
    latex->define(session, "balance", 1, balance, NULL);

    const char* document =
        "\\documentclass{article}\\begin{document}"
        "Billed to \\variable{customer}. Balance: \\$\\balance{A-1042}."
        "\\end{document}";

    const unsigned char* pdf;
    size_t size;
    if (!latex->typeset(session, document, strlen(document), &pdf, &size)) {
        fprintf(stderr, "%s", latex->error(session));
    }
    if (pdf) {
        FILE* file = fopen("invoice.pdf", "wb");
        fwrite(pdf, 1, size, file);
        fclose(file);
    }
    latex->dispose(session);
    return 0;
}

Including it from C++

From C++, the same declarations are in the namespace protocols.

Finding the fonts

compose(NULL)

compose(NULL) finds the assets folder by walking up from where the library itself was loaded.

A folder of your own

If your program keeps the library somewhere else, pass the assets folder's path to compose instead.