The Beginner’s Guide to Using TeX in OS X

Tutorial Details
• Topics: TeX, LaTeX
• Difficulty: Beginner
• Estimated Completion Time: 30 minutes

TeX is a wonderful and very versatile typesetting system that can be used to write almost anything – from reports to letters and even more complicated documents such as books and posters. This tutorial will show you the basics of TeX – how it works and how you can create a nice, simple document with very little effort.

You Can Do It!

What scares people away from TeX is its learning curve. It’s not as simple as just opening up a word processor such as Pages or Microsoft Word and tapping away. With TeX, you have to learn the various commands before you can start creating documents. This may sound a bit scary, but TeX is a lot simpler to learn than other programming languages and once you’ve used it for a while you’ll soon pick it up really easy.

What is TeX?

I think it would be prudent to first give you a brief introduction of TeX and why it’s so popular within certain circles. Basically, it is a typesetting language that was designed and developed (mostly) by Donald Knuth, a professor at Stanford University, back in the 1970s. His aim with TeX was to create a system so that documents would look the same across all computers and that the look of the documents wouldn’t diminish with time.

TeX is used heavily in the academic and scientific world, mostly due to the standardized layout and look of documents (though this can be changed), as well as the fact that TeX renders mathematical formulas extremely well (one of its fortes). If anyone has had to write a long formula in, say, the Equation Editor of Microsoft Word, then they will know how fiddly it is.

Before we get started with our first TeX document, there are a couple of things you need to install on your Mac first.

Getting Started

In order to be able to write TeX documents on your Mac, you’ll first need to download and install a distribution, which renders your documents. There are quite a few TeX distributions out there that work with OS X, but my personal recommendation would be MacTeX, which is specifically written for the Mac and supports pretty much all OS X versions (including Mountain Lion).

You can download MacTeX directly here (direct download link – the file is approximately 2.1 GB in size) or via the Torrent network. Once you’ve installed it (it’ll pop up in a dedicated TeX folder in your applications) then run the TeX Live Utility, which will update your TeX distribution to the latest version.

If you’ve installed all the available updates in the TeX Live Utility, then you should be presented with this screen.

Just like HTML, you can write TeX is virtually any text editor or word processing package. However, I would recommend a specific TeX editor as you often get a handy list of tools with them (such as auto-complete and error reporting, which flags up any errors in your TeX coding before the document is rendered). With MacTeX, you get a couple of free solutions (TeXworks, which I will use for this tutorial, and TeXShop). There are also other options out there, such as TeXstudio and the commercial Texpad offering, which I reviewed last year on Mac AppStorm. You can also write TeX in commercial code editors such as Sublime Text with the relevant syntax, but you will need to open any documents up in a TeX editor so that they can be rendered. For beginners, I would recommend a dedicated TeX editor as it makes learning to write in TeX a lot easier!

Once you’ve installed everything, then it’s time to create your first TeX document!

Every TeX document starts by defining the document class. This sets the layout and formatting of your document. There are loads of different classes to choose from, but for a standard document the article class is a good one to choose. Therefore, start by typing:

\documentclass{article}


All TeX commands are preceded by a backslash (\) and defined within {curly brackets}. Unlike other programming languages, TeX commands usually are in plain English, so often it’s clear what the command is meant to accomplish.

Starting the document

Now that we’ve defined the document class, it’s time to start the document. Here, we type:

\begin{document}


Of course, you have to tell TeX where your document ends and this is done by the command \end{document}. Are you starting to see that TeX really isn’t that difficult? I usually type these commands at the same time, with four lines in between them. You don’t have to, but I find it helps immensely. If you’re following this example, then type:

\begin{document}
\end{document}


All your TeX coding should lie in between these two commands.

Titles and Authors

Now that we’ve defined the document class and where the document starts and ends, we need to give our document a title and author. Type:

\title{My First TeX Document}
\author{James Cull}
\maketitle


So far, your coding should look like the following:

Tip: Syntax colouring can make it a lot easier to spot individual TeX commands and functions. Most TeX editors have it on as default – if you’re using TexWorks, then click on Format > Syntax Coloring > LaTeX to activate it!

When you render this (by clicking on the little play button), then it should look like this:

If the document fails to render, then check the coding against my example. If you haven’t included the command \maketitle, for example, then the document won’t render as it will simply be empty (the command \maketitle actually places the title in the document, whereas the command \title simply defines it for the document).

Abstracts

If you want to write a little abstract for your document (if you are writing a report or academic paper, for example), then type it in after the \maketitle command:

\begin{abstract}
The abstract text for your document always goes after the title and before the main body of your document.
\end{abstract}


Sections and Subsections

