keyboard-keys Getting started with LaTeX

The LaTeX document preparation system is a popular markup language for scientific and engineering documents. Learn to write your first document.

The LaTeX (more formally stylized as LATEX) document preparation system has an interesting history. When programmer Don Knuth wrote his first book, The Art of Computer Programming, in 1968, it was produced using an old-style printing press method. When he published the second edition in 1976, the publisher had moved to modern phototypesetting.

Knuth was unhappy with how the new edition looked. Addressing the problem from a programmer's perspective, Knuth decided to create his own text processing system so his future books could be formatted to look the same way, for every book in the series. And so it was that Don Knuth wrote the first version of TeX in 1978.

A few years later, Leslie Lamport created a set of macros that help authors write complex documents more easily. Lamport's macro extensions, LaTeX, essentially extends TeX to easily produce all kinds of documents. For example, many academic organizations use LaTeX to publish journals and proceedings.

Writing documents in LaTeX

LaTeX provides a fairly straightforward syntax that makes it easy to write most text documents. We will explore the more advanced formatting in LaTeX in another article, but let's start by writing a brief two-paragraph document that borrows text from the Technically We Write website:

\documentclass{article}
\begin{document}

Technically We Write is a community of technical writers,
technical editors, copyeditors, web content writers,
and all other roles in technical communication.

We'd love to share your story! Your article can be about
anything related to technical communication, such as tools,
tips, how you got started, what you've learned, or other
topics in technical communication. Contact our editors to
share your article idea.

\end{document}

Like other document formatting programs, LaTeX collects words and fills paragraphs. That means you can add new text in the middle of a paragraph and not worry about how the final document will look. As long as you don't add a blank line in the middle of a paragraph, LaTeX creates fully justified paragraphs. When it finds a blank line in the document, LaTeX starts a new paragraph.

LaTeX needs a few control statements to define the document. Every LaTeX document should start with a declaration of the document's class. LaTeX supports several kinds of documents, including letters, books, and articles. For this example, I used \documentclass{article} to set the article class.

Tell LaTeX where the text begins and ends with the \begin{document} and \end{document} statements. If you add text before the \begin{document}, LaTeX generates an error. Any text after \end{document} is not valid.

I've added some extra space around the LaTeX instructions so they are easier to see, but LaTeX doesn't really care about the blank lines and extra spaces.

To generate printable output from this source file, run the latex command:

$ latex about.tex
This is pdfTeX, Version 3.141592653-2.6-1.40.24 (TeX Live 2022) (preloaded format=latex)
restricted \write18 enabled.
entering extended mode
(./about.tex
LaTeX2e <2022-06-01> patch level 5
L3 programming layer <2022-12-17>
(/usr/share/texlive/texmf-dist/tex/latex/base/article.cls
Document Class: article 2021/10/04 v1.4n Standard LaTeX document class
(/usr/share/texlive/texmf-dist/tex/latex/base/size10.clo))
(/usr/share/texlive/texmf-dist/tex/latex/l3backend/l3backend-dvips.def)
(./about.aux) [1] (./about.aux) )
Output written on about.dvi (1 page, 824 bytes).
Transcript written on about.log.

LaTeX produces a lot of text so you can see what it is doing. If your document contains errors, LaTeX prints a message, and possibly prompts for what it should do. In most cases, you can type x at the prompt to force LaTeX to exit.

If LaTeX was successful in generating a document, it produces a file with a .dvi extension. The DVI stands for Device Independent because you can use a variety of tools to create other kinds of output. For example, the dvipdf program converts the DVI file to a PDF file. But if you want a PDF file as your output, use the pdflatex command instead of the separate latex and dvipdf commands:

