File types · Explainer

The TEX file (.tex)

A TEX file is the plain-text source of a document written in TeX or LaTeX: text plus commands that a TeX engine typesets into a PDF. This guide explains what's inside one, how it becomes a PDF, the errors and risks worth knowing, and has free sample TEX files to download for testing.

At a glance

Full name
TeX or LaTeX source file
Extensions
.tex; related: .sty (packages), .cls (classes), .bib (bibliographies)
MIME type
None registered; application/x-tex and text/x-tex are common
Kind
Plain text: markup and macros, compiled into a PDF (or DVI)
Owner
TeX: Donald Knuth, frozen at version 3.141592653. LaTeX: the LaTeX Project Team
First released
TeX in 1978 (rewritten as TeX82 in 1982); today's LaTeX, LaTeX2e, in 1994
Engines
pdfTeX (pdflatex), XeTeX (xelatex), LuaTeX (lualatex)
Opens with
Any text editor to read; TeX Live, MiKTeX or MacTeX to compile; Overleaf online
Encoding
UTF-8 is the LaTeX default since 2018; older files may be Latin-1
Magic number
None; LaTeX files usually start with comments or \documentclass

The basics

What is a TEX file?

A .tex file is the source code of a document. You write the text together with commands such as \section{…} or $x^2$, and a program, a TeX engine, turns it into a typeset PDF. It's the standard way to write papers in mathematics, physics and computer science, and common for theses, books, slides and anything with many formulas. Preprint servers such as arXiv take the LaTeX source and compile it themselves.

Unlike a DOCX file, a TEX file isn't what readers see. It's closer to a program: it can define its own commands, include other files, loop and branch, and it has to be compiled before anyone can read the result. In return you get excellent typesetting of maths, automatic numbering and cross-references, consistent layout, and a plain-text source that works with version control and lasts.

Opening a .tex file in a text editor shows the source. To see the document, compile it with a TeX distribution on your computer or upload it to Overleaf, or ask the author for the PDF.

Anatomy

What's inside a TEX file

A LaTeX file has two parts: the preamble, which sets up the document, and the body between \begin{document} and \end{document}, which holds the content. Commands start with a backslash. Here is a short but complete file, part by part.

notes.tex
% notes.tex: compile with  pdflatex notes.tex
\documentclass[a4paper,11pt]{article}
\usepackage{amsmath}

\title{Notes}
\author{Ada Example}

\begin{document}
\maketitle

\section{Introduction}\label{sec:intro}
Text with \emph{emphasis} and maths: $E = mc^2$.
\begin{equation}
  \int_0^1 x^2 \, dx = \frac{1}{3}
