Icon for parent category 'Writing'

Basic Structure

Every document starts with the same few basic commands. Like telling Microsoft Word that you want the paper a specific size, Latex also benefits from knowing what you’re trying to do.

Below is the basic structure of a Latex document.

1\documentclass[options]{documentType}
2% ... packages ...
3
4\begin{document}
5    % ... everything ... 
6\end{document}

The first part—everything that is before the document environment—is called the preamble. It is used to tell LaTeX to load what we need and determine the type of document. Only what’s inside the document environment is actually shown in the document.

 1\documentclass{article}
 2\usepackage[english]{babel}
 3
 4%In the preamble...
 5
 6\begin{document} 
 7    Inside the document...
 8\end{document} 
 9
10After the document
Code left > output right
Code left > output right
Remark

After the \end{document} command you can place anything you like (comments, leftover notes, pieces about which you still have doubts). It’s not going to show up in the document anyway!

Packages

LaTeX wants to compile as quickly as possible, while keeping the file size low as well. Therefore, you need to manually include packages if you want more than very basic commands. The syntax is

1\usepackage[optionalArgument]{packageName}

You can include as many as you like, or none at all. Most of these packages are already installed on your computer if you’ve installed a TeX distribution, but you can create or download your own custom packages.

Document Class

The document class decides the type of document you’re creating. This mostly influences which commands you’re able to use and how the finished product will look. Next chapter will tell you everything about it!

Importing Files

When your project grows in size, it’s useful to split it into multiple TeX files. (Usually one per chapter or section). These can then be combined into one main TeX file.

In the preamble, you can specify which files are going to be imported somewhere with \includeonly{filename, filename, …}.

Within the document itself, use \include{filename} to actually copy it there.

Note that LaTeX starts an included document on a new page. If you don’t want that, you can use \input{filename} to just literally dump the content of the other file right there.

1\documentclass{article}
2\includeonly{latex.tex}
3
4\begin{document}
5\input{latex.tex}
6\end{document} 
7
8%The contents of latex.tex are... This is everything that's inside latex.tex 
Code left > output right
Code left > output right

You don’t necessarily have to tell LaTeX which files you’re planning on including in the preamble. But it’s the best practice, as it increases performance. LaTeX doesn’t need to search everywhere when you include a file, but knows what’s coming.

Support me and this website!

Want to support me?

Buy one of my projects. You get something nice, I get something nice.

Donate through a popular platform using the link below.

Simply giving feedback or spreading the word is also worth a lot.