Author Topic: convert math-equations to images (C-only tool)  (Read 2772 times)

0 Members and 1 Guest are viewing this topic.

Online DiTBhoTopic starter

  • Super Contributor
  • ***
  • Posts: 3915
  • Country: gb
convert math-equations to images (C-only tool)
« on: January 21, 2024, 01:12:01 pm »
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 with
Code: [Select]
[img]pic/LaTex_eq_xxxx.{ png, gif}[/img]
(yep, it's similar to the synthax we all use on this forum  ;D )

So, something like this, but, I need this "LaTex2image" written in C.

Any idea?  :-//


(1) codenza-blocks are similar to [ code ] [ / code ], to be used to show C sources, but with Source Highlighting.
The opposite of courage is not cowardice, it is conformity. Even a dead fish can go with the flow
 

Online DiTBhoTopic starter

  • Super Contributor
  • ***
  • Posts: 3915
  • Country: gb
Re: convert math-equations to images (C-only tool)
« Reply #1 on: January 21, 2024, 01:18:18 pm »
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.
The opposite of courage is not cowardice, it is conformity. Even a dead fish can go with the flow
 

Offline DavidAlfa

  • Super Contributor
  • ***
  • Posts: 5912
  • Country: es
Hantek DSO2x1x            Drive        FAQ          DON'T BUY HANTEK! (Aka HALF-MADE)
Stm32 Soldering FW      Forum      Github      Donate
 
The following users thanked this post: DiTBho

Online DiTBhoTopic starter

  • Super Contributor
  • ***
  • Posts: 3915
  • Country: gb
Re: convert math-equations to images (C-only tool)
« Reply #3 on: January 21, 2024, 08:24:05 pm »
so, latex2png is a bash-script provied by latex2rtf, which invokes
- perl
- LaTex

The opposite of courage is not cowardice, it is conformity. Even a dead fish can go with the flow
 

Online DiTBhoTopic starter

  • Super Contributor
  • ***
  • Posts: 3915
  • Country: gb
Re: convert math-equations to images (C-only tool)
« Reply #4 on: January 22, 2024, 03:17:18 pm »
"latex2png" can also be implemented this way:

Code: [Select]
function clean()
{
    rm -f *.pdf
    rm -f *.aux
    rm -f *.log
    rm -f *.log1
    rm -f *.log2
}

function do_it()
{
    clean

    pdflatex --shell-escape "$1"
    result

    # trims border to content
    convert -trim ${1/tex/png} ${1/tex/png}
    result

    clean
}

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: [Select]
\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)

this is an usefull website with an online LaTex editor.
The opposite of courage is not cowardice, it is conformity. Even a dead fish can go with the flow
 

Online Andy Watson

  • Super Contributor
  • ***
  • Posts: 2086
Re: convert math-equations to images (C-only tool)
« Reply #5 on: January 22, 2024, 03:58:43 pm »
Code: [Select]
function clean()
{
    rm -f *.pdf
    ... ... ...
}


I 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! ;)
 

Online DiTBhoTopic starter

  • Super Contributor
  • ***
  • Posts: 3915
  • Country: gb
Re: convert math-equations to images (C-only tool)
« Reply #6 on: January 22, 2024, 04:39:01 pm »
I would be very careful about including that in a script file

Yup, it's a proof of concept, in theory pdflatex with that directive

Code: [Select]
...
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 .png  :-//



Code: [Select]
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 $target1
    rm -f $target2
    rm -f $target3
    rm -f $target4
    rm -f $target5
}
anyway, somethine like this, is safer for the user  :D
« Last Edit: January 22, 2024, 04:48:59 pm by DiTBho »
The opposite of courage is not cowardice, it is conformity. Even a dead fish can go with the flow
 

Online DiTBhoTopic starter

  • Super Contributor
  • ***
  • Posts: 3915
  • Country: gb
Re: convert math-equations to images (C-only tool)
« Reply #7 on: January 22, 2024, 04:50:33 pm »
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.

The opposite of courage is not cowardice, it is conformity. Even a dead fish can go with the flow
 

Offline berke

  • Frequent Contributor
  • **
  • Posts: 258
  • Country: fr
  • F4WCO
Re: convert math-equations to images (C-only tool)
« Reply #8 on: January 22, 2024, 05:56:50 pm »
Have a look into KaTeX: https://github.com/KaTeX/KaTeX

It's a self-contained rendering engine written in Javascript, however you could embed a JS engine within your code.
 
The following users thanked this post: DiTBho

Online IanB

  • Super Contributor
  • ***
  • Posts: 11892
  • Country: us
Re: convert math-equations to images (C-only tool)
« Reply #9 on: January 22, 2024, 06:01:59 pm »
 
The following users thanked this post: DiTBho, RAPo

Offline SiliconWizard

  • Super Contributor
  • ***
  • Posts: 14481
  • Country: fr
Re: convert math-equations to images (C-only tool)
« Reply #10 on: January 22, 2024, 09:18:34 pm »
No mention of MathML?

https://developer.mozilla.org/en-US/docs/Web/MathML

Yes, 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/mathml2svg
haven'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.
 
The following users thanked this post: DiTBho

Online DiTBhoTopic starter

  • Super Contributor
  • ***
  • Posts: 3915
  • Country: gb
Re: convert math-equations to images (C-only tool)
« Reply #11 on: January 22, 2024, 09:36:49 pm »
There is this project in C: https://github.com/janmarthedal/mathml2svg
haven'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.

as less dependencies as possilbe, this project looks *VERY* interesting!
lib_pango and lib_cairo are not a problem  :D
The opposite of courage is not cowardice, it is conformity. Even a dead fish can go with the flow
 

Offline Nominal Animal

  • Super Contributor
  • ***
  • Posts: 6264
  • Country: fi
    • My home page and email address
Re: convert math-equations to images (C-only tool)
« Reply #12 on: January 24, 2024, 05:48:26 am »
"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.
 
The following users thanked this post: DiTBho

Online DiTBhoTopic starter

  • Super Contributor
  • ***
  • Posts: 3915
  • Country: gb
Re: convert math-equations to images (C-only tool)
« Reply #13 on: January 25, 2024, 02:05:43 pm »
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 need  :D

In 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.



The opposite of courage is not cowardice, it is conformity. Even a dead fish can go with the flow
 

Offline Nominal Animal

  • Super Contributor
  • ***
  • Posts: 6264
  • Country: fi
    • My home page and email address
Re: convert math-equations to images (C-only tool)
« Reply #14 on: January 25, 2024, 03:02:32 pm »
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.
MathJax (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 at LaTeX.js; but note that then the LaTeX source is provided to the viewers also.
 
The following users thanked this post: DiTBho

Online DiTBhoTopic starter

  • Super Contributor
  • ***
  • Posts: 3915
  • Country: gb
Re: convert math-equations to images (C-only tool)
« Reply #15 on: January 25, 2024, 04:47:00 pm »
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  :o :o :o
(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
The opposite of courage is not cowardice, it is conformity. Even a dead fish can go with the flow
 

Offline Nominal Animal

  • Super Contributor
  • ***
  • Posts: 6264
  • Country: fi
    • My home page and email address
Re: convert math-equations to images (C-only tool)
« Reply #16 on: January 25, 2024, 08:30:46 pm »
There is jsPDF, which is a client-side JavaScript library for generating PDF files; I don't know if its html module works well with latex.js.

However, I do believe SwiftLaTeX would let you do the LaTeX -> PDF on the client side, in the client browser.  Might be worth looking into.

In general, using emscripten (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.
 
The following users thanked this post: DiTBho


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf