Skip to content

Instantly share code, notes, and snippets.

@bjmorgan
Last active December 15, 2015 07:18
Show Gist options
  • Save bjmorgan/5221948 to your computer and use it in GitHub Desktop.
Save bjmorgan/5221948 to your computer and use it in GitHub Desktop.
Sublime Text 2 plugin for inserting \ref{<reference string>} code into LaTeX documents. The plugin scans the file for \label{<reference string>} commands (ignoring those in commented out lines), and presents a drop-down list of <reference string> options, alphabetically sorted. The keymappings given here bind the command to ⌘-l, ⌘-r.
[
// Run LaTeX plugin to insert reference code
{
"keys": ["super+l", "super+r"],
"command": "insert_latex_reference",
"context": [
{ "key": "selector", "operator": "equal", "operand": "text.tex.latex" }
]
}
]
import sublime, sublime_plugin
import re
class InsertLatexReferenceCommand( sublime_plugin.TextCommand ):
def run(self, edit):
labels = self.get_labels()
self.view.window().show_quick_panel( self.labels, self.complete )
def complete(self, choice):
if choice == -1:
return
label = self.labels[ choice ]
output_string = ( "\\ref{" + label + "}" )
edit = self.view.begin_edit()
startloc = self.view.sel()[-1].end()
self.view.insert( edit, startloc, output_string )
def get_labels(self):
pattern = '^[^%]*label\{+([^\}]+)\}'
fromPosition = 1
self.labels = []
self.view.find_all( pattern, 0, "$1", self.labels )
if self.labels:
return self.labels.sort
else:
return ''
[
{ "caption": "LaTeX: Select reference label", "command": "insert_latex_reference" }
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment