LaTeX Tutorial

Please leave a remark at the bottom of each page with your useful suggestion.


LaTeX Tutorial


Introduction

  • LaTeX is a document preparation system
  • It is suited to producing long, structured documents, and is very good at type-setting equations
  • A LaTeX document is a plain text file with a .tex file extension.
  • A typesetting system designed by Donald Knuth in 1978 for high quality digital typesetting

Document Structure

The \begin{document} and \end{document} commands enclose the text and commands that make up your document. Anything typed before \begin{document} is known as the preamble, and will affect the whole document. Anything typed after \end{document} is ignored.

\documentclass[a4paper,12pt]{article}

\usepackage{amsmath}    % need for subequations
\usepackage{graphicx}   % need for figures
\usepackage{verbatim}   % useful for program listings
\usepackage{color}      % use if color is used in text
\usepackage{subfigure}  % use for side-by-side figures
\usepackage{hyperref}   % use for hypertext links, including those to external documents and URLs


\setlength{\baselineskip}{16.0pt}
\setlength{\parskip}{3pt plus 2pt}
\setlength{\parindent}{20pt}
\setlength{\oddsidemargin}{0.5cm}
\setlength{\evensidemargin}{0.5cm}
\setlength{\marginparsep}{0.75cm}
\setlength{\marginparwidth}{2.5cm}
\setlength{\marginparpush}{1.0cm}
\setlength{\textwidth}{150mm}


\begin{document}
A sentence of text.
\end{document}

Creating a Title

The \maketitle command creates a title. You need to specify the title of the document. If the date is not specified today’s date is used. Author is optional.

\documentclass[a4paper,12pt]{article}
\begin{document}

\title{My First Document}
\author{My Name}
\date{\today}
\maketitle

A sentence of text.
\end{document}

Table of Contents

Type \tableofcontents where you want the table of contents to appear in your document — often directly after the title page.

The \newpage command inserts a page break so that we can see the effect of the page numbering commands.

\documentclass[a4paper,12pt]{article}
\begin{document}

\title{My First Document}
\author{My Name}
\maketitle

\pagenumbering{roman}
\tableofcontents
\newpage
\pagenumbering{arabic}

\section{Introduction}\label{sec1}

\end{document}

Sections

You should divide your document into chapters (if needed), sections and sub- sections. The following sectioning commands are available for the article class:

  • \section{...}
  • \subsection{...}
  • \subsubsection{...}
  • \paragraph{...}
  • \subparagraph{...}
\documentclass[a4paper,12pt]{article}
\begin{document}

\title{My First Document}
\author{My Name}
\maketitle

\section{Introduction}
This is the introduction.

\section{Methods}

\subsection{Stage 1}
The first part of the methods.

\subsection{Stage 2}
The second part of the methods.

\section{Results}
Here are my results.

\end{document}

Labelling

You can label any of the sectioning commands so they can be referred to in other parts of the document. Label the section with \label{labelname}. Then type \ref{labelname} or \pageref{labelname}, when you want to refer to the section or page number of the label.

Type \label{sec1} on a new line directly below \subsection{Stage 1}.

Type Referring to section \ref{sec1} on page \pageref{sec1} in the Results section.

\documentclass[a4paper,12pt]{article}
\begin{document}

\title{My First Document}
\author{My Name}
\maketitle

\section{Introduction}\label{sec1}

Here is the introduction section text, see \ref{sec1} on page \pageref{sec1}.

\end{document}

Font Effects

There are LaTeX commands for a variety of font effects:

\textit{words in italics} 
\textsl{words slanted}
\textsc{words in smallcaps}
\textbf{words in bold}
\texttt{words in teletype}
\textsf{sans serif words}
\textrm{roman words}
\underline{underlined words}
  • words in italics
  • words slanted
  • WORDS IN SMALLCAPS
  • words in bold
  • words in teletype
  • sans serif words
  • roman words
  • underlined words

Font Sizes

There are LaTeX commands for a range of font sizes:

{\tiny tiny words} 
{\scriptsize scriptsize words}
{\footnotesize footnotesize words}
{\small small words} 
{\normalsize normalsize words}
{\large large words}
{\Large Large words}
{\LARGE LARGE words}
{\huge huge words}
  • tiny words
  • scriptsize words
  • footnotesize words
  • small words
  • normalsize words
  • large words
  • Large words
  • LARGE words
  • huge words

Lists Item

LaTeX supports two types of lists: enumerate produces numbered lists, while itemize is for bulleted lists. Each list item is defined by \item. Lists can be nested to produce sub-lists.

\begin{enumerate}
	\item First thing
	\item Second thing
\begin{itemize}
	\item A sub-thing
	\item Another sub-thing
\end{itemize}
	\item Third thing
\end{enumerate}


\begin{itemize}
	\item[-] First thing
	\item[+] Second thing
\begin{itemize}
	\item[Fish] A sub-thing
	\item[Plants] Another sub-thing
\end{itemize}
	\item[Q] Third thing
\end{itemize}
  1. First thing
  2. Second thing
    • A sub-thing
    • Another sub-thing
  3. Third thing

It is easy to change the bullet symbol using square brackets after the \item, for example, \item[-] will give a dash as the bullet. You can even use words as bullets, for example, \item[One].

Tables

The tabular command is used to typeset tables.

  • The table data follows the \begin{tabular} command:
  • & is placed between columns.
  • \\ is placed at the end of a row (to start a new one).
  • \hline inserts a horizontal line.
    • l for a column of left-aligned text (letter el, not number one).
    • r for a column of right-aligned text.
    • c for a column of centre-aligned text.
    • | for a vertical line.
  • \cline{1-2} inserts a partial horizontal line between column 1 and column 2.
  • The command \end{tabular} finishes the table.
