Introduction to Programming and Computing for Scientists · hyperrefwith pdftexwill make s with...

37
Introduction to Programming and Computing for Scientists Oxana Smirnova Lund University Tutorial 2a: writing a document using LaTeX Oxana Smirnova (Lund University) Programming for Scientists Tutorial 2a 1 / 37

Transcript of Introduction to Programming and Computing for Scientists · hyperrefwith pdftexwill make s with...

Page 1: Introduction to Programming and Computing for Scientists · hyperrefwith pdftexwill make s with clickable cross-references Tutorial 2a 8 / 37 . More useful packages Oxana Smirnova

Introduction to Programming and Computing for Scientists

Oxana Smirnova

Lund University

Tutorial 2a: writing a document using LaTeX

Oxana Smirnova (Lund University) Programming for Scientists Tutorial 2a 1 / 37

Page 2: Introduction to Programming and Computing for Scientists · hyperrefwith pdftexwill make s with clickable cross-references Tutorial 2a 8 / 37 . More useful packages Oxana Smirnova

You need a text editor and a LaTeX distribution• LaTeX files are simple ASCII files, like any program code

• They can be edited on any platform (Linux, Windows, anything) using any text editor

• These days everybody prefers to have the result in PDF• This was not the case 20 years ago, so by default LaTeX produces DVI files• All modern LaTeX systems can build PDF as well (pdflatex command in

Linux)

• There are different LaTeX distributions, all based on the same LaTeX2e version

• There are many packages not included in the typical distributions, but they always can be added later

Oxana Smirnova (Lund University) Programming for Scientists

Platform LaTeX distribution

Ubuntu, Debian texlive, texlive-base, texlive-full

RedHat, CentOS, Fedora, SuSE texlive, texlive-base, texlive-latex

Linux tetex – not supported since 2006

Windows MiKTeX

Mac OS MacTeX

Tutorial 2a 2 / 37

Page 3: Introduction to Programming and Computing for Scientists · hyperrefwith pdftexwill make s with clickable cross-references Tutorial 2a 8 / 37 . More useful packages Oxana Smirnova

Highly recommended way: use a LaTeX IDE• LaTeX IDEs can:

• Edit the text, highlighting elements and environments

• Assist in typing the environments and tags

• Offer menus for most common environments, tags, symbols etc

• Offer single-click interface to build and view LaTeX files

• Many such IDEs exist, today we will use Kile

• Find it in the menu, or type kile in “Run”

Oxana Smirnova (Lund University) Programming for Scientists Tutorial 2a 3 / 37

Page 4: Introduction to Programming and Computing for Scientists · hyperrefwith pdftexwill make s with clickable cross-references Tutorial 2a 8 / 37 . More useful packages Oxana Smirnova

Configure your Kile

Oxana Smirnova (Lund University) Programming for Scientists Tutorial 2a 4 / 37

Page 5: Introduction to Programming and Computing for Scientists · hyperrefwith pdftexwill make s with clickable cross-references Tutorial 2a 8 / 37 . More useful packages Oxana Smirnova

Configure your Kile: select Evince as PDF viewer

Oxana Smirnova (Lund University) Programming for Scientists Tutorial 2a 5 / 37

Page 6: Introduction to Programming and Computing for Scientists · hyperrefwith pdftexwill make s with clickable cross-references Tutorial 2a 8 / 37 . More useful packages Oxana Smirnova

Quick-start the new document using Kile Wizard

Oxana Smirnova (Lund University) Programming for Scientists Tutorial 2a 6 / 37

Page 7: Introduction to Programming and Computing for Scientists · hyperrefwith pdftexwill make s with clickable cross-references Tutorial 2a 8 / 37 . More useful packages Oxana Smirnova

We will make an article for a two-sided A4 printing

Oxana Smirnova (Lund University) Programming for Scientists

Don’t click OK yet, let’s go to the Packages tab

Tutorial 2a 7 / 37

Page 8: Introduction to Programming and Computing for Scientists · hyperrefwith pdftexwill make s with clickable cross-references Tutorial 2a 8 / 37 . More useful packages Oxana Smirnova

Let’s pick some useful LaTeX packages

Oxana Smirnova (Lund University) Programming for Scientists

