Fix the fatal fontspec error in LaTeX.
The error Fatal Package fontspec Error: The fontspec package requires either XeTeX or LuaTeX appears when you compile a document that loads fontspec with pdfLaTeX or standard LaTeX. The package relies on font-handling primitives that only XeTeX and LuaTeX provide.
The short answer: switch your engine from pdfLaTeX to XeLaTeX or LuaLaTeX in your editor settings. If your publisher forces you to use pdfLaTeX, remove \usepackage{fontspec} and use traditional font packages like lmodern with fontenc instead.
Why the error happens
Traditional pdfLaTeX handles fonts through TeX font metric files (.tfm) and Type 1 PostScript outlines. The fontspec package works completely differently: it loads OpenType (.otf) and TrueType (.ttf) fonts installed on your operating system or stored inside your project folder.
Because pdfLaTeX lacks the internal machinery to read OpenType font tables, fontspec stops immediately upon loading. It throws either Fatal fontspec error: "cannot-use-pdftex" or The fontspec package requires either XeTeX or LuaTeX to prevent corrupted PDF generation.
You might see this error even if you never wrote \usepackage{fontspec} in your own preamble. Many modern CV templates (such as Awesome-CV), multilingual packages like polyglossia, and unicode math packages load fontspec behind the scenes.
Fix 1: Switch your compiler to XeLaTeX or LuaLaTeX
Switching your project's engine is the standard solution whenever you want custom OpenType fonts or unicode text.
In Texeno
Open Project Settings from the left sidebar or top bar. Under Compiler Engine, choose XeLaTeX or LuaLaTeX. Both engines run on TeX Live 2026 with full fontspec support.
In Overleaf
Click the Menu icon in the top-left corner above the file tree. In the Compiler dropdown, change pdfLaTeX to XeLaTeX or LuaLaTeX, then recompile.
In VS Code (LaTeX Workshop)
Add a magic comment at the very top of your root
.texfile, or set the default recipe insettings.jsontolatexmk (xelatex).% !TEX program = xelatex \documentclass{article} \usepackage{fontspec}In TeXstudio
Add the program directive line at the very top of your document, or go to Options -> Configure TeXstudio -> Build and set the Default Compiler to XeLaTeX.
%!TEX TS-program = xelatex
On the command line or terminal
Run
xelatexor directly, or instruct to invoke the modern engine.
Fix 2: Keep pdfLaTeX and replace fontspec
Clean up preamble conflicts after switching
What to check if compilation fails after switching
If changing the engine clears the fatal error but compilation halts on a new message, check these two common causes.
Package fontspec Error: The font cannot be found
This happens when your code requests an operating system font (like Times New Roman, Calibri, or SimSun) that is not installed on the Linux server running your compiler. Supply the font file (.otf or .ttf) in your project directory and reference it by filename.
\setmainfont{CustomFont}[ Path = ./fonts/ , Extension = .otf , UprightFont = *-Regular , BoldFont = *-Bold ]Package unicode-math conflicts
If you load both
amssymbandunicode-math, symbol definitions will clash. Removeamssymband load onlyunicode-mathwhen compiling under XeLaTeX or LuaLaTeX.
Questions
Can I use fontspec with pdfLaTeX?
No. The fontspec package depends on primitive operations that only exist in XeTeX and LuaTeX. If you must use pdfLaTeX, replace fontspec with standard font packages such as lmodern or mathptmx.
Which engine should I pick: XeLaTeX or LuaLaTeX?
LuaLaTeX is the modern default for TeX Live and handles complex font shaping, large microtype adjustments, and dynamic memory allocation smoothly. XeLaTeX remains a solid choice for legacy templates or East Asian scripts using xeCJK.
Try your own paper in Texeno.
Start free, upload a project as a .zip and press Recompile. Upgrade when you need longer compiles.
Start writing