12.03.05

LaTeX in TextMate, part 2

Posted in TextMate, LaTeX at 10:11 pm by Haris

After some discussion on the #textmate IRC channel, we decided to break the “Insert Environment/Command based on word” command in two commands. So I decided to take this opportunity to talk about TextMate’s LaTeX bundle in general, and how I go about using it.

First of all, we have snippets for sections, subsections and subsubsections, triggered by the tab triggers sec, sub and ssub respectively. They are set to also create labels, so typing sec and pressing Tab produces:

\section{section name}\label{sec:label}

and the words “section name” are selected, so upon typing you are inserting the section’s title. Once done, pressing Tab highlights the “label” parts, so that you can type a label. A second Tab takes you outside so that you can continue with the rest of the text. This can be customized by opening the bundle editor, if you want to remove the label part or something else, or maybe just change the trigger. The symbols list has now been updated so that subsections are indented a bit, so that the commands:

\section{Sec1}
\subsection{Sub1}
\subsection{Sub2}
\subsubsection{Ssub1}
\section{Sec2}

would show up in the symbol list as:

Sec1
  Sub1
  Sub2
    Ssub1
Sec2

Next, the LaTeX bundle now has two commands for creating environments and commands. The shortcuts for them currently are Cmd-{ (that’s Command-Shift and the button containing [ and { ) and Cmd-} respectively. The first command generates a LaTeX environment from the current word, i.e. typing myEnv and pressing Cmd-{ produces:

\begin{myEnv}

\end{myEnv}

and places the cursor in-between the two lines, with a tab indent. TextMate is actually smart about it, and preserves this indent throughout the environment. The command is very smart too: Using the command with thm produces:

\begin{theorem}
    \label{thm:}

\end{theorem}

and places the cursor inside the label command, ready to move it right below after pressing Tab. If instead you use it with enum, it produces:

\begin{enumerate}
    \item 
\end{enumerate}

and places the cursor right after the item. You can also customize the command in many ways. You could directly edit it in the bundle editor, or you could use the environment variables it uses. There are two variables that are relevant here:

  1. TM_LATEX_SHORTCUTS can be used to add your own custom shortcuts. For instance, if it contains cor=>corollary,item=>itemize, then you can use your command with the words cor and item to generate corollary and itemize environments respectively. Of course these two are already in the command by default, so you don’t have to implement them.

  2. TM_LATEX_FIRSTLINE can be used to customize what appears in the first line. For example, the theorem and enumerate environments above could have been produced by setting the variable equal to

    enumerate=>\\item ,theorem=>\\label{thm:$1}\n\t

The second command is used to create commands in a slightly more comfortable way. It too uses the TM_LATEX_SHORTCUTS variable to expand things. For instance typing it and then pressing Cmd-} produces

\textit{}

and places you between the braces, ready to take you out of it at the press of Tab. You can further customize the command via the variable TM_LATEX_COMMAND_NOBRACES, which allows you to define which commands you want to have expand without the braces at the end. For instance, the command is currently customized to expand ds to \displaystyle.

Another neat command is useful when inside the itemize environment. It is triggered by the Enter key, and what it does is, if the current line starts with \item, moves you to the next line and adds \item. Maybe it doesn’t sound much, but it helps you focus on making the list of items, instead of worrying about adding the words that will make it a list. Details, details, but it’s the little things that count.

There is an extremely useful command that is not special in the LaTeX bundle, but I find it so useful that I have to mention it. If some text is selected, pressing ctrl-cmd and the arrow keys moves the selection around, without affecting the clipboard. It’s very neat and I am very surprised that not all text editors have it.

Finally, there is of course a command integrating the pdflatex command line tool. It’s triggered by cmd-b, and it runs pdflatex on the document, and produces the compilation results in a new window, with all error messages hyperlinking to the corresponding locations in the document. If there are no errors, the window changes to show you the pdf file produced, unless you have set the TM_LATEX_VIEWER variable to a different viewer, e.g. TeXniscope, in which case you get switched to that viewer.

Ok, enough for now, in our next installment we’ll take a look at some of the other commands in the LaTeX bundle. We’ll see how to use TextMate together with BibDesk, how to get a list of the labels in the document for completion, and more! You can find more detailed explanation in the README.html that lives in the LaTeX bundle.

Got any commands and other tricks you like in TextMate? Leave a comment!

Later

2 Comments »

  1. Jenny Harrison said,

    August 20, 2006 at 6:05 am

    Haris,

    I now do not recommend using \begin{section} and \end{section}. TextMate requires alignment of these, but the tabs are not terribly stable. I spent too much time straightening them up, only to have them become misaligned again. I hope you can remove my former messages about this so others don’t waste their time.

    Here is a question for you. Blue Sky’s Textures had a nice feature of “format dumps” where you could preload the preamble to save time. Can TextMate do this?

    Jenny

  2. Haris said,

    August 21, 2006 at 11:25 am

    Jenny,

    can you explain more specifically what problems you are having? I am not sure I understand what goes wrong with the \begin{section}…\end{section} method. Perhaps it is a problem that has a different root and we can fix it.

    As for “format dumps”, I am guessing this is what Templates in Textmate do. Coinsidentally enough, yesterday I had the idea of creating a command, that would scan a specific folder in your computer for “template” files with extension .tpl or some such, and offer you a pop-up to select one. Upon selection, it would insert the text of that template file at the current location. So one would first create a new file and then run this command, and pick among their selection of templates. The command itself is not hard to do, I just need to coordinate with other people about key equivalent and specifics of the behavior. It will probably make its way to the bundle soon.

    Haris

Leave a Comment