# Let’s Learn LaTex: Part 1

Few days ago Travis gave me an idea for a new series of posts. He mentioned recommending my blog to a friend who was trying to learn LaTex. Unfortunately, my blog is not the best learning resource. It is more like a collection of “hey, look what I found out today” type posts. Some of which have proven to be quite useful to the inhabitants of the interwebs. For example my post on the quirky way in which LaTex processes figure numbers still gets several thank-you comments per month, despite being over 3 years old.

I tend to bring up LaTex in random conversation on this blog all the time. In most cases I assume most of my readers know what the hell am I talking about. But this is often not the case. In fact, I can probably safely guess that majority of my regulars have never really used my favorite typesetting tool. So I figured that I might as well sit down and write a few introductory LaTex posts. Perhaps I can teach bunch of you something new. If not, it may still be an interesting exercise – and perhaps it will bring in some Google juice as well via LaTex related queries.

First off I should probably tell you why you should use LaTex instead of Microsoft Word or other word processing tool. Only I already did that. So I will just refer you to my earlier post in which I illustrate how LaTex is superior to office.

Next I should probably tell you how to install and configure the damn thing. Only I already did that too. Here is one of my posts that shows you how to install a full LaTex suite on windows. The ProTex package I linked to in the article contains Texnic Center – a decent, easy to use IDE that makes compiling tex files into PDF just a matter of pressing a button. I’m fairly confident that since you are reading this blog, you can probably locate the right icon on the toolbar by yourself. So I will not be showing you how to actually run LaTex commands from the CLI or how to use the IDE. I will jump right into teaching you the language itself.

LaTex basically works like a markup language. I always compare it to HTML because it is what most people are familiar with. Just like in HTML the text you type out will be rendered as a wall of text. LaTex will ignore most white space, save for spaces that separate words and newlines that tell it to start a new paragraph. Any other formatting or structural organization must be added to the file using specially formatted commands. HTML uses the greater and less then characters as escape sequences. Anything enclosed in them becomes a HTML tag and is not rendered, but rather interpreted as a rendering instruction. In latex our escape character is backslash (“\”). Any word prefixed with this character will be interpreted as a LaTex formatting command. For example:

\newpage

LaTex command can take attributes, which are usually enclosed in curly braces {} like this:

\textbf{this is an attribute}

Om case you were wondering, the “bf” in textbf stands for “bold face”. The command above therefore can be used to make your text bold.

Optional arguments are enclosed in square brackets []. These are usually switches and toggles that modify the commands, while the mandatory argument in curly braces is usually some sort of input that will be processed by the command (but not always). If there are several optional arguments they are written as a comma separated list. The order usually does not matter as LaTex is smart enough to figure to match them up. Here is another example:

\documentclass[letterpaper, 10pt]{article}

First I’d like to show you how a LaTex environment looks like. Environments are a bit like HTML tags – they come in pairs. First command opens the environment, and the other one closes it. Anything you type inside an environment is subject to some special formatting rules. The simplest environment you have to learn is of course:

\begin{document} This is where you type your document. \end{document}

The document environment is essentially the equivalent of the HTML body tag. It denotes where your document starts and where it ends. Anything above it is the preamble – which works a bit like the head section of a HTML document. That’s where you put settings, meta-information, credits, comments and etc.

Speaking of comments – before I forget I should tell you that LaTex uses percent sign % to denote a comment. If you need to use the % sign in your text you will have to escape it with backslash like this:

To escape \% use \\ % and this is a comment

You escape the backslash the same way – with a backslash. There are no multi-line comments – you just have to deal with it.

That’s pretty much it. 99% of time you will not need to use anything beyond these basic constructs. From this point on, it is just a matter of learning bunch of basic commands. The beauty of LaTex is that most of the aesthetic stuff is already preconfigured for you. Unlike a basic HTML page, a basic LaTex document looks good “out of the box” so most of the time you don’t have to do anything beyond some basic tweaking of the standard template.

