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.
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.
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.
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.
Select Tools | Snippets...
from the menu.
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.
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>
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
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>
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).
Quick Links
Legal Stuff
Social Media