My Latex Setup
October 15, 2023
The past few days I spent at the 44th Midwest Probability Colloquium, and there I met another graduate student interested in good latex setups. From our conversation on the subject I figured this may be a good opportunity to put my current setup on my website.
Components
Filesystem and git
Below is the directory layout for a sample project, titled document1
.
document1/
.git/
latex-preamble/
.git
commands.tex
packages.tex
theorems.tex
.gitignore
.gitlab-ci.yml
.gitmodules
document1.tex
document1.tex.latexmain
.git
: I always initialize a git repository for any latex file I begin. I don’t always use git revision, especially if it is a smaller document, but it is set up and ready to go.
latex-preamble
: I keep my preamble in a separate directory, because it is a clone of my default preamble, which I have under git revision. It gets added automatically as a git submodule to any document I make.
.gitignore
: The content of this file tells git not to track any of the produced files (e.g. document1.aux
).
.gitlab-ci.yml
: The effect of the code in this file is that if I host the document on gitlab, GitLab CI/CD will compile the document and provide the most up-to-date pdf as a link in the README. This is to avoid tracking a changing binary file (which will unnecessarily bloat the repository).
.gitmodules
: The content of this file informs git that the latex-preamble
directory is a git submodule.
document1.tex
: My main latex document.
document1.tex.latexmain
: An empty file that tells latexmk
that the main tex file is document1.tex
(as opposed to, say latex-preamble/commands.tex
).
Rather than manually set up this directory system every time, I wrote a quick shell script:
Newtex
The following shell script named newtex
creates the above directory layout. It takes a single argument, which is the name of the latex document (without the .tex
appended – e.g. newtex document1
):
#!/bin/sh
mkdir $1
cd $1
touch $1.tex
#this file tells latexmk which file is the main one
touch $1.tex.latexmain
#makes git ignore all generated files
cp /home/nco/.code/templates/.gitignore ./
#edits .gitignore to have the correct latex-file-name
sed 's/latex-file-name/'"$1"'/g' /home/nco/.code/templates/.gitlab-ci.yml > ./.gitlab-ci.yml
#initializes git repository and adds my preamble as a submodule
git init
git submodule add git@gitlab.com:njchristoffersen/latex-preamble.git
#opens new tex file
vim $1.tex
One improvement would be to add an --offline
option to this command, which doesn’t rely on a connection to gitlab.
Archlinux and Vim
I use vim on a (arch)linux system, along with vimtex to allow for automatic compiling of documents. Below is taken from my .vimrc
configuration file
Plug 'lervag/vimtex'
let g:vimtex_compiler_method = 'latexmk'
let g:tex_flavor='latex'
let g:vimtex_view_method = 'zathura'
let g:vimtex_quickfix_mode=1
"set conceallevel=1
let g:tex_conceal='abdmg'
Snippets
For snippets I use UltiSnips. Here is a current copy of my latex snippets. It is a slight modification of the system from the late Gilles Castel. For a more complete description of the capabilities of this setup, see his post.
Workflow
Finally, my current workflow is then:
- Run command
newtex name-of-document
- Execute
template
snippet - Fill in or change the following:
- Document class (I’m currently a fan of the
amsart
class) - Title
- Date (template defaults to today’s date)
- Begin writing, in which I use the snippets as discribed by Gilles Castel.
- Document class (I’m currently a fan of the
Some Final Comments
I believe that we as mathematicians should push more for git revision to be standard in the typing of documents and papers. While there are plenty of version control systems in existence, I suggest git as the tool of choice due to its large featureset and already wide adoption by the larger technical community.
Hosted git services (such as GitLab or GitHub) make it easy to create a ‘merge request’. This allows readers to not just inform authors of a typo or error, but also provide the fix themselves. This reduces the friction required for authors to encorporate corrections into their documents. Alternatively, if a reader is unwilling or unable to correct it themselves, they can simply open an ‘issue’ through these hosted services, which then the author (or others in the community!) can fix.
Further, when there are multiple owners of the same git repository, git offers itself as a great tool for collaborators to work on the same document at the same time.
I for one am commited to making publicly available my tex sourcecode, so that when (not if) others find errors in my work, corrections can be encorporated easily.
As a side note, while some authors are not good about correcting issues, I’d like to recognize that many are. In particular, many book authors keep detailed errata (e.g. Terry Tao’s “An Introduction to Random Matrix Theory”).