Sie sind auf Seite 1von 23

A very short introduction to LATEX


Huan Ha

December 16, 2018

Abstract

LATEXis very useful in academic writing. However, the learning curve


is quite deep, you need time to get use to it. This document is written
for myself during my limited journey with LATEX because I feel that I
learn better by re-write it. I think it is also useful for others who need
a short introduction to LATEX for their work (mostly for students writing
dissertations and newbies in academic). This text is far from a (rather)
complete introduction to LATEX. For a complete encyclopedia of LATEX,
go to CTAN website.

1 Why use LATEX?

I think most of us, researchers and students use LATEX for the needs of typing
mathematics in our document. While Word processors such as Microsoft Word,
Libre Office and Page gives a nice What-You-See-Is-What-You-Get editor, their
functions to typeset and export mathematic formulas are very limited, partic-
ularly for not-at-ease-using-mouse people like myself. I always feel that typing
mathematics in Word is a pain, it makes too long time and I have to move
the mouse too much to find the correct button. In LATEX, you can “code” the
formulas and export it in high quality that you can use for presentations or
∗ hathuchuan@gmail.com

1
printable articles. It takes time to learn it, certainly, but one you know it, you
can do it a lot faster than moving the mouse around the screen.
LATEX also has the advantage of predefined layouts that are really useful for
publishing your article. By producing your text in LATEX, you can then use
any template from any publisher to change it style in seconds. Saving time on
defining “Heading1”, “Heading 2” . . . , you focus more on the writing task.
LATEX can also be used to produce presentations, not as fancy as Power
Point, Preview or Prezi, but with its predefined layouts, you can make it fast.
Another reason to use LATEX is the network effect. If you are in graduate and
post-graduate at university, in disciplines related to mathematics, almost every-
body is using LATEX to write. It is required if you want your article published
anyway.

2 Installing LATEX

The installation of LATEXincludes 2 fundamental things: the LATEX distribution


for processing all behind-the-scene task to translate your input files into PDF
or DVI documents, and the LATEX editor for editing your source files.

2.1 The distribution

On MacOs, just download MacTeX and follow the instruction of installation.


MacTeX provides a full LATEX installation plus a number of additional tools
(the tools I use the most is LaTeXiT, in which you “code” the math formula an
receive the nice math that you can copy and insert in other documents)
The equivalent of MacTeX on Windows is MiKTeX. People tell me that the
coolest feature of MiKTeX is that it will download missing LATEXpackages and
install them automatically while you compile a document. As I only use MacOs,
I leave the comments to you.

2
A cross-platform distribution exists for Windows, Unix and MacOs is TeX Live.
I’ve never used it myself.

2.2 The editor

There are many LATEX editors on all platforms. I personally prefers TeXmaker
and its “children” TeXstudio. These two can be used on Windows, Mac and
Unix/Linux equally well.
If you are not happy with these two, check out the 8 best LATEXeditors of
the year 2017 or the big list of LATEX editors. TeXnicCenter and WinEdt are
specific for Windows while TeXShop is for MacOs. BaKoMa is a nice live-view1
editor but it is not free. The debate on which one is the best editor has been
for years and probably will never end. Use what you find suitable.

3 Fundamentals

The input for LATEXis a plain text file. Just open your editor and create a new
file, then you can start. This file need to be saved (as a .tex) file before you can
compile it.
Open your editor, create the new file and type

\documentclass[12pt]{article}
\usepackage{amsmath}
\linespread{1.6}
\begin{document}
This is the input file.
\end{document}

Then save it and press the COMPILE button. It is normally a right triangle on
the toolbar of the editor.
1 You will see how you miss a visual WYSIWYG (What you see is what you get) editor
when you look at the forest of codes in a standard LATEX editor

3
3.1 Preamble

This is where the “predefined layout” part of LATEX gets in. To make LATEX
understand what type of document you want to create and the layout of it, you
declare everything before to start writing the text. An example of an input file
is as follow:
Every command before \begin{document} in the input above is the pream-
ble.
You start by declaring the class of document by \documentclass[options]{class}.
The classes are

• article for articles in scientific journals, short dissertations or anything


that are not too long2

• proc for proceedings based on the article class

