LaTeX Tutorial

From Music Tech

Materials

Examples: http://www.music.mcgill.ca/~zadel/latexdocs/latexworkshopexamples.zip

Collection of useful latex documentation: http://www.music.mcgill.ca/~zadel/latexdocs/latexworkshopdocs.zip

Notes

goal

  • teach you what latex is
  • teach you to write a simple latex file, compile it and view it
  • expose you to latex functionality that'll be helpful for your academic writing
  • show you where to go as a next step

tex history

  • don knuth, stanford
  • created tex language for typesetting technical documents
  • started in 1977; first released in 1982 and revised in 1989

what is it?

  • a typesetting language
  • also, a macro processing programming language
  • it's a typesetting compiler
    • give it a document with text and markup, it produces a printable graphic file
  • it's not wysiwyg, but more like writing html

what is latex?

  • latex -> leslie lamport
  • it's a macro package that sits on top of tex
  • features:
    • cross-references
    • footnotes
    • glossary
    • table of contents
    • bibliography
  • separates content from formatting (like html + css)
  • automates all of the formatting, so you just have to give the semantics
    • you don't usually have to decide how to format something; you indicate what it is and latex knows how to format it
  • many style files available

why bother?

  • lots of propaganda available -- won't go into it
  • it's stable, well designed, popular in scientific circles, open source
  • it's not for everyone, but you might as well try it out

why its name is printed all screwy like that

  • emphasize typesetting
  • emphasize the fact that it's for technical writing
  • tau epsilon chi

where do you get latex?

how does latex run?

  • start with .tex file
  • latex it
  • produces .dvi, .aux, .log files

that's great, but how do I make it easier?

  • texshop
  • does syntax highlighting, push button compiling...
  • it's installed in mactex

basic format

  • documents in two parts
    • preamble
    • text
  • your input document needs to have these elements:
  \documentclass{article}
  \begin{document}
  \end{document}
  • then you just type in your text between the begin and the end
  • the document preamble (included packages, settings, etc) goes between the documentclass and the begin

basic typesetting

  • text (just type it)
  • paragraphs -- blank line
  • comments -- percent sign: %
  • spaces -- they're collapsed

special characters that latex uses

 # $ % ^ & _ { } ~ \
  • you'll have to escape these to use them in your document
 \# \$ \% \^{} \& \_ \{ \} \~ \\

latex commands

  • they start with a backslash, and sometimes take braces after them
  • eg \emph{this is emphasized}
  • subtle note: commands need to be broken from subsequent text, and consume space after them

iterative editing

  • edit
  • compile
  • refresh viewer
  • repeat

line and page breaking

  • \\ or \newline
  • \newpage
  • \linebreak -- not a new line, just a break -- latex still tries to justify the line