hyperref with pdftex will make PDF files with clickable cross-references

Tutorial 2a 8 / 37

Page 9: Introduction to Programming and Computing for Scientists · hyperrefwith pdftexwill make s with clickable cross-references Tutorial 2a 8 / 37 . More useful packages Oxana Smirnova

More useful packages

Oxana Smirnova (Lund University) Programming for Scientists

graphicx with pdftex will allow to insert raster graphics (JPG, PNG etc)

Tutorial 2a 9 / 37

Page 10: Introduction to Programming and Computing for Scientists · hyperrefwith pdftexwill make s with clickable cross-references Tutorial 2a 8 / 37 . More useful packages Oxana Smirnova

And now some metadata

Oxana Smirnova (Lund University) Programming for Scientists

Now it is OK to click OK

Tutorial 2a 10 / 37

Page 11: Introduction to Programming and Computing for Scientists · hyperrefwith pdftexwill make s with clickable cross-references Tutorial 2a 8 / 37 . More useful packages Oxana Smirnova

Time to save the file

Oxana Smirnova (Lund University) Programming for Scientists Tutorial 2a 11 / 37

Page 12: Introduction to Programming and Computing for Scientists · hyperrefwith pdftexwill make s with clickable cross-references Tutorial 2a 8 / 37 . More useful packages Oxana Smirnova

And now let’s build it

Oxana Smirnova (Lund University) Programming for Scientists

Oops! Something’s

wrong!

Tutorial 2a 12 / 37

Page 13: Introduction to Programming and Computing for Scientists · hyperrefwith pdftexwill make s with clickable cross-references Tutorial 2a 8 / 37 . More useful packages Oxana Smirnova

Whatever, we don’t need this package, comment it out

Oxana Smirnova (Lund University) Programming for Scientists

Use % to comment (inactivate) any line in LaTeX; you can turn off many packages

Tutorial 2a 13 / 37

Page 14: Introduction to Programming and Computing for Scientists · hyperrefwith pdftexwill make s with clickable cross-references Tutorial 2a 8 / 37 . More useful packages Oxana Smirnova

Add some text, build and view the result

Oxana Smirnova (Lund University) Programming for Scientists Tutorial 2a 14 / 37

Page 15: Introduction to Programming and Computing for Scientists · hyperrefwith pdftexwill make s with clickable cross-references Tutorial 2a 8 / 37 . More useful packages Oxana Smirnova

Where’s the title? Let’s make it: \maketitle

Oxana Smirnova (Lund University) Programming for Scientists Tutorial 2a 15 / 37

Page 16: Introduction to Programming and Computing for Scientists · hyperrefwith pdftexwill make s with clickable cross-references Tutorial 2a 8 / 37 . More useful packages Oxana Smirnova

Would be nice to add e-mail, centered

• \begin{center}

\end{center}

this environment center-aligns everything inside it

• \texttt{…} tag applies equidistant “typewriter”-like font

Oxana Smirnova (Lund University) Programming for Scientists Tutorial 2a 16 / 37

Page 17: Introduction to Programming and Computing for Scientists · hyperrefwith pdftexwill make s with clickable cross-references Tutorial 2a 8 / 37 . More useful packages Oxana Smirnova

Time to add some sections and labels for cross-reference

Oxana Smirnova (Lund University) Programming for Scientists

• \section{name}

tag creates a numbered section

• \label{label-name}

tag sets a cross-reference target. Label name can be anything, people use prefixes like sec: or fig: to avoid name clashes

Tutorial 2a 17 / 37

Page 18: Introduction to Programming and Computing for Scientists · hyperrefwith pdftexwill make s with clickable cross-references Tutorial 2a 8 / 37 . More useful packages Oxana Smirnova

So how do we do cross-referencing?

Oxana Smirnova (Lund University) Programming for Scientists

• \ref{label-name}

a very smart tag that inserts numbering of the labelled environments and even creates clickable cross-references in PDF

Tutorial 2a 18 / 37

Page 19: Introduction to Programming and Computing for Scientists · hyperrefwith pdftexwill make s with clickable cross-references Tutorial 2a 8 / 37 . More useful packages Oxana Smirnova

Let’s add a picture

