Beginning Tex / Latex / Lyx

Despite spending more years at universities than I probably should have I’ve never had the need to use Latex. It came up once when I was collaborating on a paper while studying computer science but my partner was doing most of the actual writing so I even managed to miss it then. In all the years I spent in chemistry it never came up once, it just wasn’t used in chemistry, at least not pre-2000.

Anyway, I have a couple of reasons to learn something in this area now. My wife submitted her PhD thesis many years ago and while it met all the publishing requirements for the university we were at neither of us thought it was a particularly appealing format (double spaced, single sided, etc). That format is great for making notes on (for a viva) but it lacks the polish of a properly formatted book. I thought I’d surprised her with a nicely formatted copy.

What is Tex and Latex?

Tex is a typesetting system which means it handles laying the text out on the printed page, it is not in any way, shape, or form a word processor. Tex is particularly good at laying out mathematical formulae and other things that require precise arrangements of characters (such as chemical equations). Tex is a fairly low level system so Latex was developed as a way to make it easier and quicker to use. At the heart of Latex is a collection of macros built of simpler Tex commands.

The fundamental idea behind Tex / Latex is that you separate content from how it’s presented. If you are familiar with HTML and CSS it’s the same idea at that. Essentially, you mark up your text with commands and let the Tex system handle the presentation.

Getting Started – Trying out Lyx

I read a fair bit before I started down the Tex / Latex road and many places said it’s best to start with Lyx. Lyx provides a graphical interface for editing a document and does a fairly good job of showing you what it’ll look like when it’s rendered. It essentially uses Latex but the documents it creates have a different extension and aren’t fully compatible.

Installing Lyx is pretty simple although naturally as I’m running Debian the packages are wildly out of date. Fortunately, there is a Flatpak that provides the latest version and comes installed with a ton of packages (which is good as I don’t know how to install additional packages yet).

Chemistry in Lyx

Chemical equations can be entered as just mathematical equations but without a lot of fiddling about you’ll find the elemental symbols are always in italics. There is a package called mhchem which makes the process of entering chemical formulae much easier. The package is pre-installed in the Flatpak distribution. It took me about an hour to figure out how format anything, there is some help in the Lyx Math tutorial but I didn’t understand half of what it said. So that you don’t also waste your time here’s the secret method in plain English.

Start a new mathematical formula and set it to display type, it’s quicker to use the short cut Ctrl+Shift+M. Now enter \ce and press space. The \ce will disappear. Although there’s no visual acknowledgement the formula is now in chemical equation mode. If you look at the Latex preview you’ll see \[\ce{}\] although it’ll probably be split over several lines. Anything you enter will now appear in the braces and be formatted as a chemical equation.

There’s more to it than this and I’m sure I’ll be updating this section in the future but I did come across this page which will probably help. This question was also really helpful in getting me started.

First Impressions of Lyx

Lyx is alright but I’m not sure it’s for me. As soon as I tried to enter a chemical formula I found that I needed to look at the Latex source to understand what was going on, if I’m going to be doing that I’m not sure how much use Lyx really is. Lyx gives you access to all the power of Latex but to wield that power efficiently it seems like I’d need to learn all Lyx shortcuts. If I’m going to learn all those shortcuts I might was well just learn the Latex commands. I can see how the more work processor style interface might be appealing to beginners. There’s nothing really wrong with Lyx I’m just not convinced it simplifies the process all that much.

Getting Started – Trying out Visual Studio Code with Latex Workshop

After playing with Lyx for a couple of days I decided to try out Visual Studio Code with the Latex Workshop plug-in which has over three million installs at the time of writing, I’m guessing that a lot of people are using this route to create documents. I don’t really use Visual Studio Code at the moment as I have a license for most JetBrains tools so it was all new to me. I found that the installation of the plug-in was easy enough and there are a fair number of online tutorials.

Instant Failure

As soon as I tried to compile a basic bit of Latex I hit a road block. The output from the compiler was:

[18:20:06.969][Build] Building root file: %WS1%/test.tex
[18:20:06.970][Build][Recipe] Build root file %WS1%/test.tex
[18:20:06.988][Build][Recipe] Preparing to run recipe: latexmk.
[18:20:06.989][Build][Recipe] Prepared 1 tools.
[18:20:06.999][Build][Recipe] outDir: %WS1% .
[18:20:07.001][Build] Recipe step 1 The command is latexmk:["-synctex=1","-interaction=nonstopmode","-file-line-error","-pdf","-outdir=%WS1%","%WS1%/test"].
[18:20:07.001][Build] env: {}
[18:20:07.002][Build] root: %WS1%/test.tex
[18:20:07.002][Build] cwd: %WS1%
[18:20:07.009][Build] LaTeX build process spawned with PID undefined.
[18:20:07.012][Build] LaTeX fatal error on PID undefined. Error: spawn latexmk ENOENT
[18:20:07.012]Error: spawn latexmk ENOENT
	at ChildProcess._handle.onexit (node:internal/child_process:286:19)
	at onErrorNT (node:internal/child_process:484:16)
	at process.processTicksAndRejections (node:internal/process/task_queues:82:21)
[18:20:07.012][Build] Does the executable exist? $PATH: /home/username/.cargo/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games:/home/username/.local/share/JetBrains/Toolbox/scripts, $Path: undefined, $SHELL: /bin/bash

The “PID undefined” line is a red herring, the process didn’t spawn because Debian doesn’t install the “latexmk” package by default (not to be confused with latex-mk). Once that was installed the document built fine.

Getting Started – Trying out TexStudio

TexStudio can be installed from Flathub which is probably the best bet if you’re using Debian stable (at the time of writing there is a bug in the Flatpak, the Debian repo version seems to work fine what I need). As soon as you start it up it might give you a warning that it can’t find a Latex system installed to fix this you’ll need to install TexLive. This can be done from the Debian repos or to get a newer version try directly downloading and installing it from here. You will definitely want to install the full version of the package to avoid any missing Latex package issues down the line.

sudo apt install texlive-full

This process will install about 7GB of new packages so grab a cup of tea. See here for some more information. In theory this should work as the TexStudio flatpak package can apparently use the locally installed version of Tex Live. In reality I immediately hit a bug, at the time of writing that bug is just 14 hours old..

Once I’d installed the Debian repo version of Texstudio I tested it on a simple document, it worked fine, then I tried it on Classic Thesis and… it also worked fine first time. Texstudio looks well polished and seems to be stable. The preview is quick enough for my needs, I think this is the way forward. I’ve not yet tried to put together a bibliography but that looked to be hard going whatever editor I was using.