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: de facto directives are compatible with popular LaTeX editors including TeXstudio.

Specify LaTeX compiler: at the top of the main/root LaTeX document:

% !TeX TS-program = xelatex

Specify root LaTeX file: 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

If XeTeX directive:

% !TeX program used: xelatex

yields:

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

Reference: short list of LaTeX directives and extended list.