[Tex/LaTex] Working with templates

best practicestemplates

I have just read the wonderful article LaTeX templates and realized that I have tried to practice this approach but there is one technical aspect which I don't know how to tackle. I am talking about how to actually use these pre-prepared templates. The main issue is how to easily load them, when one starts writing a new document. The simplest approach would be to have one local central repository of templates (and maybe their revisions as Stefan Kottwitz suggested in his post), and when ever template.tex is needed copy it to a new directory dedicated to the new document. However, this misses some of the modularity of LaTeX, isn't it?.

When working with a document personalized class file, which is stored somewhere where TeX can find, the user simply has to specify the class' name and it is at his disposal, regardless of where the class is stored for example. This is not the case with templates. So my question is essentially the following: What is the best way to use LaTeX templates? How should I store them? Where should I store them? How should I use them at the end of the day? Shouldn't a template be "exported" into a class file? If so, the what is the easiest way to achieve this?

Best Answer

Snippet managers

A flexible and powerful way of working with templates is to use a snippet manager. In Replace the `$$ ... $$` macro with the `\[ ... \]` macros? - Prefer the way LaTeX lays it out, but `$` are faster to write you find a simple explanation of how a snippet manager works. What you do is simply to store each template as a separate snippet and use the power of the snippet manager to modify it on expansion.

YASnippet

Here is an example with YASnippet, a snippet manager for Emacs. The following steps are how to create a simple article template:

  1. Enter the mode you want the snippets in. In this case I guess it is LaTeX-mode.
  2. Do M-x yas/new-snippet.
  3. Enter a name for the snippet.
  4. You will now get a chance to edit the snippet. Mark everything by doing C-x h, then kill it by C-w.
  5. You will now have an empty snippet. Paste the following into it and make sure the snippet ends after \end{document} and not on a new line:

    # -*- mode: snippet -*-
    # name: article
    # key: arttemp
    # --
    \documentclass{article}
    
    \usepackage[${1:english}]{babel}
    
    \title{${2:Title}}
    \author{${3:Author}}
    
    \begin{document}
    
    \maketitle
    
    $0
    
    \end{document}
    
  6. Do C-c C-c to save and load the snippet.

Now you can use the snippet in LaTeX-mode by writing arttemp and then pressing Tab. Note how $1, $2, $3 and $0 defines tab stops and that the three first has default values which can be overridden. Here is an animation that shows how the snippet works:

Animation of the snippet above

Obviously the snippet in the example is a very simple snippet. One snippet I use as a template for articles is 172 lines long. It contains \usepackages for the packages I commonly use, package configuration and macro definitions for macros I commonly use. For more information on how to write snippets see the documentation.

With YASnippet your snippets are stored as files. The manual describes how you can organize snippets. I keep my snippets in ~/.emacs.d/mysnippets and load them by the following in my .emacs:

(setq yas/root-directory "~/.emacs.d/mysnippets"); Develop and keep personal snippets under ~/emacs.d/mysnippets
(yas/load-directory yas/root-directory); Load the snippets

By syncing ~/.emacs.d/mysnippets between computers I get the same snippets on all of them.