3

I'm very new to LaTeX and want to implement Latex in Open Office for just writing mathematical equations just like: http://www.hostmath.com/ so that I can write mathematical equations directly (without packages) and convert it into a png image, for windows. (it'd be good if that software supports Linux/mac)

like:

xyz.exe -convert "\oint \frac-b \pm \sqrt{b^2 - 4ac}}{2a}, \space \forall a,b,c \in \mathbb{N}, \text{xyz text} dr" C:/temp/latex/001.png

Since OpenOffice doesn't support LaTeX, I'd be happy to extend it for the community.

Also, a light command line software needed (<5mb, preferably, otherwise it'd be difficult for others to download my extension.)

  • 1
    Have a look at CodeCogs. You can export math equations to PNG directly. Also, you may have a look at this question. – user156344 Feb 21 at 15:10
  • 1
    Maybe you are more looking for a TeX math simulation like MathJax than a real LaTeX implementation. LaTeX itself is much more than only some math commands… – TeXnician Feb 21 at 15:22
  • See also askubuntu.com/questions/319593/…, which lists several options including the built-in equation editor with LaTeX-like syntax, as well as LaTeX-plugins for Writer (although that does require that LaTeX is installed). – Marijn Feb 21 at 16:00
  • The code is not identical, but OO has a pretty good math language built in. Why does it have to be LaTeX? – Bananguin Feb 21 at 19:20
  • As per @JouleV this can be as simple as a wget to a web server such as latex.codecogs.com/… and your example is without correction latex.codecogs.com/… – user170109 Feb 21 at 20:14
3

It's possible to use the standalone class to have pdflatex call the convert tool of the program imagemagick so as to compile and convert to .png an equation at one fell swoop (see this answer for a summary).

A short script to insert the standard input into a minimum document with the correct preamble is all that is required to make your command line widget. This should be possible with most languages; I used python (3) as I'm familiar with the syntax. In a file called textopng.py:

#!/usr/bin/env python
import sys, os, subprocess
# Tested with Python 3.7.2 (4.20.10-arch1-1-ARCH)
# Requires imagemagick
# Name of .tex created (.png will have same name up to the extension)
tex_file = 'outf.tex'
# Preamble code - add additional \usepackage{} calls to this string
# Note: The size option of convert may be useful e.g. size=640
preamble = r'\documentclass[convert={density=900,outext=.png},preview,border=1pt]{standalone}\usepackage{amsmath}'
# Place input in inline math environment
equation = r'\(' + sys.argv[1] + r'\)'
# Construct our mini latex document
latex_string =  preamble + '\n' + r'\begin{document}' + equation + r'\end{document}'
# Write to tex_file
with open(tex_file, 'w') as f:
    f.write(latex_string)
try:
    # Compile .tex with pdflatex. -shell-escape parameter required for convert option of standalone class to work
    proc = subprocess.run(["pdflatex", "-shell-escape", tex_file], capture_output=True, text=True,stdin=subprocess.DEVNULL, timeout=3)
    if proc.stderr != '':
        # Print any error
        print('Process error:\n{}'.format(proc.stderr))
    if proc.stdout != '':
        # Print standard output from pdflatex
        print('Process output:\n{}'.format(proc.stdout))
# Timeout for process currently set to 3 seconds
except subprocess.TimeoutExpired:
    print('pdflatex process timed out.')

Then, for example,

python3 textopng.py  "x=\frac{y^2}{32}"

produced the .png

outputpng

You may want to edit the preamble code to load additional packages and change the standalone options (see the standalone documentation for the details on these). Let me know if you need help getting the script to work or making it more robust.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.