An Introduction to LaTeX

What is LaTeX?

LaTeX is a free, open source, document preparation system. It lets users concentrate on the content and the logical structure of a document rather than the nitty-gritty details of every item.

How do I get it?

Windows

MikTeX is a free option for Windows users. If you would like, you can also try using TeX Live (mentioned below), but I have personally found MikTeX to be easier on Windows.

Mac OSX/Linux

TeX Live is the de-facto standard for Linux, Mac OSX, and other Unix-type systems. For Mac, download TeX Live from the TeX Live link. For Linux, an easier method may be searching your repositories for texlive related packages.

How do I start using it?

Basics

LaTeX is a text-driven post-processor. What does that mouthful mean? It means you must give it a plain-text file with little commands which it then interprets. It figures out all of the things you told it to do, does it, and creates an output file - either a DVI file or a PDF file. LaTeX commands are always started with a backslash ("\"). Arguments to those commands are given with curly braces ("{"/"}") or square brackets ("["/"]").

The Editor

You can technically use any plain text editor to write a LaTeX document. However, certain LaTeX-specific editors make it really easy to compile the LaTeX document and even help you with writing it. I recommend WinEdt on Windows, TeXShop (included with TeX Live download) on Mac OSX, and gedit/kedit/emacs on Linux.

Your first LaTeX document

Open up the recommended editor for your operating system. Copy and paste the following stuff into the empty text file:

Source Result
My first \( \LaTeX \) document!

Save this as test.tex. Then, find the button that says something like LaTeX in your editor. You may have to change the compiler from TeX to LaTeX in some editors. The editor may or may not automatically display the generated document. If it doesn't, try looking for a button called Preview or something similar. Now let me explain each part of the sample document. \documentclass{article} means that LaTeX will use the standard class called article. There are also classes called book, report, letter, etc., but we will concern ourselves only with article, as it is the one most people use. \begin{document} and \end{document} must surround everything that you want typeset. This is because there are special things that can go in the preamble , the part before the \begin{document} . \LaTeX is a command that tells LaTeX to write the logo of LaTeX. That's it! Every document must have a \documentclass statement and a \begin{document} and \end{document} .

Terminology

Tips and Tricks

Here, I will provide snippets of LaTeX documents which illustrate the usage of particular environments. They should be put in the body of a LaTeX document, which means they should go between the \begin{document} and the \end{document} .

Equations

Part of the reason LaTeX is awesome is that it is extremely awesome at typesetting mathematical expressions. I highly recommend you use the amsmath package. The amsmath package is one such package which adds a couple of new environments which are better than the stock LaTeX ones. Use the equation environment to typeset equations. Here's an example:

Source Result
\begin{equation} y = x^2 \end{equation}

Obviously, there are more complicated examples, like this one:

Source Result
\begin{equation} e^{\pi i} + 1 = 0 \end{equation}

You can get Greek letters by prefixing the name of the letter with a backslash. For example, π is \pi . To get uppercase letters, simply capitalize the first letter of the name. For example, Π is \Pi .

Equation arrays

Now the equation environment is fine for individual equations scattered throughout a document, but what if you want a series of equations displayed nicely in a little array? That's what the align environment is for. Now some people may tell you to use the eqnarray environment, but eqnarray is obsolete and align is much better. Here's a simple example proving the quadratic formula:

Source Result
\begin{align} ax^2 + bx + c &= 0\\ x^2 + \frac{b}{a}x + \frac{c}{a} &= 0\\ x^2 + \frac{b}{a}x &= -\frac{c}{a}\\ x^2 + \frac{b}{a}x + \frac{b^2}{4a^2} &= -\frac{c}{a} + \frac{b^2}{4a^2}\\ \left(x + \frac{b}{2a}\right)^2 &= \frac{b^2 - 4ac}{4a^2}\\ x + \frac{b}{2a} &= \frac{\sqrt{b^2 - 4ac}}{2a}\\ x &= \frac{-b + \sqrt{b^2 - 4ac}}{2a} \end{align}

I've used a couple of new items here. The & is called an alignment operator . It tells LaTeX where to align the equations. We will also use this in tables (coming up next). The \frac command tells LaTeX to format the two following arguments (enclosed in { } ) as a fraction. The \sqrt command tells LaTeX that the argument should be placed inside a square root symbol. The ^ symbol tells LaTeX to put the argument in a superscript. Similarly, a _ can be used to tell LaTeX to put the argument in a subscript. Quick note: If the argument for a superscript or subscript is more than one character, be sure to surround it with {} . If you are not sure whether to use the curly braces or not, use them - they can't hurt!

Tables

Tables are one of the only annoying things to learn in LaTeX. They can be confusing at first, but with practice, you can make professional tables. Here's a simple example:

Source Result
\begin{array}{l l} 1&2 \\ 3&4 \\ \end{array}

There are a couple of things to explain here. First, the tabular environment takes one argument - a space-delimited list of columns and alignments. For example, I chose left and left for my two columns. You could also choose center or right in terms of basic formatting (I will explain more options in the next section). The & is used in this case to separate columns. The \\ is used to separate rows. But what if you want borders? Here's an example with borders:

Source Result
\begin{array}{|l|l|} \hline 1&2\\ \hline 3&4\\ \hline \end{array}

The command \hline simply produces a horizontal line, which we then use to our own purposes. Also notice that I added | between the columns to generate the column dividers. You can similarly add double borders by adding || instead of | or by adding two \hline commands in a row. And now we come to the issue of text wrapping in tables.

Advanced Tables

Now text wrapping requires a whole different column alignment specifier. Here's an example:

The p{5cm} tells LaTeX that you want the column to be 5 centimeters in width. This is a good time to talk about units of measurement in LaTeX. LaTeX can use pt, mm, cm, in, ex, and em, among others. pt is about 1/72.27 inches and is the standard unit of measurement. ex is about the height of a lowercase x in the current font, and em is about the width of an uppercase M in the current font. I only ever use cm or in, but it's up to you. If you want even more units of measurement, head over to the Useful Measurement Macros page on the LaTeX wiki. The other column specifiers are m{ width } which aligns the text vertically in the middle, and b{ width } , which aligns text at the bottom of the row.

However, let's now try to do multi-page tables. You'll find that it doesn't work very well - the table will run off the page and won't continue onto the next one. The package longtable will come to the rescue here. Here's a simple table running onto three pages.

Again, remember to type \usepackage{longtable} in your preamble. Otherwise, this will not work! There are many other things you can do with tables, like having one column span multiple rows and having one row span multiple columns, but they are outside the scope of this document.

Lists (numbered and unordered)

Lists are quite easy to make in LaTeX. Use the itemize environment for unordered lists and enumerate for ordered lists. Here is an example of each.

Unordered List

Ordered List

You can have up to four levels of bullets/enumerations.

Other languages

Let's say you are doing your Spanish homework with LaTeX. There's a problem. How are you going to get the accents and everything? There's an easy solution (given below).

As you can see, upside down question marks are made by appending a backtick (`) to the regular question mark. The tilde on top of the n is made by putting a backslash before the tilde. Accents are made by preponing an apostrophe with a backslash, followed by the letter which you want the accent to be on. Again, you can find more information about which accents are supported by default in LaTeX here.

Images

Now let's say you have this really nice report with tables, lists, and all, but you now want to add images. How would you do that? Well, it's actually quite easy. First, you will need to use the graphicx package to enable the following command. Next, download a random image (or find one on your computer) to use for the demo. Then, copy this code. Be sure to replace the image_name in the code with the name of your image . Also, the LaTeX file will need to be in the same directory as the image.

A couple of notes. First of all, if you are creating a DVI file, you can only use eps files . If you are creating a PDF file, you can only use formats other than eps files . Please keep this in mind when creating your LaTeX files. Second of all, notice that you don't need to include the extension of the image file - LaTeX will automatically detect which one you want to use. The easiest way to make the LaTeX file compilable with both latex and pdflatex is to have both an eps version and a jpg version of your image. Then, LaTeX will automatically use the eps file and PDFLaTeX will automatically use the jpg file. There are many more options, such as rotation and scaling. Attributes that you can set include:

Advanced LaTeX usage - typesetting Carnatic (South Indian classical) music with LaTeX (in Kannada)

(Note: I am working on rewriting this section, since KannadaLaTeX seems to be dead and there are better options now, including XeLaTeX.) First, you will need to download KannadaLaTeX. If you would like to typeset the music in other languages, you will have to search for the appropriate LaTeX packages. Next, use this template to get you started (this template will only work for KanndaLaTeX - you will need to modify it to suit your needs if you are doing this in another language):

Replace "*" with a number of columns equaling the number of beats in the taala + the number of vertical bars that are usually drawn for that taala. For example, the sequence for Vilamba Adi taala would be "c c c c c c c c c c c c c c c c c c c c" - one "c" for every beat (16), one "c" for every spacer (to separate the notation into 4 groups of 4) (there are 4). Basically, the sequence is "beat beat beat beat beat beat beat beat line beat beat beat beat line beat beat beat beat". This template is set up for a varna, but you can adapt it to any purpose - kriti, thillana, etc. When you are filling in each column, you can just type two ampersands in a row to create an empty column (it will act as a separator). For reference, I have uploaded the source for the Abhogi varna "Evari bodha" and the rendered result.

Source

Result