Skip to content

Instantly share code, notes, and snippets.

@bjmorgan
Last active December 10, 2015 00:48
Show Gist options
  • Save bjmorgan/4353640 to your computer and use it in GitHub Desktop.
Save bjmorgan/4353640 to your computer and use it in GitHub Desktop.
Sublime Text plugin for LaTeX that lists a choice of `.eps` files in the directory defined in `\graphicspath{<DIRECTORY>}`, and after the user selecting one inserts boilerplate figure code. If `\graphicspath` is missing, or the line is commented out, the plugin will look for .eps files in the root directory of the `.tex` file.
[
// Run LaTeX plugin to insert figure code
{
"keys": ["super+l", "super+f"],
"command": "insert_latex_figure",
"context": [
{ "key": "selector", "operator": "equal", "operand": "text.tex.latex" }
]
}
]
[
{ "caption": "LaTeX: Select .eps Figure", "command": "insert_latex_figure" }
]
import sublime, sublime_plugin
from string import split, join
import os, re
class InsertLatexFigureCommand(sublime_plugin.TextCommand):
def run(self, edit):
figuresDirectory = self.get_graphics_path()
extension = re.compile('\.eps$')
path = join( self.view.file_name().split('/')[0:-1], '/' )
filenames = os.listdir( path + '/' + figuresDirectory)
filenames = [ filename.encode('utf-8') for filename in filenames ]
self.figures = []
for filename in filenames:
if extension.search( filename ):
self.figures.append( filename )
self.view.window().show_quick_panel( self.figures, self.complete )
def complete(self, choice):
if choice == -1:
return
filename = self.figures[ choice ]
label = "fig:" + re.sub('\.eps$', '', filename)
output_string = "test"
output_string = ("\\begin{figure}[tb]\n"
"\t\\begin{center}\n"
"\t\t\\resizebox{8cm}{!}{\\includegraphics*{" + filename +"}} %\n"
"\t\t\\caption{\\label{" + label + "}CAPTION}\n"
"\t\\end{center}\n"
"\\end{figure}\n\n")
edit = self.view.begin_edit()
startloc = self.view.sel()[-1].end()
self.view.insert(edit, startloc, output_string)
def get_graphics_path(self):
fromPosition = 1
self.graphics_path = []
self.view.find_all("^[^\%].*graphicspath\{+([^}]+)", 0, "$1", self.graphics_path)
if self.graphics_path:
return self.graphics_path[-1].encode('utf-8')
else:
return ''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment