eccentric.dk

reality meets eccentricity

Category: Tech

  • Using TeXLive with Dev Containers in VSCode

    In this post I will describe how to set up VSCode to use a Docker container to run TeXLive and how to set up a folder template to quickly setup documents in the future.

    Prerequisites

    In this post I will assume that you are running Linux and that your template folder is called latex_template. What you need in order to get started is:

    • A working rootless Docker installation
    • VSCode installed along with the extensions Dev Containers, LaTeX Workshop, and Folder Templates installed. Container Tools is also recommended, but not required.
    • Enough disk space for the docker image which is about 5.2GB and enough bandwidth to download it in a timely manner, it is a download of about 2.3GB.

    Setting up a Dockerfile

    We shall use Island of TeX‘s Docker image as a base. But in order to use Git and commit signing (as you should) we need some slight customization. This will do the trick:

    FROM registry.gitlab.com/islandoftex/images/texlive:latest

    RUN apt-get update
    RUN apt-get install -y git ssh gnupg2
    RUN apt-get clean

    This will install Git, SSH and GnuPG into the base image in a persistent manner. Copy the above into the file latex_template/.devcontainer/Dockerfile.

    Setting up Dev Containers: Method 1

    Copy

    {
    "build": { "dockerfile": "Dockerfile" },
    "customizations": {
    "vscode": {
    "extensions": [
    "James-Yu.latex-workshop"
    ]
    }
    }
    }

    into the file latex_template/.devcontainer/devcontainer.json.

    Setting up Dev Containers: Method 2

    You can also build the Dockerfile from the commandline (from a folder with the Dockerfile from above) with the command

    $ docker build -t mytexlive . 

    and instead set latex_template/.devcontainer/devcontainer.json to be

    {
    "build": { "dockerfile": "Dockerfile" },
    "customizations": {
    "vscode": {
    "extensions": [
    "James-Yu.latex-workshop"
    ]
    }
    }
    }

    Setting up Folder Templates

    Make a file latex_template/.ftsettings.json with the contents:

    {
    "name": "Template Title",
    "omitParentDirectory": true
    }

    Where you give whatever name you want to the template.

    Make a file latex_template/latexmkrc with the contents:

    $pdf_mode = 4;

    $out_dir = "build";

    This will cause latexmk to use LuaLaTeX (as you should…) and place the output in latex_template/build. You can also add a line to latex_template/latexmkrc:

    @default_files = ('[FTName].tex');

    to specify the initial TeX file as the main document to run LuaLaTeX on, so you can include other TeX-files. For further information on how to customize latexmkrc refer to its documentation.

    The folder latex_template/build should be ignored by Git, so it is recommended to set up latex_template/.gitignore to do this:

    build/

    Next, create a file latex_template/[FTName].tex (and yes, the file name should literally be [FTName].tex). This file should contain the common code for the template you are creating, for example:

    % Preamble  
    \documentclass[12pt, a4paper]{article}

    % The title is likely not something
    % you can predetermine
    \title{}

    % Your name and other authors
    \author{Your Name}

    % Your packages etc.

    \begin{document}

    % Your document

    \maketitle
    \tableofcontents

    % ...

    \end{document}

    Move the folder latex_template to a place there you will keep other templates, e.g. ~/Documents/Templates.

    Setting up VSCode

    From the Command Palate (on a PC, the Command Palate can be accessed by pressing Ctrl + Shift + P), select the command “Set Custom Global Folder Templates Folder” and browse to and select the folder containing your templates, e.g. ~/Documents/Templates.

    In the VSCode Settings, go to the option and set Latex-workshop > Latex > Recipe: Default to latexmk (latexmkrc).

    Make your project

    Now, open an empty folder in VSCode, and from the Command Palate, select “Create New Templated Folder” (this can also be done via the context menu in the file browser), select your template and type the name of your project, e.g. myproject. You should now see some new files, like myproject.tex which contains the LaTeX document prepared earlier, and copies of the other files and sub-folders in latex_template/.

    From the Command Palate, select “Reopen in Dev Container”. After the base image has been downloaded and built, you can open myproject.tex and (on a PC) press Ctrl+S and a sub-folder build/ should appear containing the resulting PDF and other temporary files.

    Files

    You can download a ZIP archive with the folder template here.

  • Use cases for Git at university

    Even if you are not developing developing software, the Free and Open Source Software (FOSS) version control system (VCS) Git, can be of much use for students. Features like branches and tags can make your life easier and save you time and grief.

    For coursework

    For each course you take, create a folder and initialize a Git repository. Afterwards, you can add all the files you use and keep histories and remote backups of all your files and homework. This works especially well for those of us who use the \LaTeX typesetting system or other markup based document systems. No more need to tell your professor that the dog ate your homework because your disk or computer crashed, or a coffee spilled on your keyboard.

    Theses

    Each year, I see posts by fellow students writing on message boards, asking how to recover files from thumb drives, desperate to recover their bachelor or master theses. Weeks or even days before the due date. No backup. Don’t be one of them! Use Git! Use one of the numerous free Git hosting services to back up your work af often as you want, for example after each writing session and write a short description of what you did in the commit message. Is your advisor asking for a major revision you may (or may not) agree with? Create a branch, and keep your original work while you work on your revision, and and work on both at the same time. Use tags to mark each draft version, and be able to restore an exact copy of each one.

    But, but… a command line tool?

    Well, yes. But firstly command line tools are not as scary as you might think. You might not agree, and there are plenty of free GUI tools to manage Git repositories, both open and closed source. I sometimes use SmartGit (https://www.syntevo.com/smartgit/) which is free for students. All major platforms are supported, Linux, MacOS and Windows, easily installed. For Windows and MacOS go to https://git-scm.com/. Linux users can either download the latest version from the mentioned website, or from their package manager of choice.