\begin{tabular}{|l|l|}
Apples & Green \\
Strawberries & Red \\
Oranges & Orange \\
\end{tabular}

\begin{tabular}{rc}
Apples & Green \\
\hline
Strawberries & Red \\
\cline{1-1}
Oranges & Orange \\
\end{tabular}

\begin{tabular}{|r|l|}
\hline
8 & here’s \\
\cline{2-2}
86 & stuff \\
\hline \hline
2008 & now \\
\hline
\end{tabular}

Figures

This chapter describes how to insert an image in to your LaTeX document, which requires the graphicx package. Images should be PDF, PNG, JPEG or GIF files. The following code will insert an image called myimage:

\begin{figure}[h]
\centering
\includegraphics[width=1\textwidth]{myimage}
\caption{Here is my image}
\label{image-myimage}
\end{figure}

\begin{figure}[h!]
\includegraphics{ImageFilename}
\caption{My test image}
\end{figure}

Equations

Equations are written in ‘math mode’.

You can enter math mode with an opening and closing dollar sign $. This can be used to write mathematical symbols within a sentence

$ 1 + 2 = 3 $

1 + 2 = 3.

If you want a “displayed” equation on its own line use $$...$$.

$$ 1 + 2 = 3 $$

1 + 2 = 3

For a numbered displayed equation, use \begin{equation}...\end{equation}.

\begin{equation} 1 + 2 = 3 \end{equation}

1 + 2 = 3    (6.1)

The number 6 refers to the chapter number, this will only appear if you are using a document class with chapters, such as report.

Use \begin{eqnarray}...\end{eqnarray} to write equation arrays for a series of equations/inequalities.

\begin{eqnarray}
23a & = & b + c \\
& = & y - z
\end{eqnarray}

For unnumbered equations add the star symbol * after the equation or eqnarray command (i.e. use {equation*} or {eqnarray*}).


Mathematical Symbols

Basic mathematical symbols (+ - = ! / ( ) [ ] :) can be accessed directly from the keyboard

Powers & Indices

  • 'Powers' are inserted using the hat ^ symbol, e.g., $n^2$.
  • 'Indices' are inserted using an underscore _ symbol, e.g. $2_a$.
  • If the power or index includes more than one character, group them using curly brackets {...}, e.g. $b_{a-2}$

Fractions

Fractions are inserted using \frac{numerator}{denominator}.

$$\frac{a}{3}$$ 

Fractions can be nested —
$$\frac{y}{\frac{3}{x}+b}$$ 

Roots

Square root symbols are inserted using \sqrt{...} e.g. $$\sqrt{y^2}$$ and $$\sqrt[x]{y^2}$$.

Sums and Integrals

The command \sum inserts a sum symbol; \int inserts an integral. The upper limit is specified by a hat ˆ and the lower by an underscore _.

$$\sum_{x=1}^5 y^z$$

$$\int_a^b f(x)$$

Greek letters

Greek letters can be typed in math mode using the name of the letter preceded by a backslash \.

$\alpha$ 		= α
$\beta$ 		= β
$\delta, \Delta$ 	= δ, Δ
$\theta, \Theta$ 	= θ, Θ
$\mu$ 			= µ
$\pi, \Pi$ 		= π, Π
$\sigma, \Sigma$ 	= σ, Σ
$\phi, \Phi$ 		= φ, Φ
$\psi, \Psi$ 		= ψ, Ψ
$\omega, \Omega$ 	= ω

Inserting References

To create a bibliography entry the command \bibitem is used. A parameter inside braces is set to label this entry and can later be used as identifier for this reference.

\documentclass[a4paper,10pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}

\usepackage[nottoc]{tocbibind}

\begin{document}

\tableofcontents

\section{First Section}
This document ...

\bibliographystyle{unsrt}
\bibliography{sample}

\end{document}

LaTeX includes features that allow you to easily cite references and create bibliographies in your document. BibTeX file contains all the references you want to cite in your document. It has the file extension .bib.

@article{Birdetal2001,
 Author = {Bird, R. B. and Smith, E. A. and Bird, D. W.},
 Title = {The hunting handicap: costly signaling in human foraging strategies},
 Journal = {Behavioral Ecology and Sociobiology},
 Volume = {50},
 Pages = {9-19},
 Year = {2001} }

Each reference starts with the reference type (@article in the example above). Other reference types include @book, @incollection for a chapter in an edited book and @inproceedings for papers.

Type the following where you want the bibliography to appear in your document (usually at the end): Where references is the name of your .bib file.

\bibliographystyle{plain}
\bibliography{Doc1}

Type \cite{citationkey} where you want to cite a reference in your .tex document. If you don’t want an in text citation, but still want the reference to appear in the bibliography, use \nocite{citationkey}.

To include a page number in your in-text citation put it in square brackets before the citation key: \cite[p. 215]{citationkey}.

To cite multiple references include all the citation keys within the curly brackets separated by commas: \cite{citation01,citation02,citation03}.

Use the natbib package if you want to include author-date citations. Natbib uses the command \citep{...} for a citation in brackets (e.g. [Koppe, 2010]) and \citet{...} for a citation where only the year is in brackets (e.g. Koppe [2010]).

\begin{thebibliography}{9}

\bibitem{latex}
Helmut Kopka and Patrick W. Daly, \textit{A Guide to
\LaTeX: Document Preparation for Beginners and Advanced Users},
fourth edition, Addison-Wesley (2004).

\end{thebibliography}



Write Your Comments or Suggestion...