HomeContact
Archive
Creating Snippets in Sublime Text 2 for LaTeX
Jon Page
January 12, 2013
2 min

Table Of Contents

01
Requirements
02
What is a Snippet?
03
Intro to Snippets
04
Browsing Existing Snippets
05
Creating Your Own Snippets
06
Handy Article Snippet
07
Wrapping Things Up
08
See Also

Ever realize you seem to type the same block of text every time you create a new document? If you haven’t already, now is the time to introduce yourself to snippets. In this tutorial we will walk through the creation of snippets in Sublime Text 2 for LaTeX.

Requirements

You will need Sublime Text 2 (ST2) installed on your computer. For more details on this fantastic editor and how to get it working with LaTeX see this tutorial.

What is a Snippet?

Snippets are templates which you can quickly insert into any document you are working on within Sublime Text 2 (in fact, many editors support snippets: e.g., TextMate, Eclipse, Notepad++, etc.). If you’ve seen snippets before you can safely skip the intro below.

Intro to Snippets

To begin with, start a new document in Sublime Text 2 (PC: CTRL+N, OSX: CMD+N). Type ”lorem”, then press the TAB key. This should result in a large block of standard filler text. If you’re new to snippets, consider your mind blown. You’re welcome. Under the hood, ST2 is listening out for snippet triggers (in our case, “lorem”). When you press the TAB key after typing a snippet trigger, ST2 will replace the trigger with the corresponding snippet.

Browsing Existing Snippets

Through the GUI

Select Tools | Snippets... from the menu.

Using the Command Palette

Press CTRL+SHIFT+P (on OSX: CMD+SHIFT+P). This pulls up the Command Palette. Now type ”snippet”. To the right of each snippet you will see the corresponding trigger sequence (e.g., lorem,tab). In fact you can trigger the desired snippet by clicking its entry in this list.

Creating Your Own Snippets

Select Tools | New Snippet.... This will open a new snippet document with the following structure:

<snippet>
  <content>
    <![CDATA[
      Hello, ${1:this} is a ${2:snippet}.
    ]]>
  </content>
  <!-- Optional: Set a tabTrigger to define how to trigger the snippet -->
  <!-- <tabTrigger>hello</tabTrigger> -->
  <!-- Optional: Set a scope to limit where the snippet will trigger -->
  <!-- <scope>source.python</scope> -->
</snippet>

Anatomy of a snippet

Let’s break down the structure of this snippet before moving on to create one for LaTeX.

 <content><![CDATA[ Hello, ${1:this} is a ${2:snippet}. ]]></content>

The content section contains everything that will replace the trigger, everything is enclosed in a CDATA tag to avoid any internal xml-like structure from ruining your snippet. $ indicates a field. ${1:this} tells ST2 to select this immediately after pasting the snippet. This allows you to change this without having to manually select it first. Inside of any ${}, the number indicates the tab order. That is, if you press TAB after your cursor has been automatically placed on this, your cursor will automatically select snippet. The text following the colon indicates the default text for the respective field.

 <!-- Optional: Set a tabTrigger to define how to trigger the snippet --> <tabTrigger>hello</tabTrigger>

Removing the comment around the tabTrigger tag, we can see if we were to type ”hello” and press TAB, the content from above would replace the text ”hello“.

 <!-- Optional: Set a scope to limit where the snippet will trigger --> <scope>source.python</scope>

Removing the comment around the scope tag, means our snippet will only trigger when we are working on python source code, but that’s another story for another time. And now for something completely different

Handy Article Snippet

Let’s put our newfound familiarity with snippets to use by creating a snippet for the article document class. Begin by replacing the content with the following:

<content>
  <![CDATA[
    \documentclass[12pt]{article}
    \usepackage{natbib,amsmath,amsfonts,fullpage,hyphenat,booktabs,graphicx,setspace}
    \usepackage\[colorlinks,linkcolor=black,citecolor=blue,urlcolor=black\]{hyperref}
    \onehalfspacing
    \bibpunct{(}{)}{;}{a}{}{,}
    \title{${1:Title}}
    \author{Your Name}

    \begin{document}
      \maketitle{}

      \begin{abstract}
      \end{abstract}

      \section{Introduction}
      ${2:Start typing here}

      \bibliographystyle{apalike}
      \bibliography{all}{}
    \end{document}
  ]]>
</content>

This snippet and my other favorites will be explained in depth in a future post. We need to change the trigger to something appropriate, I chose ”article“:

<tabTrigger>article</tabTrigger>

Lastly, we need to change the scope (i.e. the context within which our trigger will operate). Technically, we could leave this blank to allow our article snippet to be triggered in every context. However, since ST2 is such a versatile editor and you may want to use it for other purposes beyond LaTeX (e.g., Python), we want to get into the habit of defining the context so our snippets don’t conflict. Replace the context exactly as follows:

<scope>text.tex.latex</scope>

Wrapping Things Up

Now that we have created our first custom snippet, go ahead and save it to the default location (the “User” package folder). Make sure to save the snippet with the “sublime-snippet” extension (e.g., “article.sublime-snippet”). Next time we will cover several other snippets useful for writing articles and making presentations with LaTeX (link here).

See Also

Official Documentation on Snippets in Sublime Text


Tags

#sublime-text-2#latex#snippets

Related Posts

Sublime Text 2: Tips, Tricks, and More
February 24, 2013
1 min
© 2022, All Rights Reserved.

Quick Links

About UsContact Us

Social Media