• minimal for minimal needs. It only set a page size and a base font.

• report for longer documents containing several chapters, small books,


PhD theses, . . .

• book for real books

• slides for slides with big sans serif letters. you’d better use the beamer
class instead

There are many options, for a full description, look at this document. For basic
usage, you don’t need to use much of them. The default font size is 10pt, so I
normally put 12pt in to have large font size.
\usepackage{...} is to load package that add new features to the LATEXsystem.
I will mention some usual packages in the following sections, whenever you see
a package (amsmath for mathematics typeset for example), you know that you
have to load that package (\usepackage{amsmath}) in the preamble to use it.
2 this document that you are reading uses this class

4
In the preamble, you also add commands to influence the style of the whole
documents, the command \linespread{1.6} tell LATEXabout the interline spac-
ing in the output document, here 1.6 means you want an equivalent of double
space in Microsoft Word.
When all the setup work is done, you start the body of the text with the
command \begin{document} At the end of the document, you add the com-
mand \end{document} to tells LATEX to finish. Anything after this command
will be ignore.
Remark: this is what makes LATEXis useful in publication. You can go to
the publisher, download their template, put in your preamble and everything
will be set in the required style. If I want to submit my paper to Elsevier Ltd.,
I type \documentclass{elsarticle}. No more job!

3.2 Commands

Commands are basic units of LATEXjust as in any other programming language


(see, you are coding while using LATEX). They start with a backslash \ and then
have a name cosisting of letters only. To type LATEX for example, I type the
command \LaTeX. Pay attention that the whitespace after commands is ignores,
you have to use the empty parameter {} to stop this.

If you only write \LaTeX


If you only write LATEXthere is no
there is no space between
‘‘\LaTeX{}’’ and ‘‘there".
space between “LATEX” and “there”.

To avoid this, write \LaTeX{} To avoid this, write LATEX to stop

to stop \LaTeX{} LATEX from eating the space after it.


from eating the space after it.

3.3 Comments

When LATEX (remember: \LaTeX{}) encounters a %, it ignores the rest of the


line. We use this to add notes in the input file, they will not be shown in the

5
printed version.

Huan %you know who


Huan is an economist
is an economist

The elasticity of demand


\begin{comment}
you’ve learned this The elasticity of demand is an eco-

in undergrad nomic concept

\end{comment}
is an economic concept

3.4 Spaces

All types of spaces (blank space or tab) are treated uniformly as “space” in
LATEX. Several consecutive whitespace are treated as one “space”. Whitespace
at the start of a line is ignored, a single line break is treated as “whitespace”.

It does not matter whether you


enter one or several spaces It does not matter whether you enter
after a word.
one or several spaces after a word.
An empty line starts a new paragraph.
An empty line starts a new
paragraph.

One thing to pay attention here, to start a new paragraph, make an empty
line. Most people completely underestimate the importance of well-placed para-
graph breaks. If you continue the same idea, you must be on the same paragraph.
When you start a new idea, a new paragraph must start. This is particularly
important when you have mathematics equation in the paragraph. Look at the
following input and the output in blue.

The primary producer chooses productions $q_1$ and $q_2$


to maximize its profit throughout the two periods

6
\begin{equation}
\max_{q_1, q_2} \pi^E = q_1 \Big(1 - q_1\Big)
+ q_2 \Big( 1 - q_2 - \sum_{1}^{m} r_k \Big)
\end{equation}
with $r_k$ the quantity of recycled material
produced by the recycler $k$ in the second period.

In the second period, each recycler $k$ maximizes its profit,


subject to the resource constraint of scraps collected
\begin{equation}
\begin{split}
\max_r \pi^R_k & = r_k(1 -q_2 - r_k - \sum_{l \neq k} r_l - c)\\
\text{s.t} \quad r_k & \leq \alpha q_1 - \sum_{l \neq k} r_l
\end{split}
\end{equation}

The primary producer chooses productions q1 and q2 to maximize its profit


throughout the two periods
   m
X 
max π E = q1 1 − q1 + q2 1 − q2 − rk (1)
q1 ,q2
1

with rk the quantity of recycled material produced by the recycler k in the