Oxana Smirnova (Lund University) Programming for Scientists

• Get yourself a penguin (or a cat) from Google

• \begin{figure}

\end{figure}

the environment for figures

• \includegraphics{…} inserts actual files

Tutorial 2a 19 / 37

Page 20: Introduction to Programming and Computing for Scientists · hyperrefwith pdftexwill make s with clickable cross-references Tutorial 2a 8 / 37 . More useful packages Oxana Smirnova

This looked ugly, let’s pin it and center it

Oxana Smirnova (Lund University) Programming for Scientists

• [h] means “here”; also exist H, t, b, p and ! (can be used together)

• LaTeX has its own ideas what means “here”: figures and tables are placed according to some optimal rules. Use “!” to override them (good luck…)

Tutorial 2a 20 / 37

Page 21: Introduction to Programming and Computing for Scientists · hyperrefwith pdftexwill make s with clickable cross-references Tutorial 2a 8 / 37 . More useful packages Oxana Smirnova

Every figure needs a caption

Oxana Smirnova (Lund University) Programming for Scientists

• \caption{text}

Inserts numbered caption; caption text can be formatted the same way as any other text (with few exceptions)

Tutorial 2a 21 / 37

Page 22: Introduction to Programming and Computing for Scientists · hyperrefwith pdftexwill make s with clickable cross-references Tutorial 2a 8 / 37 . More useful packages Oxana Smirnova

We also want to cross-reference figures

Oxana Smirnova (Lund University) Programming for Scientists

• \label{label-name}

and \ref{label-name} are used in the same way as for sections (and anything else)

Tutorial 2a 22 / 37

Page 23: Introduction to Programming and Computing for Scientists · hyperrefwith pdftexwill make s with clickable cross-references Tutorial 2a 8 / 37 . More useful packages Oxana Smirnova

Final adjustments

Oxana Smirnova (Lund University) Programming for Scientists

• \includegraphics[]{…} can take various options specified in []

• \textit{…} applies italic(slanted) font to the text

Tutorial 2a 23 / 37

Page 24: Introduction to Programming and Computing for Scientists · hyperrefwith pdftexwill make s with clickable cross-references Tutorial 2a 8 / 37 . More useful packages Oxana Smirnova

We have to cite bibliography reference now

Oxana Smirnova (Lund University) Programming for Scientists

• \cite{item-name}

inserts cross-reference to the named bibliography item

• \begin{thebibliography}

environment lists bibliography items

• Each item is defined via \bibitem{item-name}

• \url{…} creates clickable Web links in the PDF file

Tutorial 2a 24 / 37

Page 25: Introduction to Programming and Computing for Scientists · hyperrefwith pdftexwill make s with clickable cross-references Tutorial 2a 8 / 37 . More useful packages Oxana Smirnova

One small detail: non-breaking space

Oxana Smirnova (Lund University) Programming for Scientists

• Did you notice the annoying line break? ~ will create a non-breakable blank space

Tutorial 2a 25 / 37

Page 26: Introduction to Programming and Computing for Scientists · hyperrefwith pdftexwill make s with clickable cross-references Tutorial 2a 8 / 37 . More useful packages Oxana Smirnova

Make a new \subsectionand a table

Oxana Smirnova (Lund University) Programming for Scientists

• \begin{table} is very similar to \begin{figure}

• Tables themselves are defined by\begin{tabular}

• Each letter in {c|c} indicates a column; “c” means centered, can be also “l” or “r”

• & separates cells

• \\ ends rows

Tutorial 2a 26 / 37

Page 27: Introduction to Programming and Computing for Scientists · hyperrefwith pdftexwill make s with clickable cross-references Tutorial 2a 8 / 37 . More useful packages Oxana Smirnova

It was an ugly table, let’s make it slightly better

Oxana Smirnova (Lund University) Programming for Scientists

• \hline adds a horizontal border

• One can also use \cline{2-3} to have borders only for selected columns

Tutorial 2a 27 / 37

Page 28: Introduction to Programming and Computing for Scientists · hyperrefwith pdftexwill make s with clickable cross-references Tutorial 2a 8 / 37 . More useful packages Oxana Smirnova

A footnote

