Flumes short guide to LaTeX

Flumes Very Short Introduction to LATEX
Peter Nordin
February 4, 2016
Contents
1 What is LaTeX
1.1 Explanation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.2 Using LaTeX With a Version Control System . . . . . . . . . . . . . . . . . .
1.3 Commands and Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1
1
1
1
2 What do You Need
2.1 If You Are Using Microsoft Windows
2.2 If You Are Using GNU/Linux . . . .
2.3 If You Are Using Apple Mac . . . .
2.4 Editors . . . . . . . . . . . . . . . . .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
1
1
2
2
2
3 Getting Started
3.1 Create Headings . . . . . . . . . . . . . . . .
3.2 Choose Between The dvips or pdftex Options
3.3 Insert Images . . . . . . . . . . . . . . . . . .
3.3.1 What Image Format to Use . . . . . .
3.4 Create Tables . . . . . . . . . . . . . . . . . .
3.5 Write Equations . . . . . . . . . . . . . . . .
3.6 Create References . . . . . . . . . . . . . . . .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
2
3
3
4
4
5
5
6
4 Slightly More Advanced Things
4.1 Make My Images Appear Where I Want Them
4.2 Manually Adding or Removing Space . . . . . .
4.3 Writing Special Characters in Text . . . . . . .
4.3.1 Swedish Characters . . . . . . . . . . . .
4.4 Creating Your Own Commands . . . . . . . . .
4.4.1 A Note! Command . . . . . . . . . . . .
4.4.2 Matrix With Brackets . . . . . . . . . .
4.4.3 Simplifying Subscripts . . . . . . . . . .
4.4.4 Rotation Matrix . . . . . . . . . . . . .
4.4.5 Conditional Inclusion . . . . . . . . . .
4.4.6 Redefine Existing Commands . . . . . .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
6
6
6
7
7
7
8
8
8
9
9
9
5 Writing Suggestions
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
10
6 Recommended Packages
10
7 Where Can I Find More Help
10
1
What is LaTeX
From the http://www.latex-project.org website:
LaTeX is a document preparation system for high-quality typesetting. It is most often used
for medium-to-large technical or scientific documents but it can be used for almost any form of
publishing. LaTeX is not a word processor! Instead, LaTeX encourages authors not to worry
too much about the appearance of their documents but to concentrate on getting the right
content. LaTeX is based on the idea that it is better to leave document design to document
designers, and to let authors get on with writing documents.
1.1
Explanation
Instead of a wysiwyg (what you see is what you get) word processor like you can find in
Microsoft Office or Libre/OpenOffice, LaTeX lets you write your documents in plain text
using simple programming similar syntax. The layout and appearance of your document
will depend on options and commands specified in the text as well as the use of predefined
templates. This makes it very easy to adjust the layout and style of your document without
having to apply modifications to the actual text.
1.2
Using LaTeX With a Version Control System
Since the documents are written in plain text, they can easily be use in version control systems
like Subversion1 that offers easy version control, tracking of changes and merge capabilities.
Using the merge capabilities of such a system simplifies collaborative work on the same document.
1.3
Commands and Packages
Commands or so called “macros” are used to control what happens in your document. They
work just like functions in a programming or script language. New commands can also be
defined to allow special formatting of text the way you like, these commands can then be
reused in your document. If you later want to adjust the formatting, you only need to change
the command definition in one place and rebuild your document. Specifying commands are
especially useful when you are writing repetitive complex structures of text, for example
multiple equations where only indexes change.
A large number of predefined packages that offer a wide variety of functionality exists
and can be found in tex-distributions or at http://www.ctan.org/ The Comprehensive TeX
Archive Network.
2
What do You Need
2.1
If You Are Using Microsoft Windows
First download and install MiKTeX from http://miktex.org. MiKTeX contains many LaTeX packages and tools for compiling different kinds of LaTeX output. When MiKTeX is
installed you can use any text editor of your choice to write LaTeX documents.
1
https://subversion.apache.org/
1
It is recommended to use one of the editors specially developed for LaTeX as these include
many helpful functions. Usually they will find MiKTeX if it is installed and make MiKTeX
functions available in the editor. If you try to compile a document that includes packages
that you do not have, MiKTeX can automatically download these from the Internet. This is
very convenient.
2.2
If You Are Using GNU/Linux
You distribution should have a bunch of texlive2 packages in the package repository. Install
some of these to get started. You should also choose an editor that you like. In Debian
based distributions (Ubuntu and similar) you can list these packages with the command
apt-cache search texlive
2.3
If You Are Using Apple Mac
Install MacTeX from http://www.tug.org/mactex and then select an appropriate editor.
The TeXworks and TeXshop editors are included with MacTeX.
2.4
Editors
There are many editors to choose from. These editors usually have auto completion of
LaTeX syntax and tool bars containing icons for special functions and symbols. Some
have wizards for table creation and image insertion that can be helpful. If you are going
to use special characters like å, ä and ö, and be writing the document on multiple platforms, you should use an editor that has Unicode (utf8) support. For a comparison see
http://en.wikipedia.org/wiki/Comparison_of_TeX_editors.
We recommend:
http://www.texstudio.org/
http://www.texniccenter.org
http://code.google.com/p/texworks/
3
TeXstudio (Cross-Platform)
TeXnicCenter (Windows)
TeXworks (Cross-Platform)
Getting Started
If you have chosen an editor and properly installed a LaTeX distribution such as MikTeX or
TeXLive then lets get started with your first document.
First you must create a master document file. This document will contain instruction on
what template and packages to load and what options to use. Some of the things you can do
here are: set default font and paper sizes, set document author and title, create title page and
table of contents. You can actually do this anywhere you want in your document but some
things are usually better to put here in the beginning. You can write your entire document in
the main file or include other files containing e.g. individual chapters, (recommended for large
documents). A simple main.tex example with comments is shown below (% means comment).
\documentclass[11pt,a4paper]{article} % Load the default article template
\usepackage[T1]{fontenc}
% Use T1 font encoding
\usepackage{lmodern}
% Use lmodern fonts (instead of cm)
2
http://www.tug.org/texlive
2
\usepackage[utf8]{inputenc}
% Use unicode text encoding
%\usepackage[latin1]{inputenc}
% Use in windows if no utf8 support
%\usepackage[dvips]{graphicx}
% Use for postscript graphics
\usepackage[pdftex]{graphicx}
% Use for bitmap and pdf graphics
\author{Peter Nordin}
\title{Some Title}
\begin{document}
\maketitle
% Create default title page
\tableofcontents
% Create table of contents
\listoffigures
% Create list of figures
% Write your document contents here and/or
% include external files (recommended)
\input{Introduction.tex}
% Include document file
\input{Results.tex}
% Include document file
\clearpage
% Clear the rest of the page
\bibliographystyle{plain}
% Set bibliography style to plain
\bibliography{litterateur}
% Generate bibliography
\end{document}
More details can be fount here: http://en.wikibooks.org/wiki/LaTeX/Document_Structure
3.1
Create Headings
To create headings at different levels use:
\chapter{}, \section{}, \subsection{}, \subsubsection{}.
The chapter heading is not available in the article or letter document style.
See http://en.wikibooks.org/wiki/LaTeX/Document_Structure#Sectioning_commands for
more details.
3.2
Choose Between The dvips or pdftex Options
To render graphics (images) the graphicx package must be loaded. Now you must choose
what kind of images to use and what kind of document you want to generate.
If you are going to use postscript graphics (usually .ps or .eps files) commonly used for
vector graphics3 and generate postscript documents (that can later be converted to pdf), use
dvips. To include bitmap graphics these images need to be encapsulated inside an .eps file.
There are a few different tools that can do this.
If you are going to include many bitmap images (.jpg .png and similar) use pdftex and
directly generate .pdf documents with pdflatex. If you want to include vector graphics with
pdflatex you must save those images in the .pdf format and see to it that the document borders
are tightly fitted around the actual graphics. When including a .pdf image the entire first
page will become the graphics object. We recommend that you use the pdftex option.
Hint! You should actually put the dvips or pdftex option among the document class
options, that way all loaded packages will automatically get the option.
3
http://en.wikipedia.org/wiki/Vector_graphics
3
3.3
Insert Images
Inserting images is a bit different as you can not drag and drop or re-size by dragging the
image border. Your editor probably have wizards to help you insert images though. For a
good detailed descriptions see the following page:
http://en.wikibooks.org/wiki/LaTeX/Importing_Graphics
A typical code block for image insertion is shown bellow. The [htb] option to the figure
environment tells LaTeX that you would like to place the figure “here” (where the code is in
the text) but if that would be unsuitable try to put it at the top of a page or finally at the
bottom. By using width=0.9\linewidth as an option the image will be automatically scaled
to take up 90% of the text line width.
\begin{figure}[htb]
% Position specifier (htb)
\centering
% Center the figure
\includegraphics[width=0.9\linewidth]{image.pdf} % Insert image.pdf
% Scale to 90% line width
\caption{This is the image caption}
% The image caption
\label{fig:imageexample}
% The image label for (\ref{})
\end{figure}
3.3.1
What Image Format to Use
If you are using dvips then all of your images must be saved in the .eps (Encapsulated
PostScript) format. Note! While .eps is text based, there is no guarantee that a figure
created by one program can be edited with the same or an other program. You should always
save your original figure in an editable original format (such as .svg) and export to .eps when
needed.
If you have chosen the pdftex option, then you should use the .pdf format for all your
graphs and drawn figures. You can also directly insert bitmap (raster graphics) images such
as .jpeg and .png directly. As with .eps, .pdf files may not be editable between applications,
or at all. You should save your originals images in an editable format and then export to
.pdf.
Table 1 gives some suggestions on what image format to use for different types of images.
You should strive to use vector graphics as often as possible since it will look much better
then pixel based graphics. For images with a lot of details, or graphs with a very high number
of data points, vector graphics files may, however, become very large. In these cases it may
be better to save such images as high-resolution .png images.
What
Figures that you draw yourself
Graphs that you generate from data
Graphs that you generate from data with a very high number of data points
Screen shots or figures that you can not save in a vector graphics format
Photographs
Table 1: Recommendations on what image format to choose
4
Choose format
eps or pdf
eps or pdf
png
png
jpeg
3.4
Create Tables
Creating tables in LaTeX is a bit more tricky then in programs similar to Excel but your
editor probably have wizards to assist you. For good detailed descriptions of what you can
do see the following page: http://en.wikibooks.org/wiki/LaTeX/Tables
A very simple example with comments is given below, the result is shown in Table 2. A
tabular environment can be put into a table environment, then the table becomes floating like
figures. You do not have to use the table environment if you do not want to.
\begin{table}[position specifier]
\centering
\begin{tabular}{rl|p{5cm}}
%
%
%
%
%
%
%
%
%
%
Number & Animal & Food \\
! h t b
Center the table
r = right adjusted column
l = left adjusted column
p{5cm} = 5cm wide column
(with line breaks)
| = Vertical line
Data, & = separates columns
\\ = ends row
Horizontal line
\hline
2 & Monkey & Bananas \\
1 & Tiger & Rabbits, Sheep and other small animals \\
3 & Bird & Worms
\end{tabular}
\caption{This is a simple table example} % The table caption
\label{tab:simple_animal_table}
% The table label (for \ref[})
\end{table}
Number
2
1
3
Animal
Monkey
Tiger
Bird
Food
Bananas
Rabbits, Sheep and other
small animals
Worms
Table 2: This is a simple table example
3.5
Write Equations
It is very easy to write nice looking equations in LaTeX. You can use the equation environment for floating numbered equation objects or use $ $ for in-line mathematics in text.
$\frac{\pi}{x} \pm \phi$ would generate: πx ± φ
s
qs = C q Av
2
∆p
ρ
(1)
Equation 1 is produced by the following code. The label makes it possible to create an
automatic reference to the equation later on. See section 3.6.
5
\begin{equation}
\label{eq:orifice}
q_s = C_q A_v \sqrt{ \frac{2}{\rho} \Delta p }
\end{equation}
For advanced mathematics you should load the amsmath package and read the package
documentation from http://www.ams.org/tex/amslatex.html
Also see: http://en.wikibooks.org/wiki/LaTeX/Mathematics.
3.6
Create References
To reference say an image, equation or section. See to it that you have used the \label{key}
command, then use \ref{key} to reference it. If you want to reference a figure you may
write see Figure~\ref{fig:awsome_figure}. The ~ character is space but “Figure” and
the actual reference will not be split if the line ends.
To reference an external document use \cite{bibkey} and see to it that you have a bibtex
entry in your bibliography file.
http://en.wikibooks.org/wiki/LaTeX/Labels_and_Cross-referencing
http://en.wikibooks.org/wiki/LaTeX/Bibliography_Management
4
4.1
Slightly More Advanced Things
Make My Images Appear Where I Want Them
LaTeX usually tries to make you document look nice by moving figures and tables (floating
objects) around to get a “better” mixture of pictures and text. This is one of the things that
is the most difficult to accept when you are new to LaTeX. You can adjust the size of images
and use the [htb] option to specify where you want your images but sometimes LaTeX will
put images several pages after the text you wanted the image to be close to.
For small changes add a , [!htb] to the option. This will tell LaTeX to ignore some image
placement rules. If you really must have the image placed exactly at the point where you
inserted the graphics code and the does not help, then you can use the option [H] if you have
loaded the float package. Avoid using this though, it will likely make your document look
ugly.
An other way of limiting how far images can be moved is to include the package placeins
and then use \FloatBarrier commands to force pictures (and other floating objects to be
generated before the text after the barrier in the code. One suggestion is to use the option
[section] to placeins to automatically force images to stay within the each section, but still
allow LaTeX to move them around for a nicer looking document. This option is recommended
if the default LaTeX behaviour is not to your liking. But use the FloatBarriers sparingly, if
you add them to often your document will not look good.
4.2
Manually Adding or Removing Space
It is best to let LaTeX format the output as it is supposed to. In special occasions you may
want to alter the appearance manually though. Here are some (there are many others) ways
to do that. Do not use these if you do not really have to. The point of using LaTeX is to
spend time on the document contents rather than on manual fine tuning of its appearance.
6
• If you want to create or remove a vertical or horizontal space you can use the \vspace{size}
or \hspace{size} commands.
• In math mode: \, \: \; \! means, thin space, medium space, thick space, negative thin
space. The \quad and \qquad commands can also be used.
• \smallskip \medskip \bigskip can be used for vertical skips.
• \smallbreak \medbreak \bigbreak can be used for vertical skips with paragraph break.
• \vspace{\baselineskip} will add a blank line with text height.
• \setlength{\someLength}{length} example: \setlength{\parskip}{1cm} will change
the distance between paragraphs.
• If you want to remove an automatic indentation use the \noindent command.
• To force creation of a new line use \\ or \newline.
For more info on spaces and setting lengths, see:
http://en.wikibooks.org/wiki/LaTeX/Lengths
http://en.wikibooks.org/wiki/LaTeX/Paragraph_Formatting
4.3
Writing Special Characters in Text
Some characters are used by the LaTeX interpreter. Examples are # $ % & ~ _ ^ \ { }.
If you want to write these characters in your text you must “escape them” using the backslash
\ character in front of them, like this: \% becomes %. But since \\ means newline you can
use $\backslash$ to print the actual \ character.
The \verb command to interpret a set of characters “verbatim” may also come in handy.
Example: \verb!$X_{i}$! would show the code $X_{i}$ for generating the inline math Xi .
Note that \verb also means that type-writer font is used. The first character, in this case
! found after \verb acts as the enclosing character. The \verb command ends at its next
occurrence.
You can also use the verbatim environment to create longer verbatim texts. It is used
for the code examples in this document.
4.3.1
Swedish Characters
If you are using utf8 as input text encoding then you can write special letters directly in your
text and they will show up like any other letter. If you are not using utf8 and want to have
a cross platform supported document you must use special cods to represent special letters.
See http://en.wikibooks.org/wiki/LaTeX/Special_Characters for a full description, and
Table 3 for Swedish special characters.
4.4
Creating Your Own Commands
Commands, often referred to as macros can be created with
\newcommand{\commandName}[numArgs]{definition}. See for example this link for details
http://en.wikibooks.org/wiki/LaTeX/Macros. Here are some examples:
7
å
ä
ö
Å
Ä
Ö
\aa
\"{a}
\"{o}
\AA
\"{A}
\"{O}
Table 3: Cross-platform supported character special codes for Swedish characters. These will
work regardless of what input encoding is used. It is, however, recommended that you use
utf8 instead.
4.4.1
A Note! Command
If you end up writing “Note!” a lot, then this command my be nice. Note! you must write
\note{} in your text if you want to preserve the space after the command.
\newcommand{\note}{\textbf{Note!}}
4.4.2
Matrix With Brackets
A bracket matrix can be created with a bmatrix environment. If you write such matrices
often, the following “bmx” command might be useful as your equation code will be more
readable. Equation 2 shows the result.
% Define the bmx command:
\newcommand{\bmx}[1]{\ensuremath{\begin{bmatrix}#1\end{bmatrix}}}
% Use the bmx command: The equation code becomes more readable then if you
% would have to write \begin{bmatrix} *** \end{bmatrix} everywhere
\begin{equation}
\label{eq:command_ex1}
\bmx{ x \\ y } = \bmx{1 & 0 \\ 0 & 1} \bmx{x \\ y}
\end{equation}
" #
"
1 0
x
=
0 1
y
4.4.3
#" #
x
y
(2)
Simplifying Subscripts
Simplifying subscripts for common variables can be done like this. The result is shown in
Equation 3.
% Define a pose command, and subscript shortcuts
\newcommand{\pose}[2]{\ensuremath{X_{#1,#2}}}
\newcommand{\xij}{\pose{i}{j}}
\newcommand{\xjk}{\pose{j}{k}}
\newcommand{\xik}{\pose{i}{k}}
% Use them
\begin{equation}
8
\label{eq:command_ex2}
\xik = \xij \oplus \xjk
\end{equation}
Xi,k = Xi,j ⊕ Xj,k
4.4.4
(3)
Rotation Matrix
Defining a rotation matrix, where the input argument is the angle. You can then easily
reproduce
the matrix
for different
"
#"
# angles. \rotmat{\alpha} \rotmat{\phi} will result in
cos α − sin α cos φ − sin φ
.
sin α cos α
sin φ cos φ
Note! The example below uses the \ensuremath{} command. This means that you can
use your new math command in both text and math mode (You do not need to add $ $ for
in-line math).
\newcommand{\yawrotmat}[1]{%
\ensuremath{\begin{bmatrix} \cos #1 & -\sin #1\\
\sin #1 & \cos #1\\
\end{bmatrix}
}
}
4.4.5
Conditional Inclusion
Sometimes you may want to conditionally include contents in your document. If you are
writing a project report for a company then maybe you want to generate one public and one
secret company version of the report. This example requires the etoolbox package to be
loaded.
\newbool{isCompanyVersion}
\setbool{isCompanyVersion, true} % Set bool true
% Conditionally include text
\ifbool{isCompanyVersion}{%
The bool is True
Here you write the LaTeX code for
Note! You can leave this empty if
}{%
The bool is False
Here you write the LaTeX code for
Note! You can leave this empty if
}
4.4.6
the company version of the report
you want nothing to appear
the public version of the report
you want nothing to appear
Redefine Existing Commands
You can also redefine existing commands if you want them to do something else. Note! Be
careful, as some other packages may be relying on the command output.
9
\renewcommand{\deg}{\ensuremath{^\circ}}
\renewcommand{\vec}[1]{\ensuremath{\mathbf{#1}}}
In this example we redefine the \deg and \vec command to show a degree sign, 2\deg{} =
2◦ , and vector variables to be shown with a bold font, \vec{v} = v.
5
Writing Suggestions
• Do not micro manage the layout of your document with massive amounts of newline or
space commands. You should only do that locally when you really have to. If you find
yourself micromanaging everything then you are doing it wrong and you are wasting
your time, it will not look good. Let LaTeX do its thing and then adjust the layout
when you are done writing your text.
• While it may feel awkward at first, try to write one sentence per line, instead of writing
an entire paragraph on one long line. This makes it easier to get a correct location when
an error occurs and makes it much easier to track changes in a document when you are
using a version control system such as Subversion as they will track changes per row.
6
Recommended Packages
This section lists some recommended packages, but there are many others, see http://www.
ctan.org. Note! You should not load packages that you do not need. You should at least
glance at the documentation of a package to get some idea of what you can do with it and
what options are available. Most packages are highly configurable.
Package
csquotes
placeins
amsmath
hyperref
tikz
todonotes
fancyhdr
listings
7
Description
Create language aware “quotes” with the \enquote command.
Create \FloatBarrier to avoid floating objects to cross into other sections
of your document.
You must load this package if you want to create advanced mathematics.
Makes links and references click-able and coloured (optional) in a pdf.
Draw vector graphics by code in LaTeX (very advanced), see http://www.
texample.net/tikz/ for examples.
Add “todo notes” to your document.
Makes it possible create more fancy header and footer.
Include source code with syntax highlighting.
Where Can I Find More Help
The easiest way to find help is to use Google search or to look at
http://en.wikibooks.org/wiki/LaTeX
Some recommended documents are:
10
The not so Short Introduction to LaTeX
http://www.ctan.org/get/info/lshort/english/lshort.pdf
A LaTeX cheat sheet (pdf)
http://www.stdout.org/~winston/latex/latexsheet-a4.pdf
Everything you need to know about image insertion, and then some
http://www.ctan.org/get/info/epslatex/english/epslatex.pdf
Some other interesting sites are:
Home of the LaTeX project http://www.latex-project.org
TeX Users Group
http://www.tug.org
Björns page on LaTeX
http://www.iei.liu.se/flumes/bjorn/latex
The Comprehensive TeX Archive Network http://www.ctan.org
11