Getting started with LaTeX using TeXShop

Here is a quick “tutorial” for getting started with LaTeX. For the purposes of this tutorial, I’m going to assume that you’re using TeXShop in one of our computer labs.

Getting started

To start, open TeXShop. You should see a blank window, which looks like this:
Blank

If you don’t see a blank window, then select “New” from the “File” menu.

Now, insert the following code in to the document:

\documentclass[11pt]{article}
\usepackage[margin=1in]{geometry}          
\usepackage{graphicx}
\usepackage{amsthm, amsmath, amssymb}
\usepackage{setspace}\onehalfspacing
\usepackage[loose,nice]{units} %replace "nice" by "ugly" for units in upright fractions

\title{Title}
\author{Great Author}
\date{Spring 2015}

\begin{document}
\maketitle

\end{document}

In TeXShop, the code will be color coded. Save the file somewhere and then click the “Typeset” button (upper left). Two more windows will appear: First, a “console” window, showing the processing of the code. Second, a pdf file will appear, looking something like this:
FirstPDF

In the code, replace the title with another title, and the “Great Author” with your actual name. Be careful not to delete any braces or the like! Typeset again.

Now let’s add some content: Adjust your code to include some words below the \maketitle line. For example,

\documentclass[11pt]{article}
\usepackage[margin=1in]{geometry}          
\usepackage{graphicx}
\usepackage{amsthm, amsmath, amssymb}
\usepackage{setspace}\onehalfspacing
\usepackage[loose,nice]{units} %replace "nice" by "ugly" for units in upright fractions

\title{Title}
\author{Great Author}
\date{Spring 2015}

\begin{document}
\maketitle

I like to ride my bicycle, but only when it is sunny and warm. Does this make me a ``fair weather rider''? If so, so be it.

\end{document}  

Notice the different quotes I used: the left ones are located next to the “1” key. Typeset and see what you get.

Add in some weird spaces and line breaks, so the middle of the code looks (perhaps) like this:

\begin{document}
\maketitle

I like to ride    my     bicycle, but only when it is sunny and warm. 
Does this make me a ``fair weather rider''?      If so, so be it.

\end{document}

What happened? Here is an important lesson: LaTeX takes care of most of the formatting for us! There is an important corollary: Let LaTeX take care of the formatting! That is, do not try to “manually” over-ride the formatting.

In order to get a paragraph break in LaTeX, leave a blank line. Try it out! Perhaps the middle of your document looks like this:

\begin{document}
\maketitle

I like to ride my bicycle, but only when it is sunny and warm. 
Does this make me a ``fair weather rider''?
If so, so be it.

Each morning, I drink a cup of coffee and eat a piece of bread.
Quite a bit of bread is consumed in my household.
Fortunately, I have learned to make bread -- two or three times per week, I bake bread!

\end{document}

Notice my habit of writing one sentence per line. You don’t have to do this, but I find it helpful.

Math

Now we want to include some math. Let’s delete all the silly text we’ve written and replace it with some text. (Be sure to leave the top part of the code alone!) Try this out:

\documentclass[11pt]{article}
\usepackage[margin=1in]{geometry}          
\usepackage{graphicx}
\usepackage{amsthm, amsmath, amssymb}
\usepackage{setspace}\onehalfspacing
\usepackage[loose,nice]{units} %replace "nice" by "ugly" for units in upright fractions

\title{Getting started}
\author{Paul}
\date{Spring 2015}

\begin{document}
\maketitle

One of my favorite functions is the cosine function $f(x) = \cos{x}$.
The Taylor series, centered at $x=0$, for cosine is
\begin{equation}
\cos{x} = 1 - \frac{1}{2} x^2 + \frac{1}{4!} x^4 - \dots
\end{equation}

\end{document}

Typeset this and note the following:

  • Math which is part of the text goes between dollar signs.
  • Math which is “displayed” goes inside the “equation” environment, which automatically generates an equation number.
  • The cosine function take special code.

All math should either be between dollar signs (when in-line) or inside some sort of an equation enviroment.

Here is some more code with math that you should try out:

  • Can you plot $e^{2x}$?
  • Exercise: Compute $\frac{d}{dt}[e^{t^2}]$.
  • I like big parentheses: $\frac12 \left( \frac{1}{1+\frac{1}{x}}\right)$.
  • Everyone wants to compute $\int_{0}^{12} (x-3x^2) dx$.
  • Everyone wants to compute $$\int_{0}^{12} (x-3x^2) dx.$$

What happened with that last bit of code? It gives us a way to display equations without the equation number. Personally, I think it is better to have all equations numbered, but I want to give you the option…

Errors

LaTeX (and computers in general) are rather sensitive when it comes to syntax. Do not confuse a curly brace with a round brace, do not confuse forward slash with backslash, etc.

However, being human, eventually you’ll make an error and LaTeX will grind to a halt. What happens? Try typesetting the following code, which contains an error:

\documentclass[11pt]{article}
\usepackage[margin=1in]{geometry}          
\usepackage{graphicx}
\usepackage{amsthm, amsmath, amssymb}
\usepackage{setspace}\onehalfspacing
\usepackage[loose,nice]{units} %replace "nice" by "ugly" for units in upright fractions

\title{Getting started}
\author{Paul}
\date{Spring 2015}

\begin{document}
\maketitle

One of my favorite functions is the cosine function $f(x) = \cos{x}.
The Taylor series, centered at $x=0$, for cosine is
\begin{equation}
\cos{x} = 1 - \frac{1}{2} x^2 + \frac{1}{4!} x^4 - \dots
\end{equation}

\end{document} 

Rather than get a nice pdf, you should get this:
Error1

What happened? If you look closely at the error message you’ll see the following:

YourFileName.tex:17 LaTeX Error: Bad math environment delimiter.

LaTeX is telling you that it made it as far as line 17 of your code, at which point it found an error. The problem is that the error isn’t actually in line 17, it is in line 15, where there is a missing dollar sign after the cosine. What LaTeX was doing was alternating in and out of math mode exactly opposite as we intended, until (according to LaTeX) we tried to start an equation while already in math mode.

What lesson should we draw from this? It can be helpful to work backwards when looking for an error. Start where LaTeX thinks the error is, then move up in your code until you find what’s actually wrong.

It can be helpful to “comment out” sections of code when looking for an error — any line which starts with % is ignored by LaTeX. So you can start narrowing down the possible region where the error is by putting percent signs in front of various bits of code until you find the problem.

Fancier math

Let’s do something a bit fancier. Put the following code inside your document and typeset twice:

The logistic equation is
\begin{equation}
\label{logistic}
\frac{dP}{dt} = rP\left( 1-\frac{P}{K}\right).
\end{equation}
We want to add a second equation to \eqref{logistic} in order to account for predators.

What happened? Not only did you get an equation number, but the text refers to the equation number. Note that this reference will remain correct, even if you add another equation above (thus changing the numbering). Try it! You’ll need to typeset a few times.

Let’s keep going:

The logistic equation is
\begin{equation}
\label{logistic}
\frac{dP}{dt} = rP\left( 1-\frac{P}{K}\right).
\end{equation}
We want to add a second equation to \eqref{logistic} in order to account for predators.
We can do this by introducing the following system
\begin{align}
\frac{dP}{dt} &= rP\left( 1-\frac{P}{K}\right) - \alpha PQ
\\
\frac{dQ}{dt} &= -s Q + \beta PQ.
\end{align}
Here $\alpha$ and $\beta$ are constants that describe the strength of the interactions between the predators $Q$ and the prey $P$.

The last pair of equations does not sit inside an “equation” environment, rather it sits inside an “aligned” environment. The “&” symbols tell LaTeX where to do the aligning.

One other comment: LaTeX doesn’t like blank lines inside displayed equations. You’ll get an error if you try that.

Fancy text

There are four types of fancy text I want to discuss: lists, sections, graphics, and bibliographies.

Numbered lists can be made with the following code:

\begin{enumerate}
\item One thing\dots
\item \dots leads to another.
\end{enumerate}

If you want to have an un-numbered list with bullets, replace both instances of enumerate with itemize.

Sections (and subsections) can be generated like this:

\section{The next big thing}
In this section, we discuss the next big thing\dots

In order to include graphics in to your LaTeX document, you should save the image file in the same folder as your LaTeX code. This means that you need to keep track of your image files when using machines in the computer lab!

Suppose, for example, you wanted to include this image in your file: SampleReport-Graphic.pdf After saving it in the same folder as your LaTeX code, you could use the following code:

\includegraphics{samplereport-graphic}

However, I much prefer that graphics be encapsulated in a figure. Thus I would rather you use code like the following:

\begin{figure}[h]
   \centering
   \includegraphics[width=3in]{SampleReport-Graphic.pdf} 
   \caption{The caption of a figure should be a complete sentence.}
   \label{SampleFigure}
\end{figure}

This allows you to refer to the figure. Please see the template for reports for an example.

That same template includes an example of a bibliography.

What’s next

This is only the beginning. Take a look at the template for reports for more code to play with. Also look at the various links on my resources page. Ask around if you need advice. Finally, have fun!

Advertisements
This entry was posted in LaTeX. Bookmark the permalink.

Leave a comment here

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s