convert math-equations to images (C-only tool) - Page 1
Forum section: electronics / general

author: DiTBho
author_rank: Super Contributor
timestamp: January 21, 2024, 01:12:01 pm
content: I am writing a parser that reads an article-description and generates HTML.Lists, tables, gallery of pictures, paragraphs,codenza-blocks(1), do already work, but I need a way to include math formulas.One idea is to simply convert LaTex equations to images, which can then be simply included into the article withCode:pic/LaTex_eq_xxxx.{ png, gif}(yep, it's similar to the synthax we all use on this forum)So, something likethis, but, I need this "LaTex2image" written in C.Any idea?(1)codenza-blocksare similar to [ code ] [ / code ], to be used to show C sources, but withSource Highlighting.

author: DiTBho
author_rank: Super Contributor
timestamp: January 21, 2024, 01:18:18 pm
content: I know "LibreOffice Math" is one of the components of the "LibreOffice office suite", specifically it's a formula editor that you can launch when working on your text documents, spreadsheets, presentations, and drawings.I forgot to mention that I need a tool that I can invoke from the Bash shell, or, better still, which I can integrate (source merge) into my main C application.

author: DavidAlfa
author_rank: Super Contributor
timestamp: January 21, 2024, 02:00:58 pm
content: https://latex2rtf.sourceforge.net/latex2png.htmlhttps://sourceforge.net/p/latex2rtf/code/

author: DiTBho
author_rank: Super Contributor
timestamp: January 21, 2024, 08:24:05 pm
content: so, latex2png is a bash-script provied by latex2rtf, which invokes- perl- LaTex

author: DiTBho
author_rank: Super Contributor
timestamp: January 22, 2024, 03:17:18 pm
content: "latex2png" can also be implemented this way:Code:function clean(){rm -f *.pdfrm -f *.auxrm -f *.logrm -f *.log1rm -f *.log2}function do_it(){cleanpdflatex --shell-escape "$1"result# trims border to contentconvert -trim ${1/tex/png} ${1/tex/png}resultclean}do_it $1(bash script, "latex2png")pdflatex -> { dev-texlive/texlive-latex (v2021), app-text/texlive-core (v2021-r7), dev-texlive/texlive-latexextra (v2021), ...}convert -> { media-gfx/imagemagick (v7.1.1.11) }Code:\documentclass[convert=true]{standalone}\usepackage{amsmath, xparse}\begin{document}$\begin{bmatrix}11 & 12 & 13\\21 & 22 & 23\\31 & 32 & 33\end{bmatrix}$$\begin{pmatrix}11 & 12 & 13\\21 & 22 & 23\\31 & 32 & 33\end{pmatrix}$$\left\{\begin{matrix}11 & 12 & 13\\21 & 22 & 23\\31 & 32 & 33\end{matrix}\right.$\end{document}(matrices, working example)thisis an usefull website with an online LaTex editor.

author: Andy Watson
author_rank: Super Contributor
timestamp: January 22, 2024, 03:58:43 pm
content: Quote from: DiTBho on January 22, 2024, 03:17:18 pmI would be very careful about including that in a script file. Whilst the aux and log files are temporary, there's a non-zero chance of the directory containing other pdfs that are wanted!

author: DiTBho
author_rank: Super Contributor
timestamp: January 22, 2024, 04:39:01 pm
content: Quote from: Andy Watson on January 22, 2024, 03:58:43 pmYup, it's a proof of concept, in theory pdflatex with that directiveCode:...Output written on eq6.pdf (1 page, 17572 bytes).Transcript written on eq6.log....Class standalone:Output written on eq6.png.(see, it generates both)in the .tex source shouldn't generate any .pdf but only a .pngCode:function clean(){local target1=${1/tex/pdf}local target2=${1/tex/aux}local target3=${1/tex/log}local target4=${1/tex/log1}local target5=${1/tex/log2}rm -f $target1rm -f $target2rm -f $target3rm -f $target4rm -f $target5}anyway, somethine like this, is safer for the user

author: DiTBho
author_rank: Super Contributor
timestamp: January 22, 2024, 04:50:33 pm
content: So, we have now a Console tool that does the job and that's great!But, I wonder: what do modern graphing calculators use to display math?They certainly don't have a LaTex engine but something much simpler and more dedicated.That would be exactly what I need, while LaTex is very cumbersome in terms of source compilation, dependencies and configuration.

author: berke
author_rank: Frequent Contributor
timestamp: January 22, 2024, 05:56:50 pm
content: Have a look into KaTeX:https://github.com/KaTeX/KaTeXIt's a self-contained rendering engine written in Javascript, however you could embed a JS engine within your code.

author: IanB
author_rank: Super Contributor
timestamp: January 22, 2024, 06:01:59 pm
content: No mention of MathML?https://developer.mozilla.org/en-US/docs/Web/MathML

author: SiliconWizard
author_rank: Super Contributor
timestamp: January 22, 2024, 09:18:34 pm
content: Quote from: IanB on January 22, 2024, 06:01:59 pmYes, you need a web browser though. Don't know if that fits the OP's needs, if they want a standalone rendering library, or something like that.If you have access to a browser, MathJax is a popular option:https://www.mathjax.org/. But it's Javascript.There is this project in C:https://github.com/janmarthedal/mathml2svghaven't tried it yet. Its dependencies are cairo and pango, which should be available, or at least build without too much effort on many systems. Renders as SVG, which is great as it's vector graphics. You can then use a SVG renderer to get a bitmap if you can't display SVG directly.

author: DiTBho
author_rank: Super Contributor
timestamp: January 22, 2024, 09:36:49 pm
content: Quote from: SiliconWizard on January 22, 2024, 09:18:34 pmas less dependencies as possilbe, this project looks *VERY* interesting!lib_pango and lib_cairo are not a problem

author: Nominal Animal
author_rank: Super Contributor
timestamp: January 24, 2024, 05:48:26 am
content: Quote from: DiTBho on January 22, 2024, 03:17:18 pmWhen executing external programs that generate one or more files, run it in a temporary directory. coreutils'realpathis 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 eitherpopen()or ones ownfork() + exec(), but my point is that it should run a script that begins with something like#!/bin/shWork=$(mktemp -d) || exit 1trap "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/shrefers to any roughly POSIX-compatible shell, likedashorbash. 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 sayLATEXpointing 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),mktempandrealpathfrom coreutils, and/usr/bin/latex.")I also really like the SVG approach. Note that there is alsonanosvg(one C header file only; another for a simple rasterizer), making the SVG approach interesting even on resource-limited older architectures.

author: DiTBho
author_rank: Super Contributor
timestamp: January 25, 2024, 02:05:43 pm
content: I am completing my own web-server for XINU, but it will also backported to GNU/Linux ... and maybe also to Haiku Beta4, so I can do whatever shenanigans I needIn the end there is no need to run mod-php, mod-python, ... and that crazy stuff (especially on Apache), there is no "web language" ( { php, python, ruby, ... } ) to run "web pages", everything is based on C, statically compiled and linked as XINU is much more limited than GNU/Linux, but that's enough to handle scientific articles described similarly to how we write "posts" on forums, simply powered with more tokens, such as math-tokens to describe mathematical formulas, lists and matrices.

author: Nominal Animal
author_rank: Super Contributor
timestamp: January 25, 2024, 03:02:32 pm
content: Quote from: DiTBho on January 25, 2024, 02:05:43 pmMathJax (like \$V = \sqrt{P R}\$) is rendered completely on the client side using JavaScript interpreted on the client browser, and requires no support from the server whatsoever. For full LaTeX rendering, look atLaTeX.js; but note that then the LaTeX source is provided to the viewers also.

author: DiTBho
author_rank: Super Contributor
timestamp: January 25, 2024, 04:47:00 pm
content: The idea behind converting math-formulas(1) into images is to also provide a pdf of the articles.Partially already implemented, the server does this(2) when the user clicks on "download as pdf"(3).(1) with: fractions, roots with exponent, { canonical, line, circuited, surface, volume } integrals, { 2d, n-dimensional } derivatives, matrices, Hamiltonians, producers, summations, trigonometry and hyperbolic functions, systems of equations with complex numbers, and lots of Greek letters(2) LRU algorithm, the most clicked are stored as normal files, while less clicked picture-files are deleted, as they can be re-created on demand.(3) the pdfs are not exactly equals, such as graphics and fonts, to the HTML version ... I have to work on this a little more

author: Nominal Animal
author_rank: Super Contributor
timestamp: January 25, 2024, 08:30:46 pm
content: There isjsPDF, which is a client-side JavaScript library for generating PDF files; I don't know if itshtmlmodule works well with latex.js.However, I do believeSwiftLaTeXwould let you do the LaTeX -> PDF on the client side, in the client browser. Might be worth looking into.In general, usingemscripten(emsdk) to compile C code to run in a browser on the client side (controlled by the web page your server emits, of course) is something might wish to look into.