[Tex/LaTex] What are possible ways of using Latex templates with VS Code

code-snippetseditorstemplates

Right now, I am using VS Code as my Latex editor. I use it coupled with the Latex-Workshop extension. My workflow entails writing the solution to problem sets and also doing some presentations with Beamer once in a while. My previous editor, Texpad, has this useful feature of Templates, where I could have, for example, problem_set_template.tex and so on defined and start working right away without having to write everything from scratch or copying and pasting something from somewhere else.

I understand that VS Code has a native Snippet feature, but as I understand it, what I seek is not exactly a Snippet. I want a way, either through the terminal of through the GUI to open the editor and ask for it to create a new file based on the template I have already defined.

In principle, one could always have the file problem_set_template.tex stored somewhere and duplicate it when needed, making the copy the de facto file to be edited. But that sounds incredibly inefficient. Any ideas? Thanks a lot!

Best Answer

I understand that you may not be interested in snippets, but I found this question when looking for ways to do templates in VS code and after some digging around I think that snippets is a very nice solution (with tabbing, predefined values for things like authors/titles). There is a slight drawback in that you first have to generate a file and save it as something.tex.

  1. Go to Code -> Preferences -> User Snippets (or simply type user snippets into the command palette)
  2. Select LaTeX

A file called latex.json will open, looking something like this

{
    // Place your snippets for latex here. Each snippet is defined under a snippet name and has a prefix, body and 
    // description. The prefix is what is used to trigger the snippet and the body will be expanded and inserted. Possible variables are:
    // $1, $2 for tab stops, $0 for the final cursor position, and ${1:label}, ${2:another} for placeholders. Placeholders with the 
    // same ids are connected.
    // Example:
    // "Print to console": {
    //  "prefix": "log",
    //  "body": [
    //      "console.log('$1');",
    //      "$2"
    //  ],
    //  "description": "Log output to console"
    // }
}

Replace all the commented out code with your template as per the instructions. Here is an example of a very basic latex template

{
    "Simple Template": {
        "prefix": ["template-basic"],
        "body": ["\\documentclass{article}",
            "\\title{${1:Default Title}}",
            "\\author{${2:Default Author}}",
            "\\begin{document}",     
            "\\maketitle",
            "$0",
            "\\end{document}"
        ],
        "description": "Generates a simple LaTeX template"
    }
}

This will generate a LaTeX template, put the cursor at the title field (which has the default value "Default Title"), tab will then move you to the author field and finally to $0, which is used to indicate the final cursor position.

The template is triggered by starting to type the keyword in the "prefix" section (note that you can have several keywords if you want) while in a LaTeX document, and then using tab or enter to execute it  

selecting a template snippet