LaTeX workflow pt. 1

Sunday May 5, 2019

Day 15 using LaTeX.

Few years ago when I found out how organize vim environment to suite my operation by customizing the ~/.vimrc file, I was elated. Instead typing the command, I punched in keyboard shortcuts that I defined in the ~/.vimrc file. A command that previously took me 2 seconds to invoke, now took me less than 0.7 seconds. That’s a win for productivity.

Having lived in a vim session for so long and authored a number of markdown documents, getting myself accustomed to LaTeX did not take too long. In fact, I started tinkering around after a few hours using it.

Here are the things that I played around with.

# custom .sty file

After defining more than 5 packages in my preamble and overriding some defaults, I realized that my preamble took a fair bit lines of codes (LOCs) to a point that they appeared unnecessarily crowded. I searched around for solutions and I found one: sourcing a separate custom style .sty file.

Say that I have an .sty file, and I call it common.sty in the same directory as my main LaTeX document main.tex, I only need this one (1) line in my main.tex file:

\usepackage{common}

With this line, it sources all the configurations inside the common.sty in the same directory. I am hosting my common.sty on my GitHub Gist, which is available here through this link.

In its current shape, my common.sty has 4 main sections: (1) package declaration, (2) overriding defaults, (3) custom shortcuts, and (4) simplified scientific units. For this common.sty, my philosophy is that it should be modular and pluggable, meaning that it does not and should not have something too specific in it.

By the time you read this part, you might have noticed that the description of my common.sty in this blog does not match with the common.sty displayed above, which is pulled from a GitHub Gist. That’s because the common.sty on the GitHub Gist has a soul on its own and continue charting its own evolution

If I were to write a book chapter with fancy-pancy styles, I would come up with book.sty so that I have a proper demarcation from what is common and what is specific.

# shortcuts for commands

After a while, I began to think that typing \textcolor{red}{something in red here} was a little unwieldy (the package xcolor is required for \textcolor command to work).

Shouldn’t there be an easier way for this, along the line of Bash’s aliases? Well, there is. Enter, the LaTeX \newcommand function.

\newcommand{\tred}{\textcolor{red}}

With this command, instead of typing \textcolor{red}{something in red}, the command is now shortened to \tred{something in red}. With this shortcut, it opens a new world of possibility using (and rocking) LaTeX.

For example, I am a biologist and I frequently find myself writing experimental protocols. I have my own style guide for writing them. To give you a gist, I color-code my protocol, and I color-code them heavily. Catalog number in cyan, amount (time, mass, volume, dilution, etc.) in red, item or reagent in purple or fuchsia.

Sometimes, I go creative with symbols. Pipetting up and down multiple times? Well, let’s substitute “up and down” with half-arrow pointing up and down. In LaTeX, together with the package amssymb, I have to write this:

$\downharpoonleft$$\upharpoonright$

To get this picture:

pipette up & down

Well, that is kind of ugly. Why don’t we make it simpler?

\newcommand{\pUpDown}{$\downharpoonleft$$\upharpoonright$}

With a simple \pUpDown{} invocation anywhere in my document body, now I have the arrow I wanted. I further expanded this by adding more custom commands, for example I simplified the steps to write any units with the mu character (stands for micro).

% Simplified Units
\newcommand{\ul}{$\mu$l}    % define microliter
\newcommand{\ug}{$\mu$g}    % define microgram
\newcommand{\uM}{$\mu$M}    % define micromolar
\newcommand{\um}{$\mu$m}    % define micron

# automatic compile with entr

$ ls main.tex | entr pdflatex main.tex

This is a very basic implementation for automating the compilation with entr (also available on Homebrew for macOS users). What it does is that it looks for changes inside the main.text file, and when it sees changes, this command tells entr to run pdflatex main.tex.

However, using pdflatex poses a problem when writing documents with bibliography .bib file, as pdflatex on itself cannot perform operation to incorporate references from a .bib file. One of the easiest solutions is to use the latexmk, which should already be installed with the texlive distribution. latexmk is also provided with the minimal MikTex distribution.

To use latexmk to generate a PDF and to make it aware that there is a .bib file being sourced by the main.tex, invoke this command:

$ ls main.tex | entr latexmk -bibtex -pdf main.tex

Same operation as the previous entr command, but now it is aware of the bibliography file. Cool, but did you know that we don’t have entr on Windows?

Too bad because at work my primary workstation is a Win10 Ultrabook.

# LaTeX IDEs or Sublime Text 3

To remedy the problem that I had above (entr not on Windows), I had to experiment with LaTeX IDEs. I tried TexMaker and TexStudio. Long story short, both did not last very long.

I ended up using Sublime Text 3 together with LaTeXTools package. As a side note, it is recommended to install Sumatra PDF as well. Before going through the details, build can be triggered by pressing ctrl + b or F7 and Sublime Text 3 will trigger the LaTeX compilation on the currently open & active $file. The LaTeXTools comes with 10 different options to compile a .tex file and I have no idea the differences between those, yet.

compile LaTeX with LaTeXTools

Soon after the compilation is done and assuming that LaTeX did not nag, Sumatra PDF should open automatically and preview the current $file and it should highlight the most recent paragraph that you worked on. On Windows, this feels kind of right. I can enjoy writing LaTeX documents on the editor that I can tolerate with and it outputs beautiful documents. Life is perfect, right?

I have not yet tried replicating the same sweet feeling on Vim yet, partly because the experience of using Vim on Windows is less pleasant than on Linux. Maybe because I have not bumped into terminal emulator for Windows that makes me feel like using Linux. Yes, I tried Hyper terminal many months ago and it felt slow and sluggish.

If you bumped into a good terminal emulator for Windows that can play well with Cygwin, let me know. So far I have been using the default Cygwin terminal for basic stuff. I tried using ConEmu and cmder, both did not feel quite right to me. I think linux has spoiled me. But that’s a good thing. At least I can call myself a true geek. Maybe I should live inside Emacs.

# what do I get from LaTeX?

Say that I were to convert my friends from using Office Suites like MS Office and trying to coax them into using LaTeX, the very first question would be in the neighborhood of “what do you get from torturing yourself to use LaTeX?” This is totally a legit question.

My answer would be “nothing, other than personal satisfaction of being able to work closer to the root of something,” and this answer is related to my personal journey authoring documents in markdown, running headless Arch linux, and living inside the terminal.

When I first tried using Arch linux about 9 years ago (I think it was late 2010?), I was introduced to the world of being able to do things with bare minimum. The state of current software world, a lot of things are being abstracted. Users do not have a good grasp of what is going on under the hood, therefore software nowadays assumes the users having no intimate knowledge and how the inputs and outputs are processed.

Because of these abstractions and together with the assumption that users are stupid, software begin to have more bells and whistles to aid users to produce what they desire for. As the consequence, software becomes more bloated and becoming inefficient.

Also, the fact that I can write my config in .sty file and forget about them is something that I like. Setting the styles up would take a fair amount of time, but that process would only happen once.