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: 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
% notes.tex: …A comment
%hides the rest of the line from TeX. A literal percent sign has to be written\%. - 2
\documentclass[a4paper,11pt]{article}The document class
Always first. The class (
article,report,book,beamerfor 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
\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
\title{Notes} \author{…}Document information
Stored now, printed later by
\maketitle. The class decides how the title block looks. - 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
\section{Introduction}\label{sec:intro}A numbered section with a label
LaTeX numbers sections, equations, figures and tables itself.
\labelgives the number a name that\refcan use anywhere else in the document. - 7
\emph{emphasis}Markup by meaning
\emphsays "emphasise this", and the class picks italics. This separation of content and appearance is the point of LaTeX. - 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 classicMissing $ insertederror. - 9
\begin{equation} … \end{equation}A displayed equation
Centred on its own line and numbered.
\frac,\intand\,(a thin space) are maths commands. - 10
Section~\ref{sec:intro}A cross-reference
\refprints 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
\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:
| Engine | LaTeX command | Output | Fonts and text | Good for |
|---|---|---|---|---|
| pdfTeX | pdflatex | PDF directly | 8-bit fonts; reads UTF-8 but can't show scripts it has no fonts for | The default for most English and European documents, journals and arXiv |
| XeTeX | xelatex | Unicode and any OpenType or TrueType font installed on the system | Non-Latin scripts and documents in a specific system font | |
| LuaTeX | lualatex | Unicode and OpenType, plus the Lua programming language built in | New documents, complex scripts, accessible (tagged) PDFs | |
| TeX (Knuth's) | tex, latex | DVI | Original Computer Modern fonts | Plain 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
| Tool | What it is | Platforms |
|---|---|---|
| TeX Live | The main TeX distribution: engines, fonts and thousands of packages from CTAN, with a new edition every year | Linux, Windows, macOS |
| MacTeX | TeX Live packaged for the Mac, with Mac editors such as TeXShop | macOS |
| MiKTeX | The other major free distribution; can install missing packages the first time a document needs them | Windows, macOS, Linux |
| Overleaf | A website that edits and compiles LaTeX in the browser, with real-time collaboration. It runs TeX Live on its servers | Any browser |
| Editors | TeXstudio, TeXShop, VS Code with the LaTeX Workshop extension, Emacs with AUCTeX: syntax highlighting, one-click build and a PDF preview | Varies |
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:
$ 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| File | What it holds | Keep it? |
|---|---|---|
.tex | Your source | Yes |
.bib | Your bibliography database, for BibTeX or Biber | Yes |
.pdf | The output | Yes, to share |
.aux | Labels, citations and page numbers collected during the run, read back by the next one | No, rebuilt each time |
.log | Everything TeX did, with every error and warning and its line number | Only while debugging |
.toc, .lof, .lot | Entries for the table of contents and lists of figures and tables | No |
.bbl, .blg | The formatted bibliography and BibTeX's log | No |
.out | PDF bookmarks written by hyperref | No |
.synctex.gz | The link between source lines and PDF positions that lets editors jump between them | No |
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
| Message | Usual cause | Fix |
|---|---|---|
! 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 command | Correct 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 wrong | Install 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 packages | Compile with XeLaTeX or LuaLaTeX and a font that has the character. |
?? in the PDF instead of a number | References not resolved yet, or a \ref to a label that doesn't exist | Compile 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 hyphenate | Rephrase, allow a break, or load microtype. The PDF is still produced. |
Runaway argument? or File ended while scanning | A 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.styand.clsfiles. - 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 ofsample-article.texand left out the TikZ figure. - To HTML:
pandoc file.tex -s --mathjax -o file.html, ormake4ht(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 asbibtexandmakeindex). Packages likemintedand some graphics workflows ask you to turn full shell escape on: only do that for documents you trust. - Reading files.
\inputand\openincan 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}\xmakes 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.
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.
Comments 1 to 3: class and preamble
articleon A4 at 11 pt, thengeometry,amsmath,booktabs,tikz,microtypeandhyperref, and the title, author and PDF metadata.Comment 4: your own command
\newcommand{\file}[1]{\texttt{#1}}defines\file{…}, used for file names throughout.Comment 5: sections and labels
Numbered sections with
\label, referred to with\refand cited with\cite.Comment 6: maths
Inline maths, a numbered
equationand analignblock lined up at&, referred to with\eqref.Comment 7: a table
A floating
tablewithbooktabsrules and a caption, comparing three engines.Comment 8: a figure drawn in code
A TikZ diagram of the build cycle: no image file, so it stays sharp at any zoom.
Comment 9: why two runs
References and the page number in section 5 are only filled in on the second
pdflatexrun, from the.auxfile.Comment 10: the bibliography
A
thebibliographylist 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 withpdftex(PDF) ortex(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
babeland 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 withpdflatex.- Testing Beamer support in compile services
- A starting point for LaTeX slides
Ways to test with them
- Compile services: upload
sample-article.texand compare the result withsample-article.pdf.??in place of numbers means the service ran LaTeX only once. - Engine detection:
sample-unicode.texcompiles under XeLaTeX and stops under pdfLaTeX withThis macro is available only in LuaLaTeX and XeLaTeX, so it shows at once which engine a service runs. - Previews and syntax highlighting: open the
.texfiles in your viewer: comments, commands, maths and environments should be told apart. - Conversion: run
pandoc sample-article.tex -o out.docxor 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
- CTAN: the LaTeX package (LaTeX2e)
- TeX FAQ: the LaTeX project and LaTeX2e
- TeX Users Group: TeX Live
- Overleaf: TeX Live 2026 is now available
- Checkoway, Shacham, Rescorla: Are Text-Only Data Formats Safe? (LaTeX attacks)
- Lacombe et al.: Can You Accept LaTeX Files from Strangers? Ten Years Later (2021)
Last reviewed 27 October 2026.