How to use LaTeX directives

Many programming languages (including LaTeX) have directives. Directives are a type of pragma–a specifically formatted comment that tells the compiler to do something specific.

LaTeX pragma

For LaTeX, de facto directives are compatible with popular LaTeX editors including Atom and TeXstudio.

Specify LaTeX compiler

At the top of your main/root LaTeX document:

% !TeX TS-program = xelatex

or other compiler of your choice.

Specify root LaTeX file

Particularly for large or multi-chapter LaTeX documents, it is good practice to break up a large LaTeX file into multiple per-chapter files. To help your LaTeX editor recognize which is the top-level document, in the other .tex files you \include{} or \input{}, put at the top of each of those .tex files:

% !TEX root = thesis.tex

or whatever your main document filename is.


If XeTeX directive:

% !TeX program used: xelatex

yields error:

Error: One command expansion invalid.
  Parent Command: compile
  Primary Command: compile

Fix by:

  1. install XeLaTeX compiler

    • Linux: apt install texlive-xetex
    • MacOS: brew install mactex
    • Windows: get XeLaTeX via MikTeX
  2. Restart TeXstudio