Skip to content

Instantly share code, notes, and snippets.

@santiago-salas-v
Last active February 1, 2020 14:55
Show Gist options
  • Save santiago-salas-v/71c13967f75b5b6a25a3075fa78bb266 to your computer and use it in GitHub Desktop.
Save santiago-salas-v/71c13967f75b5b6a25a3075fa78bb266 to your computer and use it in GitHub Desktop.
sublimetext plugin to convert latex to html and pdf to png
import sublime
import sublime_plugin
import os
import subprocess
import threading
class HtlatexCommand(sublime_plugin.TextCommand):
def run(self, edit, filename=None):
filename = self.view.file_name()
th = HtlatexThread(filename)
if not filename is None and \
os.path.splitext(filename)[1] == '.tex':
th.start()
class HtlatexThread(threading.Thread):
def __init__(self, filename=None):
self.filename = filename
threading.Thread.__init__(self)
def run(self):
first_dir = os.path.abspath(os.getcwd())
new_file_with_dir = os.path.abspath(self.filename)
new_dir = os.path.dirname(new_file_with_dir)
filename = os.path.basename(new_file_with_dir)
os.chdir(new_dir)
makefile = [x for x in os.listdir(new_dir) if
os.path.splitext(x)[-1] == '.mk4']
if filename is not None and \
os.path.splitext(filename)[-1] == '.tex':
if len(makefile) > 0:
subprocess.Popen(['make4ht', filename, '-e', makefile[0]])
else:
subprocess.Popen(['make4ht', '-ul', filename])
else:
pass
os.chdir(first_dir)
import sublime
import sublime_plugin
import os
import subprocess
import threading
class Latex2pngCommand(sublime_plugin.TextCommand):
def run(self, edit, filename=None):
filename = self.view.file_name()
th = Latex2pngThread(filename)
if not filename is None and \
os.path.splitext(filename)[1] == '.tex':
th.start()
class Latex2pngThread(threading.Thread):
def __init__(self, filename=None):
self.filename = filename
threading.Thread.__init__(self)
def run(self):
first_dir = os.path.abspath(os.getcwd())
file_with_dir = os.path.abspath(self.filename)
new_dir = os.path.dirname(file_with_dir)
os.chdir(new_dir)
filename_with_ext = os.path.basename(file_with_dir)
filename, fileext = os.path.splitext(filename_with_ext)
pdf_file_names = [os.path.splitext(x)[0]
for x in os.listdir(new_dir) if
os.path.splitext(x)[-1] == '.pdf']
if filename is not None \
and fileext == '.tex' and filename in pdf_file_names:
infile = os.path.join(os.path.abspath(
os.curdir), filename + '.pdf')
outfile = os.path.join(os.path.abspath(
os.curdir), filename + '.png')
magick_command = [
'magick', '-density', '150',
'-units', 'PixelsPerInch',
infile,
'-quality', '90',
outfile]
print('running magick')
p = subprocess.Popen(magick_command,
stderr=subprocess.STDOUT,
stdout=subprocess.PIPE,
shell=True)
output, errors = p.communicate()
print('args')
print(p.args)
print('errors')
print(errors)
print('output')
print(output)
print('output file: ' + outfile)
print('removing input file: ' + infile)
os.remove(infile)
else:
pass
os.chdir(first_dir)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment