introduction to latex - people.cs.aau.dkpeople.cs.aau.dk/~htj/work/csintro06-latex.pdf · • the...

42
Introduction to L A T E X Henrik Thostrup Jensen September 29 th 2006 1

Upload: vanthuy

Post on 26-Aug-2018

228 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Introduction to LaTeX - people.cs.aau.dkpeople.cs.aau.dk/~htj/work/csintro06-latex.pdf · • The package fancyheadings has some fancy styles, e.g., fancy, fancyplain ... • Beamer

Introduction to LATEX

Henrik Thostrup Jensen

September 29th 2006

1

Page 2: Introduction to LaTeX - people.cs.aau.dkpeople.cs.aau.dk/~htj/work/csintro06-latex.pdf · • The package fancyheadings has some fancy styles, e.g., fancy, fancyplain ... • Beamer

About

• What is LATEX

• How does it work

• Exercises

• Fetch slides and work from them• Not everyone works with same speed/focus• First a topic is explained, then an exercise is given

2

Page 3: Introduction to LaTeX - people.cs.aau.dkpeople.cs.aau.dk/~htj/work/csintro06-latex.pdf · • The package fancyheadings has some fancy styles, e.g., fancy, fancyplain ... • Beamer

On learning

• ASK QUESTIONS DAMMIT!

• If something does not work• If you do not understand something• If you want to know something outside the

curriculum

• Not knowing is not failure• Not wanting to know is

• You are responsible for your learning• If you are not responsible today, you probably won’t

be tomorrow.

• Ask questions

3

Page 4: Introduction to LaTeX - people.cs.aau.dkpeople.cs.aau.dk/~htj/work/csintro06-latex.pdf · • The package fancyheadings has some fancy styles, e.g., fancy, fancyplain ... • Beamer

What is LATEX

• Document preparation system

• A markup language - not WYSIWYG

• A front end to TEX(τεχ)

• Must be “compiled” to get a document

• The thing you should write your report in• Because it is better than the alternatives

4

Page 5: Introduction to LaTeX - people.cs.aau.dkpeople.cs.aau.dk/~htj/work/csintro06-latex.pdf · • The package fancyheadings has some fancy styles, e.g., fancy, fancyplain ... • Beamer

A simple LATEXdocument

\documentclass{report}\begin{document}

some text\end{document}

5

Page 6: Introduction to LaTeX - people.cs.aau.dkpeople.cs.aau.dk/~htj/work/csintro06-latex.pdf · • The package fancyheadings has some fancy styles, e.g., fancy, fancyplain ... • Beamer

Slightly more advanced LATEXdocument

% preamble\documentclass[10pt,twoside,a4paper]{report}

% content\begin{document}\chapter{Some chapter}\section{A section}some textual content\end{document}

6

Page 7: Introduction to LaTeX - people.cs.aau.dkpeople.cs.aau.dk/~htj/work/csintro06-latex.pdf · • The package fancyheadings has some fancy styles, e.g., fancy, fancyplain ... • Beamer

Producing Output

• Compiling:latex file.tex• Will output file.dvi• Can be viewed with xdvi

• Postscript: dvips file.dvi -o file.ps

• PDF: dvipdf file.dvi -o file.pdf

• Alternative: pdflatex file.tex• Directly from LATEXto PDF

7

Page 8: Introduction to LaTeX - people.cs.aau.dkpeople.cs.aau.dk/~htj/work/csintro06-latex.pdf · • The package fancyheadings has some fancy styles, e.g., fancy, fancyplain ... • Beamer

Document Structure 1/2

• First a preamble

• Font page, Title page, Preface

• Table of contents (auto generated)

• Contents

• Appendix

• Bibliography (auto generated using BibTEX)

8

Page 9: Introduction to LaTeX - people.cs.aau.dkpeople.cs.aau.dk/~htj/work/csintro06-latex.pdf · • The package fancyheadings has some fancy styles, e.g., fancy, fancyplain ... • Beamer

Document Structure 2/2

• Split the document into several files

• Include other files using \input{file.tex}• One include/master file is usually the best

9

Page 10: Introduction to LaTeX - people.cs.aau.dkpeople.cs.aau.dk/~htj/work/csintro06-latex.pdf · • The package fancyheadings has some fancy styles, e.g., fancy, fancyplain ... • Beamer

Preamble

• The cursing that goes before the document

• Not magical!

• Typical structure:

• Document class• Packages• Macros, environments, etc

10

Page 11: Introduction to LaTeX - people.cs.aau.dkpeople.cs.aau.dk/~htj/work/csintro06-latex.pdf · • The package fancyheadings has some fancy styles, e.g., fancy, fancyplain ... • Beamer

Preamble Example

\documentclass[10pt,twoside,a4paper]{report}\usepackage[english]{babel}\usepackage{t1enc}\usepackage{graphicx}\usepackage{makeidx}\usepackage{xspace}\makeindex

\bibliographystyle{plain}

\newcommand{\word}[0]{My word\xspace}\renewcommand{\texttt}[1]{{\tt{\small{#1}}}}

11

Page 12: Introduction to LaTeX - people.cs.aau.dkpeople.cs.aau.dk/~htj/work/csintro06-latex.pdf · • The package fancyheadings has some fancy styles, e.g., fancy, fancyplain ... • Beamer

About the Exercises

• First a concept is explained

• Then a small exercise is given

• Download the slides and follow them• Available on the course home page

• You can skip an exercise if you know the topic

12

Page 13: Introduction to LaTeX - people.cs.aau.dkpeople.cs.aau.dk/~htj/work/csintro06-latex.pdf · • The package fancyheadings has some fancy styles, e.g., fancy, fancyplain ... • Beamer

Exercise 1

• Create a simple document using the previousexamples (or other sources)

• Compile it to a postscript or PDF file

13

Page 14: Introduction to LaTeX - people.cs.aau.dkpeople.cs.aau.dk/~htj/work/csintro06-latex.pdf · • The package fancyheadings has some fancy styles, e.g., fancy, fancyplain ... • Beamer

Exercise 2

• Split the document into a master, a preamble, and acontent file, and recompile

14

Page 15: Introduction to LaTeX - people.cs.aau.dkpeople.cs.aau.dk/~htj/work/csintro06-latex.pdf · • The package fancyheadings has some fancy styles, e.g., fancy, fancyplain ... • Beamer

Parts, Chapters, and Section

• \part• \chapter• \section• \subsection• \subsection• \subsubsection• \paragraph• Used to divide the report into logical chunks

• \part is usually not needed

• Note: These are specific to the report class

15

Page 16: Introduction to LaTeX - people.cs.aau.dkpeople.cs.aau.dk/~htj/work/csintro06-latex.pdf · • The package fancyheadings has some fancy styles, e.g., fancy, fancyplain ... • Beamer

Exercise 3

• Create entries with everything from chapter toparagraph in your document, and recompile

16

Page 17: Introduction to LaTeX - people.cs.aau.dkpeople.cs.aau.dk/~htj/work/csintro06-latex.pdf · • The package fancyheadings has some fancy styles, e.g., fancy, fancyplain ... • Beamer

Italic, Bold, Teletype, and Verbatim

• Sometimes you want text in italic or something else

• \textit{words in italic}• \textbf{words in bold}• \texttt{words in teletype}• Shorthand notation: {\it words in italic}• The verbatim environment create a teletype section

in which no escaping is necessary:\begin{verbatim}\ ∼{}\end{verbatim}

17

Page 18: Introduction to LaTeX - people.cs.aau.dkpeople.cs.aau.dk/~htj/work/csintro06-latex.pdf · • The package fancyheadings has some fancy styles, e.g., fancy, fancyplain ... • Beamer

Exercise 4

• Insert italic, bold, and teletype text into yourdocument

• Insert a verbatim section in your document

18

Page 19: Introduction to LaTeX - people.cs.aau.dkpeople.cs.aau.dk/~htj/work/csintro06-latex.pdf · • The package fancyheadings has some fancy styles, e.g., fancy, fancyplain ... • Beamer

Lists

• To create a numbered list:

\begin{enumerate}\item thing\item object

\end{enumerate}

• To create a bulleted list use itemize, instead ofenumerate.

• Also possible to create a description lists, see theLATEXcheat sheet for details.

19

Page 20: Introduction to LaTeX - people.cs.aau.dkpeople.cs.aau.dk/~htj/work/csintro06-latex.pdf · • The package fancyheadings has some fancy styles, e.g., fancy, fancyplain ... • Beamer

Exercise 5

• Create a numbered list in your document

• Create a bulleted list in your document

20

Page 21: Introduction to LaTeX - people.cs.aau.dkpeople.cs.aau.dk/~htj/work/csintro06-latex.pdf · • The package fancyheadings has some fancy styles, e.g., fancy, fancyplain ... • Beamer

Labels and References

• Used for referencing within the document• Example:

\section{Introduction}\label{sec:introduction}

\ref{sec:introduction}\pageref{sec:introduction}

• \ref refers to section or figure number• \pageref refers to page number• It is custom to use cha prefix for chapters, sec for

section, and fig for figures.• Note: You will often need to compile the document

two or three times to get page references right.

21

Page 22: Introduction to LaTeX - people.cs.aau.dkpeople.cs.aau.dk/~htj/work/csintro06-latex.pdf · • The package fancyheadings has some fancy styles, e.g., fancy, fancyplain ... • Beamer

Exercise 6

• Create a label for a section and create a reference,and a page reference.

22

Page 23: Introduction to LaTeX - people.cs.aau.dkpeople.cs.aau.dk/~htj/work/csintro06-latex.pdf · • The package fancyheadings has some fancy styles, e.g., fancy, fancyplain ... • Beamer

Figures 1/2

• The graphic and graphicx packages provides a simplemethod for including figures:

\includegraphics{myfigure.eps}

• Scaling:

\includegraphics[width=6cm]{myfigure.eps}

• Alternatively, it is possible to use:

\begin{figure}... \end{figure}

• Can do more (e.g., better placement control)• Usually \includegraphics is enough

23

Page 24: Introduction to LaTeX - people.cs.aau.dkpeople.cs.aau.dk/~htj/work/csintro06-latex.pdf · • The package fancyheadings has some fancy styles, e.g., fancy, fancyplain ... • Beamer

Figures 2/2

• Bitmap files (bmp, gif, jpg, png, etc.) tend to lookbad when printed. Use vector graphics wheneverpossible.

• For postscript files eps is preferred, for pdf, includeother pdf documents.

• If you exclude the dot-suffix (e.g., .eps) in\includegraphics, LATEXwill look for eps forpostscript, and pdf for pdf documents.

24

Page 25: Introduction to LaTeX - people.cs.aau.dkpeople.cs.aau.dk/~htj/work/csintro06-latex.pdf · • The package fancyheadings has some fancy styles, e.g., fancy, fancyplain ... • Beamer

Exercise 7

• Insert a figure into your document.

• Scale it

• Create a label for the figure, and refer to it

• Create an eps and pdf version of the figure and makesure the document can be compiled to bothpostscript and pdf

• Hint: Use epstopdf

25

Page 26: Introduction to LaTeX - people.cs.aau.dkpeople.cs.aau.dk/~htj/work/csintro06-latex.pdf · • The package fancyheadings has some fancy styles, e.g., fancy, fancyplain ... • Beamer

Table of Contents

• LATEXcan generate this automatically• Insert the following in your master file:

\tableofcontents

• Usually followed by:

\cleardoublepage

• The level of section inclusion can be set. E.g.,:\setcounter{tocdepth}{1} includes parts,chapters, and sections.

• Note: As a table of contents is build using pagereferences, LATEXshould be run several times, usuallytwo, sometimes three.

26

Page 27: Introduction to LaTeX - people.cs.aau.dkpeople.cs.aau.dk/~htj/work/csintro06-latex.pdf · • The package fancyheadings has some fancy styles, e.g., fancy, fancyplain ... • Beamer

Exercise 8

• Create a table of content for your document

• Remove subsection entries from the table of content

27

Page 28: Introduction to LaTeX - people.cs.aau.dkpeople.cs.aau.dk/~htj/work/csintro06-latex.pdf · • The package fancyheadings has some fancy styles, e.g., fancy, fancyplain ... • Beamer

latexmk

• You can skip this part of you are afraid of Unix

• Or if you do not want to make your life easier

• latexmk is a Perl script which can run LATEXtheright amount of times to get pagerefs correct.

• Usage: latexmk file.tex (produces dvi output)

• Or: latexmk -pdf file.tex for pdf output

• If you use Unix you can copy the script to your PC

28

Page 29: Introduction to LaTeX - people.cs.aau.dkpeople.cs.aau.dk/~htj/work/csintro06-latex.pdf · • The package fancyheadings has some fancy styles, e.g., fancy, fancyplain ... • Beamer

Exercise 9

• Use latexmk to compile your document to dvi andpdf

29

Page 30: Introduction to LaTeX - people.cs.aau.dkpeople.cs.aau.dk/~htj/work/csintro06-latex.pdf · • The package fancyheadings has some fancy styles, e.g., fancy, fancyplain ... • Beamer

Math Mode

• LATEXrocks at math

• Math mode can be entered in two ways:• Easy: $ math mode $• Longer:

\begin{equation}math mode\end{equation}

• Example: $ dˆ2\sqrt{2\pi} $ produces d2√

• The LATEXcheat sheet is handy when writing math

30

Page 31: Introduction to LaTeX - people.cs.aau.dkpeople.cs.aau.dk/~htj/work/csintro06-latex.pdf · • The package fancyheadings has some fancy styles, e.g., fancy, fancyplain ... • Beamer

Exercise 10

• Create the following formula in your document:e iπ + 1 = 0d = b2 − 4ac , x1 = −b+

√d

2a

31

Page 32: Introduction to LaTeX - people.cs.aau.dkpeople.cs.aau.dk/~htj/work/csintro06-latex.pdf · • The package fancyheadings has some fancy styles, e.g., fancy, fancyplain ... • Beamer

Page Styles

• Changing the layout style in LATEXis easy

• The page style is set like this: \pagestyle{plain}• Page style can be set anywhere in the document, and

will be style of the following pages, until redefined

• Build-in page styles are: plain, empty, headings,myheadings

• The package fancyheadings has some fancy styles,e.g., fancy, fancyplain

• \thispagestyle set the style for current page only

32

Page 33: Introduction to LaTeX - people.cs.aau.dkpeople.cs.aau.dk/~htj/work/csintro06-latex.pdf · • The package fancyheadings has some fancy styles, e.g., fancy, fancyplain ... • Beamer

Exercise 11

• Set your page style to fancy• Be sure to include the fancyheadings package

• Now set it back, because fancy looks silly

33

Page 34: Introduction to LaTeX - people.cs.aau.dkpeople.cs.aau.dk/~htj/work/csintro06-latex.pdf · • The package fancyheadings has some fancy styles, e.g., fancy, fancyplain ... • Beamer

Page Numbering

• Page numbering is usually done automatically

• But sometimes this does not fit

• Resetting the current page number to 1:\setcounter{page}{1}

• It is also possible to change the page number style:\pagenumbering{roman}.arabic is default numbering style.

34

Page 35: Introduction to LaTeX - people.cs.aau.dkpeople.cs.aau.dk/~htj/work/csintro06-latex.pdf · • The package fancyheadings has some fancy styles, e.g., fancy, fancyplain ... • Beamer

Exercise 12

• Set your page numbering to start with one in theintroduction

• Use roman numerals to number the pages before theintroduction

35

Page 36: Introduction to LaTeX - people.cs.aau.dkpeople.cs.aau.dk/~htj/work/csintro06-latex.pdf · • The package fancyheadings has some fancy styles, e.g., fancy, fancyplain ... • Beamer

BibTEX

• The way to do citations in LATEX

• Have one file with all books, articles, etc.• Can be shared across several documents

• Only includes the cited works in the report

• BibTEXautomatically sorts the entries by last name

• To use BibTEX:\bibliographystyle{plain} in the preamble\bibliography{bibliography} in the masterAnd have a file called bibliography.bib with yourbibliography.

• Then use \cite{cite entry} to cite.

36

Page 37: Introduction to LaTeX - people.cs.aau.dkpeople.cs.aau.dk/~htj/work/csintro06-latex.pdf · • The package fancyheadings has some fancy styles, e.g., fancy, fancyplain ... • Beamer

Exercise 13

• Fetch the example bibliography.bib from thecourse web page.

• Cite the entry in your document, set it up for usingBibTEX, and compile

• Note: It is a good idea to use ∼, instead of a spacebefore \cite.This creates a space which cannot be line breaked.

37

Page 38: Introduction to LaTeX - people.cs.aau.dkpeople.cs.aau.dk/~htj/work/csintro06-latex.pdf · • The package fancyheadings has some fancy styles, e.g., fancy, fancyplain ... • Beamer

Title Page

• The title page is the thing that goes between thefront page and the preface

• Has to look a certain way

• There is a LATEXversion of this available at the coursehomepage.

38

Page 39: Introduction to LaTeX - people.cs.aau.dkpeople.cs.aau.dk/~htj/work/csintro06-latex.pdf · • The package fancyheadings has some fancy styles, e.g., fancy, fancyplain ... • Beamer

Exercise 14

• Download the example title page from the courseweb page

• You need title.tex and logo.eps

• Edit the file to reflect your project (or somethingelse) and insert it into your document

• You should have a file called synopsis.tex whichcontains your synopsis (or some garbage text).

39

Page 40: Introduction to LaTeX - people.cs.aau.dkpeople.cs.aau.dk/~htj/work/csintro06-latex.pdf · • The package fancyheadings has some fancy styles, e.g., fancy, fancyplain ... • Beamer

Getting Further with LATEX

• Print out the LATEXcheat sheet:• http://www.stdout.org/~winston/latex/

• The “LaTeX Companion” is a good book

• But just use LATEXand you will get better

40

Page 41: Introduction to LaTeX - people.cs.aau.dkpeople.cs.aau.dk/~htj/work/csintro06-latex.pdf · • The package fancyheadings has some fancy styles, e.g., fancy, fancyplain ... • Beamer

LATEXBeyond Reports

• Beamer• Can create very nice slides• http://latex-beamer.sourceforge.net/• This presentation was created with it• AAU theme: http://www.cs.aau.dk/~ltm/

• Short Documents• Will rid you of the feeling that LATEXis complicated• And documents just look way better in LATEX

41

Page 42: Introduction to LaTeX - people.cs.aau.dkpeople.cs.aau.dk/~htj/work/csintro06-latex.pdf · • The package fancyheadings has some fancy styles, e.g., fancy, fancyplain ... • Beamer

Summary

• Hopefully you will know a bit more LATEXnow

• Ask questions if there is anything more you wouldlike to know

• Anything on these slides can be found in less thanfive minutes using google

• Now, go drink beer!

42