second period.
In the second period, each recycler k maximizes its profit, subject to the
resource constraint of scraps collected
X
max πkR = rk (1 − q2 − rk − rl − c)
r
l6=k
X (2)
s.t rk ≤ αq1 − rl
l6=k

When a paragraph start (”In the second period”), the first line is indented,
this is the signal to the reader that you start a new idea. But at “with rk ”,

7
even though the equation breaks the line, the sentence is not indented, this tells
reader to keep going with the same idea as above.
If you want to break the line without starting a new paragraph, you have to
type \\ or \newline. The command \\* additionally prohibits a page break
after the forced line break.
If you want to have a page break, use the command \newpage.

3.5 Functional characters

The following characters are used to tell LATEX to do somethings.

# $ % ^ &_ { } ~ \

So when you type these characters, they will not appear in your output and
may cause errors that can stops LATEXto work unexpectedly. To typeset these
characters, use

\# \$ \% \^{} \& \_ \{ \} \~{} \textbackslash

respectively. The backslash character \ cannot be entered by \\, this sequence


is used for line breaking. The command \textbackslash is used instead.

3.6 Quotation marks

The quotation mark in LATEXis not ideal, you have to use the “grave accent”
(`) for opening quotes and vertical quote (´) for closing. use two ` and two ´
for double quotes(the latter is the normal quotation marks on your keyboard).

‘‘ Please press the ‘x’ key." “ Please press the ‘x’ key.”

3.7 Ellipsis (. . . )

The command for ellipsis is \ldots (low dots), the ... makes the dots and
spaces smaller.

8
Not like this ... but like this:\\ Not like this ... but like this:

New York, Tokyo, Budapest, \ldots New York, Tokyo, Budapest, . . .

3.8 Emphasized Words

You can underline words by typing \underline{underline words}, make them


bold by typing \textbf{make them bold}, italic by \textit{italic}. In ed-
itors such as TeXstudio, you have the shortcuts for Bold and italic similar to
Word. You can also use \emph{ }. This comment will automatically make
words italic if the sentence is in normal font, and use the normal font if the
sentence is already in italic.

3.9 Environments

Environments in LATEX can be anything from equation to array, itemize. You


can nest environments within each other as long as the correct nesting order is
maintained. The input is like below:

\begin{environment}
...
\end{environment}

3.9.1 Itemize, Enumerate and Description

These are the environments for listing, itemize is suitable for simple lists,
enumerate for enumerated lists and description for descriptions. Try the
following example:

9
\begin{enumerate}
\item You can use itemize for
simple list
1. You can use itemize for simple
\begin{itemize}
\item with black dots list

\item[-] or a dash.
• with black dots
\end{itemize}
- or a dash.
\item You can use description with
imagination 2. You can use description with
\begin{description}
imagination
\item[Stupid] when you hate it
\item[Smart] when you like it Stupid when you hate it

\item[$\heartsuit$] when you love Smart when you like it


it
♥ when you love it
\item[$\Delta$] if you want to
play around ∆ if you want to play around

\item[$\infty$] if you want this ∞ if you want this list never end
list never end
\end{description}
\end{enumerate}

Listing in LATEX is leveled automatically when you nest many level of the
same listing environment.

10
\begin{itemize}
\item I know
\begin{itemize}
\item I’m stupid
\begin{itemize}
\item most of the time
\end{itemize} • I know
\end{itemize}
– I’m stupid
\end{itemize}
∗ most of the time

\begin{enumerate}
1. But I don’t know
\item But I don’t know
\begin{enumerate} (a) sometimes
\item sometimes
i. I’m too smart
\begin{enumerate}
A. particularly when
\item I’m too smart
it is unnecessary
\begin{enumerate}
\item particularly when it is
unnecessary
\end{enumerate}
\end{enumerate}
\end{enumerate}
\end{enumerate}

3.9.2 Tabular

The command tabular can be used to create beautiful tables in LATEX. The
code to type is

\begin{tabular}[pos]{cols}
...
\end{tabular}

11
cols is where you define the table’s column, if you want a vertical frame, use
|, the choices for alignment are l, r, c respectively for left, right and center
alignment.

\begin{tabular}{|l|l|}
\hline
Mai& Huynh \\
Mai Huynh
\hline
Huan An
Huan & An \\
\hline
\end{tabular}

\begin{tabular}{|p{5 cm}|}
\hline
Welcome to Box paragraph. Welcome to Box paragraph. We

We sincerely hope you will sincerely hope you will all enjoy
all enjoy the show.\\ the show.
\hline
\end{tabular}

4 Math typeset

4.1 Declare math environment

Mathematics typeset is considered as an type of environment in LATEX. To


declare in-line mathematics environment, use $ <math> $. This code exports
mathematics formula in standard format to match with the text. To declare
display mathematics environment, use $$ <math> $$. This code automatically
break the line and put the mathematics in center of the next line. A more
formally way to declare display mathematics environment is

\begin{equation}
<math>

12
\end{equation}

or

\begin{equation*}
<math>
\end{equation*}

The first one automatically put a number after your mathematics, the second
omits it.

This equation $x^2 + x - 2 = 0 $


This equation x2 + x − 2 = 0
This equation $$ x^2 + x - 2 = 0$$ This equation
\begin{equation}
x2 + x − 2 = 0
x^2 + x - 2 = 0
\end{equation}
x2 + x − 2 = 0 (3)
\begin{equation*}
x^2 + x - 2 = 0 x2 + x − 2 = 0

\end{equation*}

4.2 Matrices

Matrices can be typeset by array, but amsmath provides a better solution:


matrix for no border, pmatrix for (, bmatrix for [, Bmatrix for {, vmatrix
for |, Vmatrix for ||. Using these environment, you don’t need to declare the
number of columns as with array.
you can use \cdots and \ldots and \cdot to shortcut lines and columns in
matrices.

13
\begin{equation*}
\begin{matrix}
1 & 2 \\
3&4 \end{matrix} \qquad \begin{
bmatrix}
p_{11} & p_{12} & \ldots  

& p_{1n} \\  p11 p12 ... p1n 


 
1 2  p21 p22 ... p2n 
p_{21} & p_{22} & \ldots 
 .

 . .. .. .. 
3 4  . . . . 

& p_{2n} \\  
\vdots & \vdots & \ddots pm1 pm2 ... pmn

& \vdots \\
p_{m1} & p_{m2} & \ldots
& p_{mn}
\end{bmatrix}
\end{equation*}

4.3 Spacing in math mode

Because math mode doesn’t treat spaces when you type as spaces in the output.
You can insert special spacing commands: \, for 3/18 quad, \: for 4/18 quad,
\; for 5/18 quad, \ and \> for a medium size space, \quad and \qquad for
wider spaces. If you find that the space is to wide, you can also reduce it by
typing \! to reduce the space by -3/18 quad.
When you type multiple integrals as in $\int \int f(x,y) \di x \di y$ ,
RR
you will find the spacing between the integrals is too wide: f (x, y) dx dy.
Correct it by using \! - $\int \!\!\!\int f(x,y) \di x \di y$ or us-
ing amsmath with these command \iint, \iiint, \iiiint and idotsint like
RR
$\iint f(x,y) \di x \di y$ to get f (x, y) dx dy. The latter solution is
not only more efficient but also shorter to type.

14
4.4 Differential
Rb
a
x dx gives the conventional differential symbol a bit lean, to correct it, we
Rb
use \mathrm{d} as a x dx. If you have to write a lot of this, define the new
command by typing \newcommand{\ud}{\,\mathrm{d}} in the preamble, then
Rb
you can write \int_{a}^{b} x \di x to get the same thing a x dx.

4.5 Math fonts

Sometimes in math mode you need to write letters in different fonts like the
set of real numbers R or the Lagrangian expression L , then you have different
math fonts to choose. I simply stick to the one I like the most. Below are the
fonts to choose. As usual, if you use the specific letter a lot, it’s easier to set
new command \newcommand{\la}{\mathscr{L}} in the preamble so that you
can shorten your typeset \la for L
Example Command Required package
ABCDEabcde1234 \mathrm{ABCDE abcde 1234}
ABCDEabcde1234 \mathit{ABCDE abcde 1234}
ABCDEabcde \mathnormal{ABCDE abcde 1234}
ABCDEabcde∞∈34 \mathcal{ABCDE abcde 1234}
A BC DE \mathscr{ABCDE abcde 1234} mathrsfs
ABCDEabcde1234 \mathfrak{ABCDE abcde 1234} amsfonts or amssymb
ABCDEa1234 \mathbb{ABCDE abcde 1234} amsfonts or amssymb
Sometimes you need to tell LATEX the correct font size. In math mode, this
is set with the following commands: \displaystyle (123), \textstyle(123),
\scriptstyle (123) and \scriptscriptstyle (123)

Changing styles generally affects the way big operators and limits are dis-
P
played. If is placed in a fraction, it’ll be set in text style unless you tell
LATEXotherwise:

\begin{equation*}

15
P = \frac{\sum_{i=1}^n (x_i- x) (y_i- y)}
{\displaystyle{\left[ \sum_{i=1}^n(x_i-x)^2
\sum_{i=1}^n(y_i- y)^2 \right]^{1/2}}}
\end{equation*}

gives Pn
i=1 (xi − x)(yi − y)
P =" #1/2
n
X n
X
2 2
(xi − x) (yi − y)
i=1 i=1

4.6 Bold symbols

In math mode you can use \mathbf to get bold symbols but it doesn’t work
on lower case Greek letters. There is a \boldmath command that works for
symbols too, but this can only be used outside math mode:

$\mu, M \qquad \mathbf{\mu}, \mathbf{M}$ \qquad \boldmath{$\mu, M$}

gives µ, M µ, M µ, M

4.7 Theorems, Lemmas, . . .

When writing, you probably need a way to typeset “Lemmas”, “Definitions”,


“Axioms” and similar structure , then you can use this

\newtheorem{name}[counter]{text}[section]|

4.8 Proofs and End-of-Proof Symbol

The amsthm package provides the proof environment

16
\begin{proof}
Proof. Trivial, use
Trivial, use
\begin{equation*}
E = mc2 .
E=mc^2.
\end{equation*}
\end{proof}

With the command \qedhere you can move the ‘end of proof’ symbol around
for situations where it would end up alone on a line.

\begin{proof}
Trivial, use
Proof. Trivial, use
\begin{equation*}
E=mc^2. \qedhere E = mc2 .
\end{equation*}
\end{proof}

In IEEEeqnarray, the trick of \qedhere is a bit messy. You need to define


the stretching explicitly to put it in the correct position. Look at the two
examples bellow:

\begin{proof}
This is a proof that ends
Proof. This is a proof that ends with
with an equation array:
an equation array:
\begin{IEEEeqnarray*}{rCl}
a & = & b + c \\ a=b+c
& = & d + e. \qedhere
= d + e.
\end{IEEEeqnarray*}
\end{proof}

17
\begin{proof}
This is a proof that ends
Proof. This is a proof that ends with
with an equation array:
an equation array:
\begin{IEEEeqnarray*}{+rCl+x*}
a & = & b + c \\ a=b+c
& = & d + e. & \qedhere
= d + e.
\end{IEEEeqnarray*}
\end{proof}

Here the + in {+rCl+x*} denotes the stretchable spaces, one on the left and
one on the right of the equations. But now on the right, after the stretching
column, we add an empty column x. This column is to put the \qedhere in.
Finally, we specify a *. This is a null-space to prevent IEEEeqnarray from
adding another unwanted space.
In the case of equation numbering, without declaring \qedhere, we’ve got
the same problem

\begin{proof}
Proof. This is a proof that ends with
This is a proof that ends
with a numbered equation:
a numbered equation:

\begin{equation}
a = b + c. (4)
a = b + c.
\end{equation}
\end{proof}

we have to correct it by

18
\begin{proof}
This is a proof that ends Proof. This is a proof that ends with
with a numbered equation: a numbered equation:
\begin{equation}
a = b + c. (5)
a = b + c. \qedhere
\end{equation}
\end{proof}
Another example is

\begin{proof}
Proof. This is a proof that ends with
This is a proof that ends
an equation array:
with an equation array:
\begin{IEEEeqnarray}{rCl}
a=b+c (6)
a & = & b + c \\
= d + e. (7)
& = & d + e.
\end{IEEEeqnarray}
\end{proof}
contrasting with

\begin{proof}
This is a proof that ends Proof. This is a proof that ends with
with an equation array:
an equation array:
\begin{IEEEeqnarray}{+rCl+x*}
a & = & b + c \\ a=b+c (8)
& = & d + e. \\ = d + e. (9)
&&& \qedhere\nonumber
\end{IEEEeqnarray}
\end{proof}

5 Graphics

Put \usepackage[driver ]{graphicx} in the preamble. Then use the com-


mand

19
\includegraphics[key=value, . . . ]{file}

to include file into your documents. The keys can be used to alter the width,
height and rotation of the included graphic.
\width scale graphic to the specified width
\height scale graphic to the specified height
Example:
\angle rotate graphic counterclockwise
\scale scale graphic size

\begin{figure}
\centering
\includegraphics[angle=90, width=0.5\textwidth]{test}
\caption{This is a test.}
\end{figure}

includes the graphic stored in the file test. The graphic is rotated by an angle of
90 degree and scaled to the final width of 0.5 the width of a standard paragraph.

6 Bibliography

This part is really important if you are writing academic documents. Check out
the BibTex program.
For short bibliography, you can include it in the input file as follow

\cite{Ha (2007)} has Ha (2007) has proposed that . . .


proposed that \ldots
\begin{thebibliography}{99}
\bibitem{Ha (2007)} Huan Ha:
References
\emph{very short intro to \TeX},
[1] Huan Ha: very short intro to
2017
TEX, 2017
\end{thebibliography}

20
7 Creating mathematical graphics

Sometime you will want to draw graphs directly in the input file because you
want a high-quality graph or you want to use the graph in slides with different
animations. Then LATEXprovides a package Tikz (\usepackage{tikz}) to help
you. The creation phase is tiring but you will enjoy the result when it is done.

8 Customizing LATEX

8.1 New command

To shorten the typeset, you can put \newcommand{name}[num]{definition}


in the preamble. Then whenever you want to calll it, simply call the name part,
LATEXwill understand the shortcut. Examples: After defining

\newcommand{\di}{\,\mathrm{d}}
\newcommand{\la}{\mathscr{L}}
\newcommand{\tvsil}{The very short intro to \LaTeX}

and write This is $\di$, $\la$ and \tvsil, you will get
This is d, L and The very short intro to LATEX

8.2 New environment

Just as with \newcommand, you can also create your own environments with the
syntax

\newenvironment{name}[num]{before}{after}

21
8.3 Fonts and Sizes

8.4 Spacing

8.4.1 Line spacing

If you want the use large inter-line spacing (single, 1.5, double spacing in
Word), put \linespread{factor } in the preamble of your document. Use
\linespread{1.3} for 1.5 line spacing, and \linespread{1.6} for double line
spacing. Without declaring, the default line spread factor is 1.

8.4.2 Horizontal spacing

To add horizontal space, use \hspace{length}. You can declare length by cm


or other units that LATEXunderstands. If you want to keep the space even if it
falls at the end or the start of the line, use \hspace*{lenght} instead.

8.4.3 Vertical spacing

Similarly to horizontal spacing, you can use the commands \vspace{length}


and \vspace*{length} if you want to keep the space even at the top or the
bottom of a page.
Additional space between two lines of the same paragraph or within a table
is specified with the command \\[length].
With \bigskip and \smallskip you can skip a predefined amount of vertical
space without having to worry about exact numbers

9 More fun stuffs

9.1 Macros

In TexStudio, TexMaker or TexShop, you can define macros to speed up your


typing. Macros are simply shortcuts to help you type longer texts/codes. In

22
Figure 1: Edit macros in TexStudio

Figure 9.1, you can find an illustration of editing macros in TexStudio. In this
example, I define the macro \serv to auto-complete for the world “servitiza-
tion”. As I am lazy and the word is rather weird, this macro helps me in writing
fast and correctly the word. In the other macro, \equa is to be replaced by the
environment

\begin{equation}

\end{equation}

the symbol %| or %<%> indicates the placeholders where located the cursor where
you start to write when the environment pops out. Here in the

23

Das könnte Ihnen auch gefallen