LaTeX compiler directives are very useful

less than 1 minute read

Many programming languages (including LaTeX) have compiler directives–a type of pragma in the form of a specifically formatted comment.

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 master 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 you get error

%!TeX program used: xelatex

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

Fix by:

  1. install XeLaTeX compiler
    • Linux: apt install texlive-xetex
    • Mac
    • Windows: get XeLaTeX via MikTeX
  2. Restart TeXstudio

Leave a Comment