Skip to content

LaTeX

Published:

Installation

On Arch

Install texlive-installer

yay -S texlive-installer

Install a minimal working LaTeX installation, around 200MB.

install-tl

Make sue tlmgr is on the $PATH and use tlmgr to install texliveonfly. It requires python.

sudo tlmgr install texliveonfly

Then, use texliveonfly to compile the target file. It will install all the needed packages.

sudo texliveonfly <input file>

External dependencies

You may still need to install some external dependencies manually.

# Used to add the italian language support for babel
sudo tlmgr install babel-italian
# Used by minted to hightlight code
pip3 install Pygments

Useful scripts

% PlantUML diagrams
% Usage: \plantuml[textwidth percentage]{path/to/diagram}{caption}{label}
\newcommand{\plantuml}[4][1]{
    \begin{figure}[h]
        \begin{adjustbox}{width=#1\textwidth,center}
            \input{#2}
        \end{adjustbox}
        \caption{#3}\label{dg:#4}
    \end{figure}
}

Tricky packages

Minted: shell escape and how to avoid it

minted is a package that allows to highlight code in LaTeX documents. Under the hood, it uses the Python’s Pygments library to do so. As a consequence, it requires the shell-escape option to be enabled in the LaTeX compiler to invoke the Python interpreter. Understandably, there are many situation where this option is disabled (e.g. ArXiv).
To avoid this, you can use the minted package instructing it to cache the highlighted code.
First, you need to compile the document with the --shell-escape option enabled. You can do so on your own machine or using a service like Overleaf. Make sure to include the minted package with the finalizecache option enabled:

\usepackage[finalizecache,cachedir=.]{minted}

If you are compiling locally using VsCode with the LaTeX Workshop extension, you need to enable the --shell-escape option in the settings.json file:

// In .vscode/settings.json
{
  "latex-workshop.latex.tools": [
    {
      "name": "latexmk",
      "command": "latexmk",
      "args": [
        "-shell-escape",
        "-synctex=1",
        "-interaction=nonstopmode",
        "-file-line-error",
        "-pdf",
        "-outdir=%OUTDIR%",
        "%DOC%"
      ],
      "env": {}
    }
  ]
}

If the compilation is successful, make sure to collect the minted cache files (those with the .pyg{style,tex} extension) and include them in the final submission. Finally, change the finalizecache option to frozencache to avoid needing the --shell-escape option in the future:

\usepackage[frozencache,cachedir=.]{minted}

The main drawback of this approach is that you need to recompile the document every time you change the code. Therefore it is recommended to use this approach only when the document is in its final form, before submission.

Glossaries: Custom .latexmkrc

latexmk is a tool able to create some targets and to run all the LaTeX commands in the correct order to produce the desidered output.
Since some packages, like glossaries, need some specific build steps, it may become tedious to repeat them manually between each build. This can be solved by adding a .latexmkrc configuration file like this:

.latexmkrc

# Add makeglossaries to the list of commands
add_cus_dep('glo', 'gls', 0, 'run_makeglossaries');
add_cus_dep('acn', 'acr', 0, 'run_makeglossaries');

# Clean up the temporary files after running latex, pdflatex,
push @generated_exts, 'glo', 'gls', 'glg';
push @generated_exts, 'acn', 'acr', 'alg';
$clean_ext .= ' %R.ist %R.xdy';

sub run_makeglossaries {
    my ($base_name, $path) = fileparse( $_[0] ); #handle -outdir param by splitting path and file, ...
    pushd $path; # ... cd-ing into folder first, then running makeglossaries ...

    if ( $silent ) {
        if ( "$^O" eq "MSWin32" ) {
            system "makeglossaries", "-q", "$base_name";
        } else {
            system "makeglossaries -q $base_name";
        }
    }
    else {
        if ( "$^O" eq "MSWin32" ) {
            system "makeglossaries", "$base_name";
        } else {
            system "makeglossaries $base_name";
        }
    };

    popd; # ... and cd-ing back again
}