Last active
March 8, 2022 20:22
-
-
Save glegoux/b7bd69c4704506abdb124f02eadb416b to your computer and use it in GitHub Desktop.
[Python] Get math formula under the shape of PNG image to add LateX equation to your stackoverflow post
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
"""Insert LaTex equation (for https://stackoverflow.com/) | |
Add LaTex equation under the shape of PNG image to your stackoverflow post. | |
Print text to add to your post, and download the image equation if necessary. | |
(use https://chart.googleapis.com). | |
See help with -h and --help option for more details. | |
if MathJax, KaTeX or other javaScript display engine for mathematics | |
is used on the website, this script is outdated. | |
References: | |
- https://www.mathjax.org/ | |
- https://khan.github.io/KaTeX/ | |
- https://chart.googleapis.com (LateX section) | |
""" | |
# Authors: Gilles LEGOUX <gilles.legoux@gmail.com> | |
# | |
# MIT License | |
# Copyright (c) 2017 Gilles LEGOUX | |
if __name__ == '__main__': | |
import sys | |
if sys.version_info < (3, 2): | |
sys.stderr.write("Sorry, requires Python 3.2+.\n") | |
sys.stderr.write("See -h or --help options.\n") | |
sys.exit(1) | |
import os | |
from argparse import ArgumentParser, RawDescriptionHelpFormatter | |
from subprocess import call | |
from urllib.parse import quote | |
import uuid | |
URL = 'https://chart.googleapis.com/chart?cht=tx&chl=' | |
parser = ArgumentParser(formatter_class=RawDescriptionHelpFormatter, | |
description=__doc__ | |
) | |
parser.add_argument("-e", "--editor", | |
dest="editor", | |
default="nano", | |
help="choose editor to write equation " | |
"(default nano)." | |
) | |
parser.add_argument('-i', '--input', | |
dest="file", | |
default="equation.txt", | |
help="your math formula file " | |
"(default equation.txt)." | |
) | |
parser.add_argument("-p", "--prefix", | |
dest="prefix", | |
default="stackoverflow_", | |
help="choose prefix for image name if download " | |
"(default stackoverflow_)." | |
) | |
parser.add_argument("-d", "--dest", | |
dest="dest", | |
default="./", | |
help="choose where image will be downloaded " | |
"(default ./)." | |
) | |
parser.add_argument("-n", "--no-download", | |
dest="download", | |
action="store_false", | |
help="choose if image will be download, " | |
"-p and -d options become useless if used " | |
"(by default download image)." | |
) | |
args = parser.parse_args() | |
_file = args.file | |
editor = args.editor | |
download = args.download | |
dest = args.dest | |
prefix = args.prefix | |
call([editor, _file]) | |
with open(_file, 'r') as descriptor: | |
# without end line file | |
equation = descriptor.read()[:-1] | |
encoded_equation = quote(equation) | |
_uuid = uuid.uuid1() | |
print('![{}]({}{})'.format(equation, URL, encoded_equation)) | |
if download: | |
call(['wget', | |
'{}{}'.format(URL, encoded_equation), | |
'-O', '{}{}.png'.format(os.path.join(dest, prefix), _uuid), | |
'-q']) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment