Biostatistics Student Seminar An Introduction To L A T E X Josh Murray Osvaldo Espin-Garcia (edition) Dalla Lana School of Public Health University of Toronto
Introduction There are four stages to creating a document: 1 Text is entered and stored in the computer for corrections, extensions, deletions, etc... 2 The input text is formatted into lines of equal length and pages of a certain size 3 The output text is displayed on the computer screen 4 The final output is sent to a printer T E X, is a powerful computer programming language that focuses on step 2 above. L A T E X, is an extension that allows the user to create publication quality materials with T E Xcommands.
History Computer scientist Donald Knuth was frustrated with the errors that occurred when he sent his journal articles and books to be published. In 1978 wrote the typesetting program T E X so that anyone could create publication quality documents. In 1985 mathematician Leslie Lamport created an extension of T E X, called L A T E X, that focuses on document structure as opposed to the minute detail of T E X.
How do you use L A T E X? There are three steps to creating a publication quality document with L A T E X: 1 Enter text with L A T E X markup into a text editor and save it as a.tex file. (e.g. TeXworks, Tinn-R, TexnicCenter) 2 Send your.tex file to a program that can process L A T E X input. (MikTeX). This will create a file of your choice, PDF, DVI, or Postscript. 3 Open the file you created in a viewer (Adobe).
A Basic Document Every L A T E Xdocument must contain the following three components. \documentclass{article} %preamble \begin{document} %body Hello World! %body \end{document} The preamble. It tells L A T E X what kind of document to process. In the example above I chose article, but I could have chosen something else (e.g book, report, letter, or beamer). This is where you put all of the global commands that will appear in your document. The document s text is placed between the second and third item.
Sections L A T E X is very useful for breaking up your document into sections. Giving your document structure by adding logical sections makes it easier to read. A well structured document usually contains the following components: A title page A table of contents An Abstract Section headings Subsection headings Bibliography
Sections (cont d) A section can be declared in one of two ways: \section{section name} \section*{section name} Once you declare a section it is best to give it a label. This is done by adding the command \label{label name}. Once you have done this, you can reference your section by adding the command \ref{label name}.
Math Mode There are two main modes for processing commands in Latex. 1 Paragraph mode, which is the normal processing mode. 2 Math mode. Latex enters this mode when certain commands are encountered, telling Latex that what follows is a formula. When Latex is in math mode, spaces and blanks are ignored.
Math Mode (cont d) There are two ways to enter math mode. The first is within a paragraph by enclosing your math symbol or formula between two $ s. An example would be: The short hand symbol for summation is the capital Greek letter sigma, $\Sigma$. The short hand symbol for summation is the capital Greek letter sigma, Σ.
Math Mode (cont d) The second method way to enter math math is to place your formula between two $$ s. This will place your formula in a paragraph of its own: $$ f(x)=\frac{1} { \sqrt{2\ pi\sigma^{2} } } e ^{-\frac{(x-\mu) ^{2}}{2\sigma^{2}}} $$ f (x) = 1 (x µ)2 e 2σ 2 2πσ 2
Constants and Variables In Math Mode constants are typeset in Roman and variables are in italics. Spaces are ignored. z = 2a + 3y y = c{f [y, y(x)] + g(x)} M(s) < M(t) < M = m
Exponents and Indices \pi^{2} π 2 x^{5y + 3} x 5y+3 x_i x i a_{12} a 12 x^n_i x n i A_{i,j,k}^{n!2}?? A x 2 i j 2n m.n
Fractions Fractions can be displayed as x/y x/y \frac{x}{y} x y Both, \frac{x/y - z/y}{u/v -s/t}?? a x y + b x+y 1+ a b a+b
Roots Roots can be displayed as \sqrt{8} 8 \sqrt[3]{8}=2 3 8 = 2 \sqrt{x^2 + y^2 +2xy} x 2 + y 2 + 2xy \sqrt[3]{-q + \sqrt{q^2 +p^3}}?? SE( ˆ ) = 1 n 11 + 1 n 12 + 1 n 21 + 1 n 22
Sums and Integrals Summation and integration formulas are made with the commands \sum and \int: n i=1 b a n a x x=1 2 n b a i f i (x)g i (x) dx4 i=1 a More complex equations can be made by following the simple rules. \int\frac{\sqrt{(ax+b)^3}}{x} produces: (ax + b) 3 x
Arrays Arrays are used to create matrices, determinants, systems of equations and so on. An array begins with the command \begin{array}{justification} and ends with \end{array}. The justification consists of entering l for left, c for center, or r for right. You need to include a justification rule for every column that you include. The entries in each array are separated by an & and each line ends with \\.
Examples of arrays $$ \ l e f t ( \ begin { array } { r c l } 1 & 0 & 0 \ \ 0 & 1 & 0 \ \ 0& 0 & 1 \ \ \ end { array } \ r i g h t ) $$ 1 0 0 0 1 0 0 0 1 $$ \ l e f t ( \ begin { array } { c } \ l e f t \ begin { array } { c c } x _{11} & x _{12} \ \ x _{21} & x _{22} \ end { array } \ r i g h t \ \ x \ \ y \ end { array } \ r i g h t ) $$ x 11 x 12 x 21 x 22 x y
What code produces the following? F(x, y) = 0 and F xx F xy F x F yx F yy F y F x F y 0 = 0
Multiple Line Equations Equation deployed over several lines of code. They have the same form as an array. \begin{eqnarray} (x+y)(x-y) & = & x^2 - xy - y^2 \\ & = & x^2 - y^2 \\ (x+y)^2 & = & x^2 + 2xy + y^2 \end{eqnarray} x n u 1 + + x n+t 1 u t = x n u 1 + (ax n + c)u 2 + ( ) + a t 1 x n + c(a t 2 + + 1) = (u 1 + au 2 + + a t 1 u t )x n + h(u 1,..., u t ) u t
Tables Tables are created in Latex using the tabular environment. The basic syntax for a table in Latex is: \begin{tabular}{justification} r1c1 & r1c2 &... \\... \\ rnc1 & rnc2 &... \end{tabular} The justification is either l for left, c for center, or r for right. Each column entry is separated by an & and each row ends with \.
Example of a table The following code: \ begin { t a b l e } \ begin { t a b u l a r } { c c c c } \ hline \ hline B i r t h & \ multicolumn { 2 } { c } { Smoking Status } & \ \ \ cline {2 3} Weight & No & Yes & T o t a l \ \ \ hline Normal & $n_{11}$ & $n_{12}$ & $n _ { 1. } $ \ \ \ hline Low & $n_{21}$ & $n_{22}$ & $n _ { 2. } $ \ \ \ hline \ hline T o t a l & $n _ {. 1 } $ & $n _ {. 2 } $ & $n _ {.. } $ \ \ \ hline \ hline \ end { t a b u l a r } \ caption { $ 2 \ times2$ contingency t a b l e } \ end { t a b l e }
Example of a table (cont d) Produces the table: Birth Smoking Status Weight No Yes Total Normal n 11 n 12 n 1. Low n 21 n 22 n 2. Total n.1 n.2 n.. Table: 2 2 contingency table
Table: Summary of Categorical variables Distribution of Gender Gender n (%) Females 296 (58.04%) Males 214 (41.96%) Distribution of Age Spread n (%) Age grp 1 1 (0.21%) Age grp 2 92 (19.53%) Age grp 3 159 (33.76%) Age grp 4 219 (46.50%)
Special Characters List of Special Characters: $ = \$ & = \& % = \% # = \# _ =\_ { = \{ } = \}
Lists There are three types of lists available in L A T E X. \begin{itemize} list text \end{itemize} \begin{enumerate} list text \end{enumerate} \begin{description} list text \end{ description} To add a new bullet/number/description you call the command \item.
Example itemize The individual entries are indicated with a black dot, a so-called bullet, as the label The text in the entries may be of any length. The label appears at the beginning of the first line of text. Successive entries are separated from one another by additional vertical spaces.
Example of enumerate 1 The labels consists of sequential numbers 2 the numbering starts at 1 with every call to the enumerate environment
Sample description purpose This environment is appropriate when a number of words or expressions are to be defined example A keyword is used as the label and the entry contains a clarification or explanation other uses It may also be used as an author list in a bibliography
Online resources 1 http://en.wikibooks.org/wiki/latex 2 http://latex.wikia.com/wiki/main_page 3 http://www.ctan.org/tex-archive/info/ lshort/english/ 4 http://tex.stackexchange.com/