tl;dr? Look here: preamble, example 1, example 2 and example1.pdf and example2.pdf

## A Quick and Dirty Intro to LaTex for Computer Science Students

Why use LaTex? There are arguments on both the pedagogical side and the useful side. The pedagogical argument that I was given is that it takes you so much longer to nicely typeset a homework assignment in LaTex than to handwrite it that you will discover the bugs in your proof by being forced to think about it. I have actually found this argument to be true.

The useful argument is based on the power and versatility of the LaTex as a typesetting language. When used correctly, it produces beautiful documents - I've used it for my resume, graduate school and fellowship applications, paper submissions, and basically anything where I wanted to appear professional. LaTex is the standard for paper submission in every field of Computer Science (and Math!), so if you are contemplating graduate school in any area, learning LaTex is useful. I can't speak to how often LaTex is used in industry, but I can't help but think that it doesn't hurt to spend an hour figuring LaTex out, just in case.

The first step to LaTex is getting an environment set up on your machine. There are numerous ways to do this so I'm only going to mention free ways.

- Windows
A super easy Windows approach is to install TeXnicCenter with MikTeX as the backend. There are numerous other editors like WinEdt but TeXnicCenter is free.
- Mac
My Mac friends swear by TeXShop. Again, you've got numerous options. Lately, I've heard nice things about Sublime Text 2 as an all-around editor.
- Linux
My main system is Ubuntu. I have the full texlive package installed, and then use gedit for document editing. I compile documents on the command line via pdflatex and use evince as the pdf viewer. I swear this is easy.
- Online Compilers
There are also a number of online, Google Docs like systems for creating LaTex documents. LaTex Labs is one and ScribTex is another. There are many more. I can't vouch for what packages these systems have installed or how well they will work, but you could give it a shot.

From here, you just need to write up a document and compile it to look at the final pdf, postscript or dvi document. The LaTex compiler is not very forgiving, but you probably already know to put compiler errors into a search engine when you can't clear them.

### Using LaTex

LaTex is an extremely customizable markup language. The basic mechanics are based on environments and macros. Without getting into any details about creating your own templates or macros, let me suggest that a search engine is your biggest friend. You want to know how to do tables? Adjust space around images? Crop images? A nice resume template? Just search and there will be numerous sources. That being said, a really useful website is Detexify for when you don't know what the name of the symbol you are looking for it called.

### Document Setup

A basic LaTex document always starts with:

` \documentclass{article}`

\usepackage{}

\begin{document}

content

\end{document}

This can be highly customized by use of a style sheet, and I would encourage you to look at examples to figure out how to use style sheets. Given the generality of LaTex, it is usually the case that looking at code snippets will help the most.

I left the \usepackage{} macro empty. This is the standard way of adding extra macros to your document. A couple of really basic useful packages for computer science are graphicx,amssymb, amsmath, amsthm,fullpage, algorithm, algorithmic, times,helvet, courier. These add the ability to add images, mathematical symbols, theorem and proof environments, algorithms, and fonts.

### Math Mode

Given that LaTex's strength is as a mathematical typesetter, it is important to discuss math mode. There are many ways to tell LaTex that you're about to start typing some equations. The first is to encase the equation in $'s, as in $x^2$. This allows you to do inline equations. The second is to use the equation environment: \begin{equation}x^2\end{equation}. The third is to use \[ x^2 \]. The final two are used for equations that you'd like to lay out centered on their own lines. If you have multiple equations you would like laid out in an aligned fashion, you might want to use the eqnarray environment.

There are many useful macros defined for math mode. To go over a couple useful ones quickly, there are superscripts and subscripts (as in $x_1$ and $x^2$), all of the greek characters (\alpha, \beta, \Alpha, \Beta etc), fractions (\frac{x}{y} renders x/y), operators (\sum, \prod) and functions (\log, \max etc). There are far too many to name, so you might want to glance over a symbol cheat sheet like this or use Detexify.

For CS 174, I have made the following style sheet available. It's a very generic homework style sheet with a number of extra math macros defined (check out the part of the preamble about \newcommand{} to see a custom macro). See Discussion 1 and Discussion 2 for examples of the source code, with Discussion1.pdf and Discussion2.pdf as compiled examples.

How do you use this preamble? I've already mentioned math mode, but it's also important to discuss how to make special characters, like $, render as text. \'s are used to indicate to escape special characters. You used also know that many of the macros are actually functions, so you can give them input. Anything in curly braces is, in a sense, required, while parameters given in square brackets [] are optional. An example is \documentclass[letter]{article} vs \documentclass{article}. You're about ready to write your first LaTex document now.

The key features of the examples to note are the use of the preamble itself:

`\input{174preamble}`

This just includes the code from the preamble file. Note this isn't how stylesheets normally work.

`\homework{1 Solutions}{Elchanan Mossel}{Due: Sept 7, 2011}{Isabelle Stanton}`

The preamble supports lecture notes (\lecture), homework submissions (\homework) and recitation notes (\recitation). Each of these takes 4 inputs, as you see in the example line.

Given that you'll probably be using this for homework assignments, the next environment to be aware of is \begin{problem}... \end{problem}. This environment will automatically number itself as you use it. If this bothers you, you're welcome to redefine it. For subparts of the problem, you may want to use either the \begin{subproblem} environment, or the item environments (\begin{itemize} \item .. \item .. \end{itemize} or \begin{enumerate}\item .. \item .. \end{enumerate}). As you enter your solutions, you should be aware of both the \begin{proof} and \begin{solution} environments. Also included are: proof-sketch, proof-idea, proof-of-lemma, proof-attempt, proofof. You should refer to the preamble file to see what inputs these require and how to use them.

Finally, since you are in a probability based class, the \Pr{} and \E{} commands will be extremely useful. These will give you nice formatting for probabilities and expectations, i.e. try to compile $\Pr{X}= \frac 1 n$. If you are in math mode ($$) and would like to enter regular text, use \mbox{ } within math mode (message box) to escape mathmode. Also, \frac{}{} is how to do fractions in LaTex. If you want to escape curly braces (as in defining a set), that would be something like $\{1,2\ldots,n\}$.

You'd probably like some extra formatting help. Here is how to insert tables. Images are notoriously difficult in LaTex, so here is a tutorial on that. Usually, pdf and ps images are the easiest to handle, but you might find a package other than graphicx that will do jpg or gif or whatnot. I would encourage you to scan through the preamble file to see what other environments have been provided for you. This should also give you enough of a template to define your own (using \newcommand{}) if you'd like more.

This is just supposed to be a quick guide to get you started. Getting started is easy, but mastering LaTex takes years. If you are interested, you should absolutely read a more thorough tutorial, like Getting Started with LaTex or any of the other hundred tutorials that comes up as a result of searching for 'latex tutorial'.

My favorite resume template is moderncv. Recruiters like it!