Sections are defined by the command \section, with the section title again going in between two curly brackets, for example: \section{Introduction}. In our particular document class, article, sections are numbered (so the Introduction section would automatically be rendered as 1. Introduction). You can also create subsections (1.1, 1.2 and so on) and subsubsections (1.1.1, 1.1.2 and so on). The commands for these are \subsection{Subsection Title here} and subsubsection{Subsubsection Title here}.

All text for the individual sections goes straight underneath the section commands and unlike the abstract, you don’t have to start every single one with the \begin and \end commands. For this tutorial, I’ll create a couple of sample sections (the files above both contain some place filler text as well within each section) so you can see how they work:

\section{Introduction}
\subsection{More Introduction}
You can write some more text here.


Body Text and Formatting

If you’re not using sections, then you can simply start writing your main body text after the \maketitle command. Make sure you insert an empty line between paragraphs so that TeX knows when to start a new paragraph.

TeX supports text in bold and in italics through the \textbf and \textit commands. Remember to encase the text you would like in either bold or italics between the two curly brackets. So:

I would like to emphasise that \textbf{this} measure is unacceptable.
\textit{Microsoft Corporation} is one of the largest technology and software companies globally.


TeX also includes three default fonts, which can be seen in the example below:

Use the command \textsf{ } for text in the Sans Serif font and \texttt{ } for text in the Typewriter font. The coding for the image above is as follows:

\documentclass{article}
\begin{document}
This is an example of text in the default \textbf{Roman} font.
\textsf{This is an example of text in the \textbf{Sans Serif} font.}
\texttt{This is an example of text in the \textbf{Typewriter} font.}
\end{document}


You can mix and match fonts within the same document without any problems – just make sure you watch out for the correct placement of the curly brackets (some TeX editors will do this for you automatically).

TeX also supports different font sizes through a variety of commands.

To create the document, I used the following:

\documentclass{article}
\begin{document}
This text is in the {\tiny{tiny}} font size. It corresponds to a text size of roughly 7 pt.
This text is in the {\scriptsize{script size}} font size. It corresponds to a text size of roughly 8 pt.
This text is in the {\footnotesize{footnote size}} font size. It corresponds to a text size of roughly 8.5 pt.
This text is in the {\small{small}} font size. It corresponds to a text size of roughly 9.3 pt.
This text is in the {\normalsize{normal size}} font size. It corresponds to a text size of roughly 10 pt.
This text is in the {\large{large}} font size. It corresponds to a text size of roughly 12 pt.
This text is in another {\Large{large}} font size. It corresponds to a text size of roughly 14 pt.
This text is in yet another {\LARGE{large}} font size. It corresponds to a text size of roughly 16 pt.
This text is in the {\huge{huge}} font size. It corresponds to a text size of roughly 19 pt.
This text is in another {\Huge{huge}} font size. It corresponds to a text size of roughly 23 pt.
\end{document}


Note the use of a curly bracket before the \ command and the two curly brackets at the end. If you only use one set of curly brackets (like with other TeX commands), the entire sentence will be in that particular font size.

Lists

Lists are relatively easy in TeX and there are three basic types.

An itemized list uses simply bullet points for your items:

\begin{itemize}
\item The first item of your list
\item The second item of your list
\item The third item of your list
\end{itemize}


An enumerated list uses the standard sequential numbering for your individual list items:

\begin{enumerate}
\item The first item of your list
\item The second item of your list
\item The third item of your list
\end{enumerate}


A descriptive list uses specific labels for each item of your list (for example, a, b and so on). TeX will not automatically generate the numbering and each label must lie within the [ ] as in the example below:

\begin{description}
\item[a)] The first item of your list
\item[b)] The second item of your list
\item1 The third item of your list
\end{description}


When rendered, this is how the three lists turn out:

Finishing Up

And that’s it for your first TeX document! Hopefully, this tutorial has provided you with enough information to start creating simple documents in TeX that look good. Given the sheer extent of TeX, we can’t cover everything in this tutorial, but in later articles we’ll have a look at more advanced features, such as mathematical formulas and working with images (which I purposely haven’t touched upon in this tutorial as it is quite complicated).

Once again, please feel free to download and play around with the sample document for this tutorial (all the examples listed here are in it) as it’s a good way to learn how to use TeX. Yes, it takes a bit of extra time to learn but the main reason to use TeX is that you get standardized and professional-looking documents which can be used in a wide variety of scenarios.

Note: Want to add some source code? Type <pre><code> before it and </code></pre> after it. Find out more
• _tian

>> was using it when in the University of Applied Sciences here in Fribourg, for mathematics and physics lessons. it works great when it comes to mathematic formulas :) but a little bit harder to add graphics and pictures … but the rendrered document is so class.
I love Tex.