1

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!

1

1 Answer 1

2

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

6
  • How do you trigger the template to be inserted? Feb 6, 2021 at 16:49
  • @UweZiegenhagen it is triggered using the prefix. In this case, when I am in a LaTeX document, when I start typing "template-basics" (without the "") it shows up in a list, and I can execute it with tab. I will add a picture in the answer. Feb 7, 2021 at 8:24
  • 1
    Got it! I removed all the code in the template, but the braces need to stay! Feb 7, 2021 at 8:38
  • 1
    Good point! I added the "out braces" back in my example code for clarity. Feb 7, 2021 at 8:41
  • Note that you have to select the LaTeX snippets file latex.json, and not the TeX one tex.json! Otherwise the snippets will not activate.
    – Alex Li
    Sep 21, 2021 at 21:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.