$ pdflatex about.tex
This is pdfTeX, Version 3.141592653-2.6-1.40.24 (TeX Live 2022) (preloaded format=pdflatex)
restricted \write18 enabled.
entering extended mode
(./about.tex
LaTeX2e <2022-06-01> patch level 5
L3 programming layer <2022-12-17>
(/usr/share/texlive/texmf-dist/tex/latex/base/article.cls
Document Class: article 2021/10/04 v1.4n Standard LaTeX document class
(/usr/share/texlive/texmf-dist/tex/latex/base/size10.clo))
(/usr/share/texlive/texmf-dist/tex/latex/l3backend/l3backend-pdftex.def)
(./about.aux) [1{/usr/share/texlive/texmf-var/fonts/map/pdftex/updmap/pdftex.ma
p}] (./about.aux) )</usr/share/texlive/texmf-dist/fonts/type1/public/amsfonts/c
m/cmr10.pfb>
Output written on about.pdf (1 page, 17441 bytes).
Transcript written on about.log.

a printed LaTeX document

image: Jim Hall (Creative Commons)

Enumerated and itemized lists

LaTeX supports two kinds of lists: an enumerated list where each item starts with a number, and an itemized or "bullet" list. An enumerated list starts with \begin{enumerate} and ends with the \end{enumerate} statement. Similarly, itemized lists use \begin{itemize} and \end{itemize}. Each item in the list starts with the \item instruction.

For example, to add an enumerated list after the second paragraph to list the ways that folks can get involved with Technically We Write, write this list:

\begin{enumerate}

 \item Write with us!

 \item Read and share our stories

\end{enumerate}

a printed LaTeX document

image: Jim Hall (Creative Commons)

You can also add a list within a list. This is a neat feature if you need to provide a list with several options for each item. As an example, we can update the list to provide several ways that folks can contribute to Technically We Write. The embedded list uses its own \begin{..} and \end{..} statements:

\begin{enumerate}

 \item Write with us!

 \begin{itemize}
   \item Technical writers
   \item Students
   \item Open source tools
   \item Command line
   \item Interviews
   \item Personal journeys
   \item Professional communication
 \end{itemize}

 \item Read and share our stories

\end{enumerate}

a printed LaTeX document

image: Jim Hall (Creative Commons)

Sections and subsections

You can make a long document easier to read by breaking it up into sections. To add a section title to a LaTeX document, use the \section{..} statement, and write the section's title inside the braces. To add a subsection or second level heading, use the \subsection{..} statement. Let's demonstrate by adding a section to the top of the document, and a subsection before the second paragraph:

\section{About Us}

Technically We Write is a community of technical writers,
technical editors, copyeditors, web content writers,
and all other roles in technical communication.

\subsection{Welcome to the Community}

a printed LaTeX document

image: Jim Hall (Creative Commons)

The article document class adds a number before each major section, and increases the font size so it stands out in the document.

Adding emphasis

Scientific and other technical documents often include terms and phrases that need to carry special emphasis. LaTeX provides several font effects you can use in technical documents, including emphasis text (usually displayed in italics), bold text, and small caps.

When you need to add emphasis, use the \emph{..} command, with the word or phrase between the curly braces. To display text in italics, boldface, or small caps, use a variation of the \text..{..} command, such as \textit{..} for italics, \textbf{..} for boldface, and \textsc{..} for small caps. LaTeX supports lots of other ways to style text, but these styles get you pretty far in writing scientific documents.

Let's demonstrate a few of these by updating the LaTeX document. We can set "Technically We Write" in small caps, and add emphasis to "love," and bold to the "Contact our editors" action statement:

\textsc{Technically We Write} is a community of technical writers,
technical editors, copyeditors, web content writers,
and all other roles in technical communication.

\subsection{Welcome to the Community}

We'd \emph{love} to share your story! Your article can be about
anything related to technical communication, such as tools,
tips, how you got started, what you've learned, or other
topics in technical communication. \textbf{Contact our editors} to
share your article idea.

a printed LaTeX document

image: Jim Hall (Creative Commons)

I've only touched on a few ways to write scientific and technical documents in LaTeX. Depending on your needs, you can also use LaTeX to add a cover page, insert footnotes, create tables, and typeset mathematical equations and expressions. Watch for follow-up articles that explore these other features in more depth.