\end{equation}
As Section~\ref{sec:intro} shows, \LaTeX{} numbers things for you.
\end{document}
  1. 1% notes.tex: …

    A comment

    % hides the rest of the line from TeX. A literal percent sign has to be written \%.

  2. 2\documentclass[a4paper,11pt]{article}

    The document class

    Always first. The class (article, report, book, beamer for slides, or a journal's own) decides the layout: page size, fonts, how headings look. Options go in square brackets, required arguments in braces.

  3. 3\usepackage{amsmath}

    A package

    Adds features, here better maths environments. A real document often loads ten or twenty packages. Each must be installed in the TeX distribution, or compiling stops with File not found.

  4. 4\title{Notes} \author{…}

    Document information

    Stored now, printed later by \maketitle. The class decides how the title block looks.

  5. 5\begin{document}

    The start of the body

    Everything before it is the preamble, which may only set things up. Text before this line is an error.

  6. 6\section{Introduction}\label{sec:intro}

    A numbered section with a label

    LaTeX numbers sections, equations, figures and tables itself. \label gives the number a name that \ref can use anywhere else in the document.

  7. 7\emph{emphasis}

    Markup by meaning

    \emph says "emphasise this", and the class picks italics. This separation of content and appearance is the point of LaTeX.

  8. 8$E = mc^2$

    Inline maths

    Dollar signs switch to maths mode, where letters become italic variables and ^ makes a superscript. Forgetting one is the source of the classic Missing $ inserted error.

  9. 9\begin{equation} … \end{equation}

    A displayed equation

    Centred on its own line and numbered. \frac, \int and \, (a thin space) are maths commands.

  10. 10Section~\ref{sec:intro}

    A cross-reference

    \ref prints the number of the label. The ~ is a space where the line may not break. On the first compile it prints ??, because numbers are only known at the end; the second run fills it in.

  11. 11\end{document}

    The end

    Anything after this line is ignored, which is why some authors keep notes there.

Versions and variants

TeX vs LaTeX, and pdfTeX vs XeTeX vs LuaTeX

TeX is the typesetting program Donald Knuth wrote, first released in 1978 and rewritten as TeX82 in 1982. Knuth froze it: bug fixes only, with the version number creeping towards π (it is 3.141592653). TeX on its own is low-level. Plain TeX is Knuth's small set of macros on top. LaTeX, started by Leslie Lamport in the 1980s, is a much larger macro package that adds document structure: sections, lists, figures, references. The LaTeX used today is LaTeX2e, released in 1994 and maintained by the LaTeX Project Team, with new releases about twice a year. Almost every .tex file you meet is LaTeX.

An engine is the program that actually runs the file. The original TeX writes DVI files; modern engines extend it:

EngineLaTeX commandOutputFonts and textGood for
pdfTeXpdflatexPDF directly8-bit fonts; reads UTF-8 but can't show scripts it has no fonts forThe default for most English and European documents, journals and arXiv
XeTeXxelatexPDFUnicode and any OpenType or TrueType font installed on the systemNon-Latin scripts and documents in a specific system font
LuaTeXlualatexPDFUnicode and OpenType, plus the Lua programming language built inNew documents, complex scripts, accessible (tagged) PDFs
TeX (Knuth's)tex, latexDVIOriginal Computer Modern fontsPlain TeX and reproducing old documents

A file written for pdflatex usually compiles with the other engines too, but not the other way round: our sample-unicode.tex uses babel with system fonts and needs XeLaTeX or LuaLaTeX. Other macro packages exist besides LaTeX, notably ConTeXt, which uses .tex files with different commands.

Tools

Distributions, editors and Overleaf

ToolWhat it isPlatforms
TeX LiveThe main TeX distribution: engines, fonts and thousands of packages from CTAN, with a new edition every yearLinux, Windows, macOS
MacTeXTeX Live packaged for the Mac, with Mac editors such as TeXShopmacOS
MiKTeXThe other major free distribution; can install missing packages the first time a document needs themWindows, macOS, Linux
OverleafA website that edits and compiles LaTeX in the browser, with real-time collaboration. It runs TeX Live on its serversAny browser
EditorsTeXstudio, TeXShop, VS Code with the LaTeX Workshop extension, Emacs with AUCTeX: syntax highlighting, one-click build and a PDF previewVaries

A full TeX Live installation takes several gigabytes; a basic one plus packages installed on demand is much smaller. Linux distributions also split TeX Live into many packages, so a missing .sty file often means a package wasn't installed rather than that it doesn't exist.

Under the hood

The build cycle and auxiliary files

TeX reads the file once from top to bottom. It can't know the number of a section it hasn't reached yet, so it writes what it learns to auxiliary files, and the next run reads them. That's why documents with references, a table of contents or a bibliography are compiled several times:

terminal
$ pdflatex notes.tex      # run 1: writes labels to notes.aux, prints ?? for references
$ pdflatex notes.tex      # run 2: reads notes.aux, references are filled in
$ bibtex notes            # only with a .bib file: writes notes.bbl
$ pdflatex notes.tex      # run 3 (and 4): citations appear
$ latexmk -pdf notes.tex  # or let latexmk work out how many runs are needed
FileWhat it holdsKeep it?
.texYour sourceYes
.bibYour bibliography database, for BibTeX or BiberYes
.pdfThe outputYes, to share
.auxLabels, citations and page numbers collected during the run, read back by the next oneNo, rebuilt each time
.logEverything TeX did, with every error and warning and its line numberOnly while debugging
.toc, .lof, .lotEntries for the table of contents and lists of figures and tablesNo
.bbl, .blgThe formatted bibliography and BibTeX's logNo
.outPDF bookmarks written by hyperrefNo
.synctex.gzThe link between source lines and PDF positions that lets editors jump between themNo

If a document suddenly won't compile after an error, delete the auxiliary files (latexmk -c does it) and build again: a half-written .aux from a failed run can break the next one.

Troubleshooting

Common LaTeX errors and what they mean

MessageUsual causeFix
! Undefined control sequence.A typo in a command, or a command from a package you didn't load. The log shows the line and the commandCorrect the spelling, or add the \usepackage it needs.
! Missing $ inserted.A maths character such as _ or ^ in normal text, or a missing closing $Wrap the maths in $…$, or write \_ for a literal underscore.
! LaTeX Error: File … not found.The package isn't installed, or an \input or image path is wrongInstall the package (TeX Live Manager, MiKTeX Console or your Linux package manager), or fix the path.
! LaTeX Error: Unicode character … not set up for use with LaTeX.pdfLaTeX met a character it has no font for: an emoji, Chinese, Greek without the right packagesCompile with XeLaTeX or LuaLaTeX and a font that has the character.
?? in the PDF instead of a numberReferences not resolved yet, or a \ref to a label that doesn't existCompile again; if they stay, check the label names. The log lists undefined references.
Overfull \hbox (12.3pt too wide)A warning: a line sticks into the margin, often because of a long URL or word TeX can't hyphenateRephrase, allow a break, or load microtype. The PDF is still produced.
Runaway argument? or File ended while scanningA missing closing brace }Look just before the line the log names.

When a compile stops at an error in interactive mode, TeX waits at a ? prompt. Type x and Enter to quit, or use -interaction=nonstopmode so it carries on and writes everything to the log.

How to

How to convert a TEX file to PDF, Word or HTML

  • To PDF: compile it: pdflatex file.tex (twice), latexmk -pdf file.tex, the build button in your editor, or upload the project to Overleaf. Include every file it uses: images, .bib, custom .sty and .cls files.
  • To Word (DOCX): Pandoc converts structure, lists, tables and equations (as editable Word equations): pandoc file.tex -o file.docx. Custom macros, complex layouts and TikZ drawings don't survive; in our test, Pandoc kept the table and the maths of sample-article.tex and left out the TikZ figure.
  • To HTML: pandoc file.tex -s --mathjax -o file.html, or make4ht (from TeX Live) and LaTeXML for documents that rely more on LaTeX's own macros.
  • From Word or Markdown to LaTeX: Pandoc works in that direction too, which is a quick start for a journal template.
  • Just to read it: any text editor. The source of a well-written paper is quite readable on its own.

Security

Is it safe to compile a TEX file?

Reading a .tex file is safe: it's text. Compiling one runs it, and TeX is a full programming language with access to files and, optionally, other programs:

  • Shell escape (\write18). With --shell-escape, a document can run any command on your computer. TeX Live's default is a restricted mode that only allows a short list of tools considered safe (such as bibtex and makeindex). Packages like minted and some graphics workflows ask you to turn full shell escape on: only do that for documents you trust.
  • Reading files. \input and \openin can read any file the user can, and TeX Live's default (openin_any = a) allows it. A document can print the contents of a file into the PDF, so a compile service that returns PDFs can leak its own files.
  • Writing files. TeX Live restricts writes to the current folder and below by default (openout_any = p). Researchers showed around 2010 that a crafted LaTeX file could write a script into the Windows Startup folder on a MiKTeX system, which lacked that restriction.
  • Endless loops. A one-line macro such as \def\x{\x}\x makes TeX run until it's stopped.

Compile untrusted .tex files in a sandbox or container (Overleaf runs every compile in one), never as an administrator, with shell escape off (-no-shell-escape) and a time limit. Filtering commands out of the source doesn't work: TeX's syntax is flexible enough to hide any command.

Free downloads

Sample TEX files to download

Free sample TeX and LaTeX files, plus the PDFs they compile to, for testing editors, converters, compile services and previews. Every file compiles without errors or overfull boxes in TeX Live 2023.

sample-article.tex · 4.9 KBDownloadOpen
Loading…

What's inside sample-article.tex

The file is commented: open it in a text editor and each numbered comment marks one of these parts. Compare it with sample-article.pdf, which is this file compiled.

  1. Comments 1 to 3: class and preamble

    article on A4 at 11 pt, then geometry, amsmath, booktabs, tikz, microtype and hyperref, and the title, author and PDF metadata.

  2. Comment 4: your own command

    \newcommand{\file}[1]{\texttt{#1}} defines \file{…}, used for file names throughout.

  3. Comment 5: sections and labels

    Numbered sections with \label, referred to with \ref and cited with \cite.

  4. Comment 6: maths

    Inline maths, a numbered equation and an align block lined up at &, referred to with \eqref.

  5. Comment 7: a table

    A floating table with booktabs rules and a caption, comparing three engines.

  6. Comment 8: a figure drawn in code

    A TikZ diagram of the build cycle: no image file, so it stays sharp at any zoom.

  7. Comment 9: why two runs

    References and the page number in section 5 are only filled in on the second pdflatex run, from the .aux file.

  8. Comment 10: the bibliography

    A thebibliography list with two entries, so no BibTeX run is needed.

All sample TEX files

  • Commented LaTeX article

    sample-article.tex · 4.9 KB (5,057 bytes)

    Sections, maths, a table, a TikZ figure, cross-references and a bibliography, with ten numbered comments. Compiles with pdflatex (twice) using only standard packages.

    • Testing LaTeX compile services and editors
    • Teaching the parts of a LaTeX document
  • The article, compiled

    sample-article.pdf · 185.1 KB (189,571 bytes)

    Two A4 pages produced by pdfTeX from sample-article.tex, with embedded Type 1 fonts, PDF bookmarks and clickable references.

    • Comparing your own build with the expected output
    • Checking converters against a known result
  • Minimal plain TeX

    sample-plain.tex · 796 bytes

    Plain TeX without LaTeX: a font loaded by hand, text, inline and displayed maths, and \bye. Compile with pdftex (PDF) or tex (DVI).

    • Testing that a tool tells plain TeX from LaTeX
    • The smallest useful TeX document
  • UTF-8 with non-Latin scripts

    sample-unicode.tex · 2.2 KB (2,258 bytes)

    Accented Latin, Greek, Russian, Japanese, Arabic and Hebrew (right to left) with babel and system fonts (DejaVu, Noto Serif CJK). Needs XeLaTeX (or LuaLaTeX); pdfLaTeX stops with babel errors.

    • Checking encoding handling in editors and pipelines
    • Testing which engine a compile service uses
  • The UTF-8 file, compiled with XeLaTeX

    sample-unicode.pdf · 56.6 KB (57,983 bytes)

    One page from sample-unicode.tex, with the Arabic and Hebrew paragraphs set right to left and the fonts embedded.

    • Comparing font and script rendering
    • Checking text extraction from a multilingual PDF
  • Beamer slide deck

    sample-slides.tex · 1.7 KB (1,787 bytes)

    Three frames in the built-in Madrid theme at 16:9: a title slide, a list revealed step by step (\item<2->), and two columns with a table and a block. Compiles to five PDF pages with pdflatex.

    • Testing Beamer support in compile services
    • A starting point for LaTeX slides

Ways to test with them

  • Compile services: upload sample-article.tex and compare the result with sample-article.pdf. ?? in place of numbers means the service ran LaTeX only once.
  • Engine detection: sample-unicode.tex compiles under XeLaTeX and stops under pdfLaTeX with This macro is available only in LuaLaTeX and XeLaTeX, so it shows at once which engine a service runs.
  • Previews and syntax highlighting: open the .tex files in your viewer: comments, commands, maths and environments should be told apart.
  • Conversion: run pandoc sample-article.tex -o out.docx or your converter and check which parts survive: headings, equations, the table, the TikZ figure.

Free to use for testing, teaching and demos, commercially or not, with no attribution needed. A link back to this page is always appreciated.

Put it online

Have a TEX file? Give it a link.

Drop a .tex file on 99helpers and it gets its own web address, where it opens as plain text: the source, not the typeset document. To share what readers should see, put the compiled PDF online next to it.

Questions

TEX files, frequently asked

What is a .tex file?

The source of a document written in TeX or LaTeX: plain text with commands. A TeX engine compiles it into a PDF.

How do I open a TEX file?

Any text editor shows the source. To see the formatted document, compile it with TeX Live, MiKTeX or MacTeX, or upload it to Overleaf and press Recompile.

How do I convert a TEX file to PDF?

Run pdflatex file.tex twice, or latexmk -pdf file.tex, or use the build button of a LaTeX editor or Overleaf. All images and other files the document uses must be in place.

What's the difference between TeX and LaTeX?

TeX is the typesetting engine Donald Knuth wrote. LaTeX is a large set of macros on top of TeX that adds document structure such as sections, figures and references. Most .tex files are LaTeX.

Should I use pdfLaTeX, XeLaTeX or LuaLaTeX?

pdfLaTeX if a journal or template asks for it. XeLaTeX or LuaLaTeX for non-Latin scripts or system fonts. LuaLaTeX for the newest features, such as tagged, accessible PDFs with maths.

Why does my LaTeX document show ?? instead of numbers?

References are resolved from the .aux file written by the previous run. Compile again; if they stay, the label doesn't exist or is misspelled.

Can I convert a TEX file to Word?

Yes, with Pandoc: pandoc file.tex -o file.docx. Text, headings, tables and equations convert; custom macros and TikZ drawings usually don't.

Is it safe to compile a LaTeX file from someone else?

With shell escape off, the main risks are reading your files into the PDF and endless loops. Never use --shell-escape on files you don't trust, and compile them in a sandbox if you run a service.

Related formats

Other file types explained

Keep going

Related

Sources

Last reviewed 27 October 2026.