"latex2png" can also be implemented this way:
When executing external programs that generate one or more files, run it in a temporary directory. coreutils'
realpath is extremely useful, because when run on input file or directory name arguments, it prints out paths you can use after moving to the temporary directory.
From C, one can obviously use either
popen() or ones own
fork() + exec(), but my point is that it should run a script that begins with something like
#!/bin/sh Work=$(mktemp -d) || exit 1 trap "cd; rm -rf '$Work'" EXIT # Source=$(realpath "$1") cd "$Work" # whatever the script should accomplish ...Such scripts are commonly installed under in
/usr/share/application/ in Linux, BSDs, and Unix in general. The
/bin/sh refers to any roughly POSIX-compatible shell, like
dash or
bash. The trap ensures the temporary directory is always removed when the script exits, even if it exits due to an error or signal delivery (except kill). I like to provide the input and output via pipes on the standard streams, although that does require your own fork()+exec() implementation.
Because such scripts are administrator-editable without recompiling anything, you then don't need to worry about passing configuration environment variables (like say
LATEX pointing to the actual typesetter binary), just provide one script for each of the OSes you support, and document their requirements. (I only bother with GNU/Linux, since they're common and easily adapted to other systems.)
(By document, I mean: "Rendering LaTeX is done via /usr/share/
application/latex-to-svg, which requires
/bin/sh (POSIX-compatible shell),
mktemp and
realpath from coreutils, and
/usr/bin/latex.")
I also really like the SVG approach. Note that there is also
nanosvg (one C header file only; another for a simple rasterizer), making the SVG approach interesting even on resource-limited older architectures.