special characters

  • `` '' for double quotes (two backticks, two forward ticks)
  • dashes: -- en dash, --- em dash, $-$ minus sign
  • tilde: $\sim$
  • ellipsis: \ldots
  • accents:
  H\^otel, na\"\i ve, \'el\`eve,\\
  sm\o rrebr\o d, !`Se\~norita!,\\
  Sch\"onbrunner Schlo\ss{}
  Stra\ss e

emphasis

  • \emph{}
  • \underline{}

lists

  • these are called environments -- put latex in a different mode between \begin and \end
  • \begin{enumerate}
  • \begin{itemize}
  • \begin{description}

tabular

 \begin{tabular}{rl}
 7C0 & hexadecimal \\
 3700 & octal \\
 11111000000 & binary
 \end{tabular}

other environments

  • quotations
  • abstract
  • verbatim

other things

  • ~ is a space that can't be enlarged and prevents a line break, eg: M.~Zadel

mathematics

  • math mode vs text mode -- input interpreted differently
  • inline
    • \( \)
    • $ $
    • \begin{math} \end{math}
  • display
    • \[ \]
    • \begin{displaymath} \end{displaymath}
  • these two typeset things differently
    • display mode puts the equation on its own line, and it's more spaced out
  • numbered equations
    • \begin{equation} \end{equation}
  • math mode assumptions
    • spaces not significant
    • empty lines not allowed
    • each char is assumed to be the name of a variable
  • {} for grouping
  • _ is subscript
  • ^ is superscript
  • greek letters: need special commands (see docsS)
  • square root: \sqrt{}
  • fraction: \frac{}{}
  • vector: \vec a
  • mult dot: \cdot
  • sums + integrals:
    • \sum_{i=1}^{n} \qquad
    • \int_{0}^{\frac{\pi}{2}} \qquad
    • \prod_\epsilon
  • brackets:
    • \left( \right)
    • \left[ \right]
  • \ldots, \cdots

the basic latex document classes

  • the documentclass command indicates which style you're using
  • article -- shorter articles
  • report -- longer articles (including theses)
  • book -- real book
  • slides -- slides -- never used this one
  • or custom styles -- eg conference formatting -- will talk about this later

arguments to the article class

  • 10pt, 11pt, 12pt
  • onecolumn twocolumn
  • twoside
  • a4paper, letterpaper
  • titlepage, notitlepage

title elements

  • \title{}
  • \author{}
  • \date{} -- optional -- it'll put in today's date if you leave this out
  • \maketitle

sectioning commands

  • \section{}
  • \subsection{}
  • \chapter{}
  • \section*{}, etc.

tables of contents:

  • \tableofcontents
  • need to run latex twice for this

bibliographies w/o bibtex

  • the argument to \begin{thebibliography}{arg} is the width of the label (for lining things up)
  Partl~\cite{pa} has
  proposed that \ldots
  ...
  \begin{thebibliography}{99}
  \bibitem{pa} H.~Partl:
  \emph{German \TeX},
  TUGboat Volume~9, Issue~1 (1988)
  \end{thebibliography}

bibliographies w/ bibtex

  • make a bibtex file
  • add \cite{} commands in text that refer to the keys in your bibtex file
  • put this at the bottom
 \bibliographystyle{plain}
 \bibliography{mybibfilenamewithouttheextension}
  • compiling:
    • latex document
    • bibtex document
    • latex document
    • latex document
  • entry types:
    • article, book, inbook, inproceedings, misc, phdthesis, unpublished
      • each has mandatory and optional fields*
  • field types:
    • address, author, booktitle, chapter, howpublished, journal, number, pages, publisher, title, url, volume, year, ...plus lots of others
  • lots of helper programs (bibdesk, endnote, jabref, zotero, ...)
  • some sites (eg acm) output things in bibtex format for you
  • use {} to suppress bibtex handling of things
    • for example, if bibtex is making a word in your title lowercase, put the word in braces {} and bibtex won't touch it
  • the four standard bibtex styles are plain, abbrv, alpha, unsrt

cross references

  • \label{marker} \ref{marker}
  • need to run latex twice to resolve these references
  • things that you can add labels to
    • figures
    • sections
    • equations
    • bibliography entries
    • ...

floats

  • \begin{figure}
  • \begin{table}
  • placement: htbp!
  • \caption{my caption}

graphics + floats

  • \usepackage{graphicx}
  • \includegraphics{file}
  • can add a label, too
  • formats allowed:
    • latex: ONLY eps files
    • pdflatex: png, jpg, pdf (NO eps)

graphics example

 \begin{figure}
 \centerline{
 \includegraphics[keepaspectratio,height=2.5in]{livesessionview}
 }
 \caption{\emph{Ableton Live}'s session view}
 \label{livesessionview}
 \end{figure}

changing the default font

footnotes

  • \footnote{bahlblah}

how can I avoid dvi files?

  • use pdflatex -> goes directly to pdf
  • also
    • dvips -> ps2pdf
    • dvipdf
  • texshop uses pdflatex to make pdfs

conference style files

getting and installing packages

  • download the .cls file and put it next to your .tex source file

some useful packages

  • amsmath
  • comment
  • fancyhdr
  • float
  • fullpage
  • geometry
  • graphicx
  • hyperref
  • sectsty
  • setspace
  • subfigure
  • tocloft
  • url
  • ...and many others

some useful bibliography packages

  • natbib
  • chicago
  • ...and many others

including other files

  • \include{filename}

what to do if you run into a problem?

  • compiler shows an error and tells you where it's happening (l.linenumber)
  • there's probably a package to do what you want already, and it's probably already installed

the things that latex can do that I haven't gotten into

  • list of figures
  • list of tables
  • ...

Resources