fjebaker/zigtex
Embeddable LaTeX to SVG in Zig.
ZigTeX is a wrapper around MicroTeX with a custom SVG renderer. ZigTeX principally gives you one function, which takes some LaTeX code and spits out an SVG:
const std = @import("std");
const ztex = @import("zigtex");
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const allocator = gpa.allocator();
var render = try ztex.TexSvgRender.init(allocator, .{});
defer render.deinit();
const tex =
\\\begin{equation}
\\ \hat{F}(\nu) = \int_{-\infty}^\infty e^{-i 2\pi t \nu} f(t) \text{d}t
\\\end{equation}
;
const output = try render.parseRender(allocator, tex, .{});
defer allocator.free(output);
var f = try std.fs.cwd().createFile("example.svg", .{});
defer f.close();
try f.writeAll(output);
}
This produces the title image on a transparent background.
To use in your Zig project, add this project as a dependency
zig fetch --save https://github.com/fjebaker/zigtex/archive/main.tar.gz
Then modify your build.zig
in the usual way:
const zigtex_dep = b.dependency(
"zigtex",
.{.optimize = optimize, .target = target},
);
// ...
exe.root_module.addImport("zigtex", zigtex_dep.module("zigtex"));