轻量级的命令行 Latex 软件

轻量级的命令行 Latex 软件

我对 LaTeX 还很陌生,想要在 Open Office 中实现 Latex 来编写数学方程式,就像这样:http://www.hostmath.com/这样我就可以直接编写数学方程式(无需软件包)并将其转换为适用于 Windows 的 png 图像。(如果该软件支持 Linux/mac 就好了)

喜欢:

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

由于 OpenOffice 不支持 LaTeX,我很乐意为社区扩展它。

此外,还需要一个轻量级的命令行软件(最好小于 5mb,否则其他人很难下载我的扩展。)

答案1

可以使用standalone类来pdflatex调用convert程序的工具imagemagick,以便.png一次性编译并转换为方程式(参见这个答案以查看摘要)。

只需一个简短的脚本,将标准输入插入到具有正确前言的最小文档中,即可制作命令行小部件。这应该适用于大多数语言;我使用了 python (3),因为我熟悉语法。在名为的文件中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.')

然后,例如,

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

制作.png

输出png

您可能需要编辑前言代码以加载其他软件包并更改独立选项(有关这些详细信息,请参阅独立文档)。如果您需要帮助让脚本运行或使其更强大,请告诉我。

相关内容