LaTeX Up!

Explain how to use LaTeX on Arch Linux: build tex documents, integrated vim workflow, real time viewers and more!

Installation (Arch)

The texlive group in Arch package registry contains many packages in the TexLive distribution of LaTeX. Note that MacOS and Windows have their own LaTeX distributions as well, you may refer to their documentation to install LaTeX on your system.

It’s a good idea to install as few packages as possible, keep them only to what you need, rather than install the entire group. TexLive is a huge system of many packages, fonts and so on. I recommend the following packages

1
2
3
4
5
6
7
pacman -S texlive-basic \
texlive-latex \
texlive-latexextra \
texlive-latexrecommended \
texlive-fontsrecommended \
texlive-fontsextra \
texlive-binextra

They give your basic LaTeX programs, pdflatex typesetting engine, latexmk compiler and many Latin fonts. With those, you should have no problem compiling a LaTeX document that has only ASCII texts.

After successfully compile a .tex file to PDF, you need a viewer. zathura is a popular choice on Linux systems. It imitates Vim in its keybinds, and support advance features like forward search and reverse search. Run

1
2
pacman -S zathura
pacman -S zathura-pdf-poppler

Chinese (Unicode) support in LaTeX

Unfortunately, you are still limited to Latin languages so far. If you tried to put a Chinese character like ‘云’ in your tex document, pdflatex won’t be able to handle it.

You have several options

1
2
3
pacman -S texlive-langcjk
pacman -S texlive-xetex
pacman -S texlive-langchinese

In the tex document, you need

1
2
\usepackage{xeCJK}
\setCJKmainfont{Noto Serif CJK SC}

The xeCJK package is only available in the xelatex engine. That’s to say, you can’t compile \usepackage{xeCJK} with pdflatex or other engines. You could manually tell latexmk to use xelatex by the -xelatex flag. Some LateX IDES also understand a kind of “magic comments”, much like the way “shebang” works. For example, put the following line at the top of the document

1
%! TeX program = xelatex

The neovim plugin vimtex supports this directive as well. The plugin basically turn neovim into a LaTeX IDE, giving it completion, compilation, realtime update and so on.

xelatex is different from pdflatex or other backends in the way that it relies heavily on the operating system for fonts. By setting the \setCJKmainfont command, you need font Noto Serif CJK SC installed on your computer, not in the LaTeX environment. xelatex supports Unicode, and can use fonts in ttf or otf files directly. That’s why we use it for Chinese characters.

On Arch, you can install the noto-fonts-cjk package from pacman. “cjk” stands for Chinese, Japanese and Korean. These three languages, noticeably all in east Asia, represent the major non-Latin languages.

(Neo)Vim Workflow

Install the vimtex plugin, lazygit plugin manager for example

1
2
3
4
5
6
7
8
9
10
{
"lervag/vimtex",
enabled = function()
if utils.executable("latex") then
return true
end
return false
end,
ft = { "tex" },
},

The plugin binds a few keybindings for common LaTeX procedures. <LocalLeader>ll will compile the currently opened tex buffer.

If the compilation is successful, the output file will be opened by a configured viewer in a new window, side by side with neovim. The output format can be configured to PDF or other formats.

Note the vimtex plugin uses latexmk as the default compiler underneath the hood. There are options to latexmk you can tweak to control the compiling process. The plugin actually exists in the vim era, so the config is done in viml scripts.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
let g:vimtex_compiler_latexmk = {
\ 'aux_dir' : 'aux',
\ 'out_dir' : '',
\ 'callback' : 1,
\ 'continuous' : 1,
\ 'executable' : 'latexmk',
\ 'hooks' : [],
\ 'options' : [
\ '-verbose',
\ '-file-line-error',
\ '-synctex=1',
\ '-interaction=nonstopmode',
\ ],
\}

I made a small change to the default compiler arguments. I set the aux_dir option, so a bunch of intermediate files generated by LaTeX will go in that folder, leaving me a clean project root directory.

The ll command by default call latexmk in “continuously compiling” mode. It monitors any changes in the source file and recompiles if necessary.

If your viewer supports dynamic update, while you edit the source tex file, the viewer will update the output (like PDF) in real time. This provides similar experience to online editors like Overleaf.

Type <LocalLeader>lv will open the viewer if it’s not already opened. If already opened, it will highlight the current cursor position in the PDF with green color, giving you a visual hint where you were working on.