Oxana Smirnova (Lund University) Programming for Scientists

• \footnote{text} puts text in the automatically numbered footnote

Tutorial 2a 28 / 37

Page 29: Introduction to Programming and Computing for Scientists · hyperrefwith pdftexwill make s with clickable cross-references Tutorial 2a 8 / 37 . More useful packages Oxana Smirnova

Now let’s try mathematics in a new section

Oxana Smirnova (Lund University) Programming for Scientists

Noticed something

wrong?

Tutorial 2a 29 / 37

Page 30: Introduction to Programming and Computing for Scientists · hyperrefwith pdftexwill make s with clickable cross-references Tutorial 2a 8 / 37 . More useful packages Oxana Smirnova

Corrected mathematics text

Oxana Smirnova (Lund University) Programming for Scientists

• Group several characters in {}

• Always escape underscore with a backslash

• Even in URL links!

Tutorial 2a 30 / 37

Page 31: Introduction to Programming and Computing for Scientists · hyperrefwith pdftexwill make s with clickable cross-references Tutorial 2a 8 / 37 . More useful packages Oxana Smirnova

And now let’s try an equation

Oxana Smirnova (Lund University) Programming for Scientists

• \begin{equation} also needs a label, but needs no caption

• Equations are numbered automatically

Tutorial 2a 31 / 37

Page 32: Introduction to Programming and Computing for Scientists · hyperrefwith pdftexwill make s with clickable cross-references Tutorial 2a 8 / 37 . More useful packages Oxana Smirnova

Oh, that was also ugly. Fixing…

Oxana Smirnova (Lund University) Programming for Scientists

• \left( and \right) are some of the very many mathematical symbols in LaTeX

Tutorial 2a 32 / 37

Page 33: Introduction to Programming and Computing for Scientists · hyperrefwith pdftexwill make s with clickable cross-references Tutorial 2a 8 / 37 . More useful packages Oxana Smirnova

And finally, some bulleted lists

Oxana Smirnova (Lund University) Programming for Scientists

• \begin{itemize} creates a list of un-numbered items

Tutorial 2a 33 / 37

Page 34: Introduction to Programming and Computing for Scientists · hyperrefwith pdftexwill make s with clickable cross-references Tutorial 2a 8 / 37 . More useful packages Oxana Smirnova

There are also numbered lists

Oxana Smirnova (Lund University) Programming for Scientists

• \begin{enumerate} is similar to \begin{itemize}, only the items get numbered

Tutorial 2a 34 / 37

Page 35: Introduction to Programming and Computing for Scientists · hyperrefwith pdftexwill make s with clickable cross-references Tutorial 2a 8 / 37 . More useful packages Oxana Smirnova

Last, but not least: you can have unformatted text, too

Oxana Smirnova (Lund University) Programming for Scientists

• \begin{verbatim} is useful to type code; nothing inside is interpreted by LaTeX

Tutorial 2a 35 / 37

Page 36: Introduction to Programming and Computing for Scientists · hyperrefwith pdftexwill make s with clickable cross-references Tutorial 2a 8 / 37 . More useful packages Oxana Smirnova

And the result should look like this:

Oxana Smirnova (Lund University) Programming for Scientists Tutorial 2a 36 / 37

Page 37: Introduction to Programming and Computing for Scientists · hyperrefwith pdftexwill make s with clickable cross-references Tutorial 2a 8 / 37 . More useful packages Oxana Smirnova

Concluding notes• There are many more LaTeX tags and environments

• Those tags and environments we tried have many different options

• Every tag and environment can be modified and tailored to your needs

• There is no way you can remember all the tags; get yourself a book (many good books exist), or use any of the multiple online references

• Wikibooks: http://en.wikibooks.org/wiki/LaTeX

• LaTeX Reference Manual: http://home.gna.org/latexrefman/

• All serious scientific journals have official LaTeX templates and styles, usually complete with instructions

• Homework: upload the document (and the picture) you have just created to the Training portal, HW2A (http://training.lunarc.lu.se/mod/assign/view.php?id=338)

• If you did not finish it in today, please complete it at home

• Deadline: TOMORROW!

Oxana Smirnova (Lund University) Programming for Scientists Tutorial 2a 37 / 37