0
votes

Problem

I am developing a Rust program which has a GTK3 GUI using the given rust-gtk-binding. The program should be cross-platform (at least Linux and Windows). The GUI should be able to show custom plaintexts and small LaTeX-snippets to allow the use math environments (small means sizes of one formula as an element to display). Therefore, I need a way to convert LaTeX-code into something which can be displayed by the GUI.

Ideas and their problems

I can see two approaches displaying LaTeX:

  • Compile the LaTeX-source into pdf and then into some image type. It should be possible to use Ghostscript to get the image. But I do not know how to generate the pdf in a way which is lightweight (does not include rather large packages like miktex) and cross-platform. This option could also be overkill as there is no need to dynamically download special packages, a good math support would be sufficient. The positive side is that rendering an image in GTK should be easy.
  • Use KaTeX which should be sufficient for math environments. I was able to install matching crates and generate HTML source from some formulas. But here it becomes difficult to render the result as GTK has no native way for displaying HTML. As it would be difficult to integrate a HTML-engine into the GUI it would be optimal to find a tool which is able to render HTML to an image type which then can be displayed.

Now I have two ways both using an intermediate step where for common LaTeX the first step is difficult and for KaTeX the second step displays a problem. For both approaches' difficult steps I could not find any feasible solution. Are there any libraries or similar I could not find or are there any different approaches? It would be perfectly sufficient to be able to render a single formula, I just want to avoid such massive and difficult overkills like using a complete LaTeX compiler or half a browser to render HTML.

1
It's not that active anymore, but maybe you can generate bindings for lasem? - nielsdg

1 Answers

0
votes

After searching and evaluating many more approaches I got a solution which is kind of good while also having some major drawbacks:

First of all, I use TinyTex as LaTeX environment. I did not restrict the use of LaTeX to e.g. math environments. TinyTex offers support for major platforms while being lightweight and portable. Additional LaTeX packages have to be installed manually which allows me to decide which ones are being shipped with my application. The negative side is that while TinyTex is lightweight for a LaTeX environment it still is rather big for its purpose here (about 250MB).

I installed the required packages to use \documentclass[preview]{standalone} to get an already cropped pdf. Afterwards I use Ghostscript to get a png-image for the generated pdf. I did not use language bindings and instead just went std::process::Command.

The following lines should be sufficient to convert test.tex into test.png with the portable TinyTex installation and Ghostscripts gswin64c.exe present in subfolders of the project's directory under Windows: (As TinyTex and Ghostscript also exist for other OS the given example can easily be changed to work on other systems)

use std::process::Command;

fn main() {
    let output = Command::new("TinyTex\\bin\\win32\\pdflatex.exe")
        .args(&["test.tex"])
        .output()
        .expect("Some error message 1");

    println!("{}", String::from_utf8(output.stdout).unwrap());

    let output = Command::new("gs\\gswin64c.exe")
        .args(&[
            "-dNOPAUSE",
            "-dBATCH",
            "-sDEVICE=png16m",
            "-r1000",
            "-sOutputFile=test.png",
            "test.pdf",
        ])
        .output()
        .expect("Some error message 2");

    println!("{}", String::from_utf8(output.stdout).unwrap());
}

Of course this is no particulary good and useful code at this stage but it shows how to proceed with the given problem and I wanted to leave it here in case anyone with a similar problems finds this post.