Similarly to HTML, LaTex tries to separate content from presentation. The presentation is usually controlled by document class files (.cls) which are a little bit like CSS (but don’t look too much into that parallel – it’s a whole different ballgame). These files describe how the text is supposed to flow on the page, default margin settings, how and where the page numbers are displayed, whether or not the pager should be printed as if it was to be bounded in a book and etc. You can override most of these settings via simple latex commands in your document of course. But if you don’t, then changing from one style to the other becomes just a matter of changing a single line in the preamble.

Wen you start an new document, the first thing you need to do is to tell LaTex what document class you want to use. That’s where that line I skipped comes into play:

\documentclass[letterpaper, 10pt]{article}

Here I’m telling LaTex that I want to use article as my class document class. Document classes are basically files with *.cls extension which usually somewhere in the LaTex file hierarchy (along with all the libraries and what not) or in the current folder where you are compiling your document. The \documentclass command takes in several optional arguments. These arguments depend on the class you choose, but most of them relate to default font size (here 10 points), default paper size, page orientation, number of columns and etc. You can usually find the list of those in the documentation for your class file. The standard article class included with your installation accepts the following arguments:

Default Argument Acceptable Arguments
Font Size 10pt 11pt, 12pt
Paper Size letterpaper a4paper, a5paper, b5paper, executivepaper, legalpaper
Column Layout onecolumn twocolumns
Single/Double Sided oneside twoside
Landscape Mode landscape
Draft/Debug Mode draft

Your standard installation comes with 4 basic classes: article, report, book and letter. The optional arguments listed above will actually work in the first three. Letter document class is a little bit different but we’ll cover that later.

As you may imagine, each is geared towards different type of document. Article is for writing short articles destined to be published standalone or in a magazine or journal of some sort. Report is for longer scientific documents that need to be broken down into chapters. Book formats your document in preparation for binding, with alternate styles for even and odd pages, distinctive chapter pages and etc. Letter format on the other hand conforms to the customary rules for official letter writing. If you need something more specific you can usually obtain an appropriate document class file online.

Foe example, if you need to publish your paper in IEEE standard you can grab their class file from their website, put it in the same directory as your document and just use it in your preamble. That said, downloading document classes is not something you are going to be doing very often. The basic classes pretty should cover 90% of the documents you will be generating.

So let’s put all this information together and create a simple hello world file:

\documentclass[10pt, letterpaper]{article} \begin{document} Hello World \end{document}

Once you type all of that in, simply hit the big PDFLatex button in your IDE (most of them have one) to generate a nice PDF file. It should look something like this:

Hello World Document

That’s it folks. That’s how you start a LaTex document. What? You thought it would be hard? No it is not. It’s easy! Pick a document class, start a document, type your stuff, end document. That’s all! Of course this is merely the tip of the iceberg. Next time I will show you more useful commands and environments, as well as introduce you to packages.

Let’s Learn LaTex
<< Prev Next >>
This entry was posted in programming and tagged , . Bookmark the permalink.

### 5 Responses to Let’s Learn LaTex: Part 1

1. mcai8sh4 says:

@Luke – I started learning LaTeX purely after reading some of your earlier posts. I found the initial learning curve quite steep (just tried diving in), but eventually with the help of our mate Google I got the job done. Great idea to post an introduction to LaTeX (cheers Travis). I’ll look forward to the follow-up posts.

Be careful though – or else you may now get a load of messages asking you to ‘Do a post on XYZ…’ but I’m sure your always up for a sane techie challenge :)

2. sid says:

Thank you. I learnt LaTex to write my thesis. Though the learning experience was steep, I throughly enjoyed it and now I am happy I did that. Continue your posts with more lessons on LaTex.

3. gogi-goji says:

Great quick start. I learned LaTeX through the book LaTeX: A Document Preparation System by Leslie Lamport. It’s a really good book that details essentially everything you would every need to do with LaTeX, and is a great reference book. It was also great for just learning the ropes.

4. Man I really need to follow your blog more closely, since my life has become hectic I don’t check out anyones blogs. However, over half a year later I find this post… whats funny is that you posted it on my birthday. :) So I got a shoutout from my favourite blogger on my birthday… pretty cool!