Latex tutorial
The basics
LaTeX is designed to separate the content of a document from the layout. It lets you focus on the content while leaving the style to someone else. To get
started, we need to know that formatting in LaTeX is done with commands. Commands start with a \ and then are followed by the command name, e.g. \command. There
are lots of these and learning LaTeX is essentially about learning how to use the different commands. This will give you a VERY basic introduction to them.
Commands are case sensitive.
Your file will have the .tex extension. In this example, we'll call our file example.tex.
Remember - Google is your friend. I've been using latex for years and I still look up commands online. If you don't know how to do something, googling latex and
the thing you want to do will usually pull up tons of examples. This is a great way to learn and it's a pretty common paradigm for learning to program in any
language.
Document Structure
The basic LaTeX document has a header section and a body section. The header can be very long and complicated, as you will see when you use templates from
journals. However, the only thing it needs to contain is the \documentclass command that tells LaTeX what kind of document follows. The body of the document
needs to begin and end. Thus, your simplest LaTeX document would look something like this:
\documentclass[11pt]{article}
\begin{document}
Very basic document.
\end{document}
You can look up different basic types of documentclass that are available. You can also use customized styles for this. Those files are provided by journals,
conferences, or other organizations. UMD provides them for dissertations.
Compiling
Once you have your document, you need to compile it. This reads all your text and code and applies the layout rules to produce a PDF. Your LaTeX program will
have a compile or typeset command that will do this.
Basic Formatting
Paragraphs
In LaTeX documents, you are not editing the document as it will appear on the final page. You are essentially editing some backend source material. Thus, the
document is formatted a bit differently that you will be used to. To start, each paragrph must be separated by a blank line. If that blank line is missing, the
text will be combined into one paragraph.
Bold, Italic, Underline
- Bold - \textbf{Text here}
- Italic - \emph{Text here} or \textit
- Underline - \underline{Text here} -
Comments
In LaTeX, you can comment out part of the document. This means you keep the text in the file you are editing, but it is ignored when the document is compiled.
This is useful for giving yourself notes and for editing. You may want to remove paragraphs or sections without actually deleting the text. Comments begin with a
% and everything is ignored until the end of the line. Note that if you want to comment out a sentence in the middle of a paragraph, you should put that sentence
on its own line and put the comment in front of it. The paragraph will remain whole as long as there is no blank line. Here's an example:
President-elect Barack Obama hopes to have a massive economic stimulus plan waiting for his signature when he takes office on Jan. 20. It's expected to include
hundreds of billions in spending on infrastructure and green energy, but he also made clear last week that he wants it to also feature tax cuts to lower- and
middle-income Americans.
%Tax cuts are "part and parcel of what we need when it comes to stimulus," Obama said last week.
"We're going to be putting money in people's pockets so that they can spend on buying a new computer for their kid's school, so that they can, you know, make
sure that they are able to deal with heat and groceries and all the other strains on the family budget," he added.
Sections
To make a new section with a heading, you use \section{The Section Name}. You should not number the sections. LaTeX will automatically number the sections for
you
according to the document style you are using. Similarlly, you can create a subsection with \subsection, a sub-subsection with \subsubsection, and at any level
lower than that you usually use a paragraph, \paragraph.
\section{Example section}
blah blah
\subsection{Part of the example}
more blahs go here
\subsubsection{Smaller part of the example}
Finally, more text here.
\section{Another Example Section}
As described above, this is an example.
Labels and References
One of the more powerful things you can do with LaTeX is refer to sections. So, if you want to say "As discussed in section 1.2", you do not want to actually put
the "1.2" in there because LaTeX is labelling them and if you add another section before it, the number will change. Instead, we can label the sections, refer to
the labels, and then LaTeX will insert the correct number. If we extend our example above, note the addition of the \label command right after the sections and
the \ref command.
\section{Example section}
\label{exampleSection}
blah blah
\subsection{Part of the example}
\label{examplePart}
more blahs go here
\subsubsection{Smaller part of the example}
\label{smallPart}
Finally, more text here.
\section{Another Example Section}
As described in section \ref{exampleSection}, this is an example.
Sometimes when you add labels, you need to compile your document twice. The first time lets LaTeX build a list of all the labels. The second time lets the refs
work because now LaTeX knows what the sections are called. If you see ?? in place of the section number the first time, recompile. If you see it there after the
second time, it's likely you have a typo. Keep in mind that labels are case sensitive.
We have not created figures and tables yet, but they can be labeled and referred to in the same way. We just say "In figure \ref{figureLabelName}, you will
see..."
Figures
To insert a figure, you first need the image file. This generally needs to be a png or eps file, depending on the program you have. The example below shows a
centered image with a caption. The other step you need to take is to include the command \usepackage{graphics,graphicx} up in the header. You can put it right
after the documentclass command. That shows latex how to handle figures.
\begin{figure}
\begin{center}
\includegraphics[width=2in]{images/sampleNetwork.png}
\caption{A sample social network with trust values (on a 0-1 scale) as edge labels.}
\label{sampleNetwork}
\end{center}
\end{figure}
LaTeX will automatically place the figure in the document where it fits, close to where you put it in the text. If you want to refer to this figure in the text,
you would use \ref{sampleNetwork} referring to the label of the figure.
Tables
Tables are really a pain in LaTeX. Basic tables are done as shown below. You use the tabular command to start. That is followed by the alignment of each cell (r
for right, c for center, l for left). If you put a vertical bar between them, you will get a line separating the cells in the table. This is not required. Each
row of the table has the content of each cell separated by a &. The rows end with a \\ which is a new line command. The \hline command can be used to insert a
horizontal line between the rows.
If you are doing tables, I receommend going online and reading lots of examples. They can be tricky to do anything beyond the basics and it's one of the few
things in LaTeX that makes your life more difficult than if you did it another way.
\begin{tabular}{|c|c|c}
\label{exampleTable}
first cell & second & third \\
\hline
$\pi$ & big long line of text can go here & another third cell\\
\end{tabular}
Citations and BibTeX
The thing in LaTeX that will save you the most time is formatting citations. You will use a format called BibTeX to format your references. It is pretty easy
to figure out. Many
sources, like the ACM digital library will give you bibtex formatted references for their papers.
First, let's talk about how you do a reference in the text. Each reference gets a
label, just like the sections and images discussed above. If you want to include a citation, you use that label like this: "As discussed in \cite{bobSmithpaper},
it is well understood that...". It's just like \ref, but you use \cite. LaTeX will automatically make the citation in the right format, based on your
specification. It could be [1] or (Smith, 2006) or [Sm06]. You don't worry about that.
To get the references to appear at the end of the document, you just put two lines:
\bibliographystyle{apa}
\bibliography{example}
The \bibliographystyle command specifies which style to use. There are some built into LaTeX and your journal or conference may provide their own style. If they
do, it will come in a file called something.bst. ACM, for example, includes an acmtras.bst file. This is the bibliography style file. To use one, you put the
file name without the extension in the command. So for ACM, it would be \bibliographystyle{acmtrans}. The \bibliography command gives the name of the file with
all your references. The extension is not used. This example says they are in a file called example.bib. Some LaTeX programs require that the name of this file
matches the name of your tex file. Thus, we would have example.tex as the main document and example.bib as the reference file. It's a good idea to make them
match even if it is not required by your program, because it may be required for your co-authors.
Now, the example.bib file has your references in bibtex format. Example bibtex entries look like this:
@book{vanleunen,
title = "A Handbook for Scholars",
author = "Mary-Claire van Leunen",
publisher = "Knopf",
year = "1979"
}
@ARTICLE{Zurek:1993,
AUTHOR = {Zurek, R. W. and Martin, L. J.},
TITLE = {Interannual Variability of planet-encircling dust activity on {M}ars},
YEAR = {1993},
JOURNAL = jgr,
VOLUME = {98},
NUMBER = {E2},
PAGES = {3247--3259}
}
Basically, you start by saying what kind of document you are referencing. That is followed by the label (e.g. vanleunen). Then, you have a set of field names set
equal to their values. The values appear in quotes or curly braces, and each must be followed by a comma. You can leave off fields or include ones that aren't
needed. I've put a link below to a good web page that gives you all the different types of documents and fields. Again, if you need a format for a specific kind
of document, google will bring you tons of examples with a little searching.
To link these two together, first compile your LaTeX document. This will build a list of the references used. Then, compile your BibTeX document. It will make a
file that formats all the references that were used in the paper. Then, compile your LaTeX document again (sometimes twice). That will read the newly formatted
references, put them at the end, and include all the in-line citations.
To change the format of the citations, you simply change the \bibliographystyle command and then recompile everything.
Resources and Links
Books
Websites