Created
September 24, 2020 14:27
-
-
Save jondoesntgit/80e772aa9ac57de2228ec2f87da71b4a to your computer and use it in GitHub Desktop.
Subliem Tikz Compile Plugin
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
import sublime | |
import sublime_plugin | |
import os | |
import subprocess | |
import time | |
COMPILE_DIRECTORY = '/tmp/sublime-tikz' | |
TEMPLATE_FILE = os.path.join(os.path.dirname(__file__), 'template.tex') | |
try: | |
os.mkdir(COMPILE_DIRECTORY) | |
except FileExistsError: | |
pass | |
class ExampleCommand(sublime_plugin.TextCommand): | |
def run(self, edit): | |
self.view.insert(edit, 0, "Hello, World!") | |
class CompileTikz(sublime_plugin.WindowCommand): | |
def tikz2tex(self, tikz_text): | |
with open(TEMPLATE_FILE) as f: | |
template = f.read() | |
tex_file = os.path.join(COMPILE_DIRECTORY, 'document.tex') | |
with open(tex_file,'w') as f: | |
document = template.replace('$CONTENTS', tikz_text) | |
f.write(document) | |
return tex_file | |
def tex2pdf(self, tex_file): | |
proc = subprocess.Popen([ | |
'/Library/TeX/texbin/pdflatex', | |
'-file-line-error' , | |
'-interaction=nonstopmode', | |
'-jobname', | |
'document', | |
'-output-directory', | |
COMPILE_DIRECTORY, | |
tex_file | |
], stdout=subprocess.PIPE, stderr=subprocess.PIPE) | |
stdout, stderr = proc.communicate() | |
if proc.returncode != 0: | |
# There was an error# | |
for line in str(stdout).split(r'\n'): | |
print(line) | |
#print(stdout.split('\n')) | |
pdf_name = tex_file.split('.')[0] + '.pdf' | |
return os.path.join(COMPILE_DIRECTORY, pdf_name) | |
def pdf2png(self, pdf_file, png_file): | |
png_path = os.path.join(COMPILE_DIRECTORY, 'document.png') | |
subprocess.call([ | |
'/usr/bin/sips', | |
'-s', | |
'format', | |
'png', | |
pdf_file, | |
'--out', | |
png_file | |
]) | |
return | |
#print(png_path, png_file) | |
#os.rename(png_path, png_file) | |
#print(png_file) | |
def run(self): | |
"""Divide layout""" | |
parent_view = self.window.active_view() | |
file_name = parent_view.file_name() | |
file_stem = file_name.split('.')[0] | |
tikz_text = parent_view.substr(sublime.Region(0, parent_view.size())) | |
tex_file = self.tikz2tex(tikz_text) | |
pdf_file = self.tex2pdf(tex_file) | |
target_png_file = os.path.join(os.path.dirname(__file__), file_stem + '.png') | |
self.pdf2png(pdf_file, target_png_file) | |
print('made file') | |
time.sleep(.1) | |
print('waiting') | |
#self.window.set_layout({ | |
# "cols": [0.0, 1.0], | |
# "rows": [0.0, 0.6, 1.0], | |
# "cells": [[0, 0, 1, 1], [0, 1, 1, 1]] | |
# }) | |
"""Open image in group 1""" | |
self.window.focus_group(1) | |
self.window.open_file(target_png_file) | |
self.window.focus_group(0) | |
print('done') |
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
Show hidden characters
[ | |
{ "caption": "Compile Tikz", "command": "compile_tikz" }, | |
] |
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
\documentclass{article} | |
\usepackage{tikz} | |
\usepackage[graphics, active, tightpage]{preview} | |
\usepackage{wheelertikz} | |
\PreviewEnvironment{tikzpicture} | |
%!tikz preamble begin | |
\usepackage{circuitikz} | |
\usepackage{pgfplots} | |
\usepackage{amsmath} | |
\usetikzlibrary{circuits.logic.US} | |
\usetikzlibrary{positioning} | |
\usetikzlibrary{shapes} | |
\usetikzlibrary{calc, 3d} | |
\usepackage{helvet} | |
\usepackage{sansmath} | |
\usepackage{comment} | |
\usepackage{sfmath} | |
%!tikz preamble end | |
\begin{document} | |
\begin{tikzpicture} | |
$CONTENTS | |
\end{tikzpicture} | |
\end{document} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment