Introduction to LaTeX

(2 votes, average 5.00 out of 5)

LaTeX is a self-described document preparation system that is often used for research papers or other technical publications.  It is, in essence, an extremely useful computer language that interprets code and produces a well-formatted document.  It can greatly increase productivity due to the ease of formatting, and other nicities like automatic figure and reference numbering and easy mathematical representation.  Unfortunately, it also have a very steep learning curve.  Here, we examine how to make a basic document with LaTeX.  This article assumes a few things, most notably:

  • That you are working on a Linux operating system.  LaTeX syntax seen here, however, is exactly the same regardless of operating system.  Sections of the article dealing with building your document, however, will be different.
  • That you already have a TeX (the typeset that LaTeX is built off of) installed with LaTeX.  See here for more information regarding this.

 

Document Basics

Now that you have LaTeX, lets learn some syntax. LaTeX is a computer language, remember -- it interprets files and produces a meaningful output.  So, open up your favorite editor and type in the simple code below.  Save it as a .tex file. 

1
2
3
4
5
6
7
8
9
10
11
12
13
\documentclass[twocolumn]{article} 
 
\begin{document}
 
\title{Example title here.}
\author{John Doe}
\date{\today}
\maketitle
 
Hello world!
%Use the percent sign for comments
 
\end{document}

 

Let's point out a few things which are important about this piece of LaTeX code.

  • The article begins with \documentclass.  Every LaTeX file should begin with this.  Passed into it is the type of document you are writing -- typically it will be article, but you can also choose book, report, or letter.  Further elaboration on what each of these contains can be found here.
  • Any line (or something even any word) beginning with the \ character denotes a keyword to LaTeX.  
  • The \begin{document} command is used to denote the begin of code that should actually be typeset into the document.  Other declarations can be placed before here -- we will see an example of this later.
  • We create a title by using the \maketitle command.  This command gathers pre-configured keywords and puts them into a title -- in this case, the title, author, and date.  Without the maketitle, the title, author, and date would not have been typeset.

 

Now, open a linux command line and build this file using the pdflatex command.

pdflatex helloworld.tex

This should produce a pdf called helloworld.pdf.

 

Using Sections and Subsections

Organizing an article is clearly very important -- fortunately, LaTeX provides a very easy way to label sections and subsections, and very convenient way to refer to them.  Let's take a look at the previous code with some added text using sections and subsections.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
\documentclass[twocolumn]{article} 
 
\begin{document}
 
\title{An Example Article}
\author{John Doe}
\date{\today}
\maketitle
 
\section{My First Section}
\label{My First Section}
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque in arcu nibh. Aenean sed lorem eu ante molestie dapibus. Fusce at turpis lorem, sed tristique nisl. 
 
\subsection{Tech Repo}
\label{The Tech Repo}
Etiam semper sapien vitae odio posuere eleifend lobortis sem eleifend. Proin malesuada arcu ac nibh tincidunt euismod. 
 
\subsection{Competitors}
\label{Competitors}
Nullam nec orci pellentesque mauris gravida aliquet.  
 
\section{Importance of Jibberish}
\section{some Section Name}
In Section~\ref{The Tech Repo} we looked at some jibberish.
 
\end{document}

 

Copy this code into a text editor and build it using LaTeX.  You'll notice how sections and subsections get formatted.

Something else important to note here is the reference to a previous section through the ~\ref{labelName} command.  By labeling sections using the \label{labelName}, we can reference them later using their label by typing ~\ref{labelName}.  LaTeX will automatically enter in the number of the Section, so you don't have to worry about section numbers changing if things get rearranged. 

 

 

Adding Figures

Most technical articles provide some graphs and charts throughout the report.  LaTeX provides a reasonably easy interface for these additions, and once again allows us to very easily reference Figures like we saw for sections and subsections.  Let's look at a basic figure template stub below, and explain what each line does.

 

1
2
3
4
5
6
7
8
\begin{figure*}[t]   %[t]can be t, b, or p
\center
\includegraphics[width=0.95\textwidth]{fig.pdf}
\vspace{-0.6em}
\caption{Probability of something.}
\vspace{-1.1em}
\label{fig:probability}
\end{figure*}

 

  1. This line declares we are beginning a figure, and where on the page it should go.  The star indicates the figure should take up the entire width of the page; to restrict the figure to a column, remove the star.  The letter in the brackets indicates where on the page it should appear:  t for top, b for bottom, or p for float.
  2. Remove this line if you would like the figure to be aligned to the left.  Typically figures should be centered in their column.
  3. Specifies the location of the figure to be added.

The rest of the code is fairly self-explanatory.  Notice the label attached to this figure -- we can use this label later to reference like so:

1
In Figure~\ref{fig:probability}, we examine some probability of something.

 

 

Organizing Code

Articles can get pretty lengthy, and as you may have noticed, without formatting things can get pretty messy in the code.  In this section we will suggest a clean way to keep your article organized.

We have already seen how to use sections in our article.  Often times sections are seperate pieces of thought -- why not split them into their own files?  Suppose we have 5 sections, labeled "conclusion", "methodology", "results", "discussion", and "conclusion".  Now suppose we created 5 seperate .tex files with each section individually represented in its respective file.  We can use these files as input  to our "main" .tex file, like so:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
\documentclass[twocolumn]{article} 
 
\begin{document}
 
\title{An Example Article}
\author{John Doe}
\date{\today}
\maketitle
 
\input{conclusion}
\input{methodology}
\input{results}
\input{discussion}
\input{conclusion}
 
\end{document}

When using the \input command, LaTeX assumes the file has a .tex file extension, and essentially "copies" the code into the file (similar to the #include construct in C).  Now we can work on each section seperately and keep the organization of sections very clean.  References across sections and everything else still works.

 

Other Techniques

As you might have guessed from this tutorial, there is a lot  to do in LaTeX.  We have covered the very basics for writing articles here.  Check back soon for more articles on LaTeX, including how to write mathematical equations in LaTeX and how to write macros.

Partner Links:
Last Updated on Tuesday, 02 November 2010 21:17  
Related Articles

» How to Find a Process Using a Particular Port in Linux

If you are trying to find the process using a particular port in Linux, you can do the following: lsof -i tcp:2700 The above example will return a list of processes using the TCP port with ID 2700.Alternatively you can view all open network connections by doing the following: sudo netstat -nlp 

» How to Configure iTunes to Import in MP3 Format

iTunes is a widely used media program, but often times users become frustrated that the default encoding for ripping a new CD is Apple's proprietary format.  A more widely used format such as MP3 is more convenient.  Here is how to configure iTunes to rip in MP3:Open iTunes and navigate to Edit -> Preferences.  On Mac this is under iTunes->Preferences.On the General tab, click the "Import Settings" button.In the "Import Using" dropdown, select "MP3 Encoder".  Now...

» Make an ISO file from Folders or CDROM drive in Linux

If you want to create an ISO image of a CD/DVD or a folder and its contents for archive purposes, copies, easy mounting, burning out, and other purposes, Linux comes with two quick and easy to use command line utilities to make this happen dd and mkisofs.First you will need to open a terminal ("CTRL + ALT + T" in Ubuntu) and type in the following commands for the operation you want to accomplish. CD/DVD Drive:Type: sudo umount /dev/cdrom to unmount your CDRom drive from the...