Math 442 - Introduction to LATEX Glenn Lahodny Jr. 1 Introduction In this course, we will be using LATEX to create documents and presentations. 1.1 Getting Started with LATEX There are several TEX editors (TeXworks, Kile, LyX) available in the calclabs at Texas A&M University. In class, we will be using TeXworks. 1.1.1 How to Start TeXworks in a Calclab Room To start TeXworks in a calclab room (e.g. BLOC 122) 1. Log in to your calclab account using your NetID and password. 2. Click Start, Office, TeXworks. A schedule of the calclab rooms can be found at http://calclab.math.tamu.edu/. 1.1.2 How to Start TeXworks in an Open Access Computer Lab You can access your calclab account from any open access lab with the NX client. 1. Log in to your calclab account using your NetID and password. 2. Click Start, All Programs, Communications, NX Client for Windows. 3. Choose calclab1.math.tamu.edu as your host. In setting up this connection you can create an icon on the desktop that will automatically connect to your calclab account. 1.1.3 LATEX on your Personal Computer There are many TEX editors (e.g. TeXworks, Kile, TeXnic Center) available for free online. Each of these editors requires you to first install MiKTEX. 1. Install MiKTEX from http://.miktex.org/. 2. Choose which TEX editor you would like to use on your personal computer and download that editor from their website. Most people should have no trouble installing MiKTEX. However, you may have some issues with getting the editor up and running. I suggest using Google to search for instructions or a video on how to install a given editor. 1 2 2.1 Text and Symbols Quotation Marks To make properly formatted quote in LATEX, use two left single quotes (‘) and two right single quotes (’). For example, the command ‘‘This is a quote!’’ gives “This is a quote!” 2.2 Hyphens and Dashes There are two lengths for hyphens: -and –. The first is used for compound words such as father-in-law, and produced by -. The second is used for ranges of numbers such as pages 1–10, and produced by --. A third type of dash is the minus sign −, which is given by $-$. 2.3 Command Characters The characters #, $, &, , %, { } are interpreted as commands. To print these characters as text, use a command of \ followed by that character. For example, \$ gives $. 2.4 Accented Letters Accented letters can be displayed by using the appropriate commands. To get the accented e in Poincaré, use \’e. Some other examples are Gödel, given by \"o or Øksendall, given by \{\O\}. A full list can be found in [1]. 2.5 Line Breaks Breaking text into lines is done automatically in LATEX. However, there are times when a line break must be forced. This can be done with the command \\ at the end of a line or paragraph. 2.6 Vertical and Horizontal Spacing It is possible to add extra vertical spacing between paragraphs using the commands \smallskip, \medskip, or \bigskip. To generate a specific amount of vertical space, such as 1 cm, use the command \vspace{1 cm}. The command \vfill can be used to fit the top and bottom of the text to the margins of the page. Horizontal spacing can be added using the commands \quad or \qquad. To generate a specific amount of horizontal space, such as 1 cm, use the command \hspace{1 cm}. The command \hfill can be used to fit the width of the text to the margins of the page. 2.7 Page Breaks Breaking text into pages is done automatically in LATEX. However, a page break can be forced with the commands \newpage or \eject. 2 3 Displaying Text 3.1 Changing Font Style The font size for a document can be set when specifying the document class. To create an article with 11 pt font, we use the command \documentclass[11 pt]article. 3.1.1 Choice of Font Size The font size can be changed within the text using commands such as \tiny, \small, \large, \Large, \LARGE, \huge, and \Huge. For example, {\tiny This text is tiny} gives This text is tiny and {\Large This text is large} gives This text is large. 3.1.2 Font Attributes Font attributes can be changed by using the appropriate LATEX commands. • The command {\bf This text is bold.} gives This text is bold. • The command {\it This text is italicized.} gives This text is italicized. • The command {\em This text is emphasized.} gives This text is emphasized. • The command {\tt This is typewriter text.} gives This is typewriter text. Note that there is little difference between the italicized and emphasized text. 3.1.3 Font Color The font color can be changed using the color package in LATEX. Once you have called the color package, use the syntax \textcolor{DesiredColor}{Some Text}. For example, \textcolor{red}{This text is red} produces This text is red. 3.2 Text Justification and Indenting 3.2.1 Centering and One-sided Justification Text can be centered or given a left/right justification by using the appropriate LATEX environment. • To center text, use the syntax \begin{center} This text should be centered. \end{center} • To produce text that is left justified, use the syntax \begin{flushleft} This text is left justified. \end{flushleft}. • To produce text that is right justified, use the syntax \begin{flushright} This text is left justified. \end{flushright}. 3 3.3 Lists There are two types of lists in LATEX. 3.3.1 Itemized List To produce an itemized list, use the syntax \begin{itemize} \item This is item one. \item This is item two. \item This is item three. \end{itemize} The commands above produce • This is item one. • This is item two. • This is item three. 3.3.2 Enumerated List To produce an enumerated list, use the syntax \begin{enumerate} \item This is item one. \item This is item two. \item This is item three. \end{enumerate} The commands above produce 1. This is item one. 2. This is item two. 3. This is item three. 3.3.3 Nested Lists Lists can be included within one another, to a depth of four levels. 1. This is level one. (a) This is level two. i. This is level three. A. This is level four. B. This is level four. ii. This is level three. (b) This is level two. 4 3.4 Printing Literal Text It may be necessary to print text exactly as it is typed, and in a typewriter font. Lines of computer code and LATEX input are examples of such text. This can be done with the environment \begin{verbatim} Text \end{verbatim}. 4 4.1 Mathematical Formulas Mathematical Environments Equations can be displayed within a line of text by using single dollar signs to begin and end the equation. For example, $f(x)=x+1$ produces f (x) = x + 1. An equation can also be centered on its own line using double dollar signs to begin and end the equation. For example, $$f(x)=x+1$$ produces f (x) = x + 1 A numbered equation can be displayed using the equation environment \begin{equation} f(x)=x+1 \end{equation} The above commands produce f (x) = x + 1 (1) Multiple equations can be displayed using the equation array environment \begin{eqnarray} f(x) &=& x+1 \\ g(x) &=& x+2 \end{eqnarray} The above commands produce f (x) = x + 1 g(x) = x + 2 The numbering can also be suppressed by using the syntax \begin{eqnarray*} f(x) &=& x+1 \\ g(x) &=& x+2 \end{eqnarray*} 5 (2) (3) Multiple equations can also be given a single number using the subequation environment. \begin{subequations} \begin{align} f(x) &= x+1 \\ g(x) &= x+2 \end{align} \end{subequations} The above commands produce f (x) = x + 1 g(x) = x + 2 4.2 (4a) (4b) Main Elements of Math Mode 4.2.1 Constants and Variables Constants and variables can be displayed by typing the appropriate numbers and letters in math mode. Other symbols that are available on the keyboard are + 4.2.2 - = < > / : ! ’ | [ ] ( ) Subscripts and Superscripts To make a subscript, we use an underscore. For example, $x j$ produces xj . To make a superscript, we use a caret. For example, $x^2$ produces x2 . If sub- and superscripts occur together, their order is not important. For example, $x_j^2$ and $x^2_j$ both produce x2j . If the superscript or subscript contains more than one character, the group of characters 2 must be enclosed in braces { }. For example, $e^{x^2-1}$ produces ex −1 . 4.2.3 Fractions Short fractions, especially within the text, are best represented using the slash character /, as in $(x+y)/2$ for (x + y)/2. For more complicated fractions, we use the command \frac{numerator}{denominator} For example, $\frac{x+y}{2}$ gives x+y . 2 To increase the size of this fraction, we can use the command \dfrac{}{}. For example, x+y $\dfrac{x+y}{2}$ gives . 2 If an equation is displayed on its own line, then \frac{x+y}{2} and \dfrac{x+y}{2} yield the same output x+y 2 6 4.2.4 Roots Roots√ can be displayed using the command \sqrt[n]{Argument}. For example, \sqrt[3]{x} 3 gives x. If the√optional argument n is omitted, then we get the square root. For example, \sqrt{x} gives x. 4.2.5 Sums Summation signs are made with the command \sum. P \sum {j=1}^n x j gives nj=1 xj . The appearance of summation signs can be improved using the \displaystyle command. \displaystyle\sum {j=1}^n x j gives n X xj . j=1 If a summation is displayed on its own line, then \displaystyle is not needed. 4.2.6 Integrals Integral signs are made with the command \int. Rb \int a^b f(x)dx gives a f (x)dx The appearance of integral signs can be improved using the \displaystyle command. Z b f (x)dx \displaystyle\int a^b f(x)dx gives a If an integral is displayed on its own line, then \displaystyle is not needed. To get a little extra spacing between the integrand f (x) and the differential operator dx, use the small spacing command \ . This improves the appearance of an integral: Z b Z b f (x)dx versus f (x) dx. a a 4.2.7 Limits Limits are displayed with the command \lim. \lim {x \to a}f(x) gives limx→a f (x) The appearance of limits within text can be improved using the \displaystyle command. \displaystyle\lim_{x\to a}f(x) gives lim f (x) x→a If a limit is displayed on its own line, then \displaystyle is not needed. 7 4.2.8 Continuation Dots Formulas occasionally contain a row of dots . . ., meaning and so on. Different types of continuation dots can be displayed. • The command \ldots gives . . . • The command \cdots gives · · · . • The command \vdots gives .. . • The command \ddots gives . . 4.3 4.3.1 Mathematical Symbols Greek Letters The Greek letters can be displayed in math mode by using the commands \alpha or \beta to produce α or β. Uppercase Greek letters can be displayed using similar commands such as \Gamma to produce Γ. 4.3.2 Calligraphic Letters Calligraphic letters can be displayed in math mode by using the command \mathcal{ }. For example, \mathcal{A,B,C} displays A, B, C. 4.3.3 Blackboard Bold Letters Blackboard bold letters can be displayed in math mode by using the command \mathbb{ }. For example, \mathcal{R,N,Q} displays R, N, Q. 4.3.4 Function Names To display functions such as sin(x) and cos(x), we use the commands \sin(x) and \cos(x). It is not difficult to guess the commands for tan(x), sec(x), ln(x), log(x), arctan(x), etc. 4.3.5 Mathematical Accents Several mathematical accents are available in math mode. • The command \hat{x} gives x̂ • The command \bar{x} gives x̄ • The command \dot{x} gives ẋ • The command \tilde{x} gives x̃ • The command \vec{x} gives ~x A full list can be found in [1]. 8 4.4 4.4.1 Additional Elements Automatic Sizing of Brackets To automatically size the bracket symbols in LATEX, use the syntax \left and \right before the brackets you want. For example, $$f(x) = \ln\left(\frac{x}{y+1}\right) gives x f (x) = ln . y+1 Sometimes an equation contains only a single opening or closing bracket without its counterpart. In this case, the \left and \right commands are still used, but with a period ‘.’ as an invisible bracket. For example, $$\int_1^2 x^2\ dx = \left.\frac{1}{3}x^3\right|_1^2 produces Z 1 4.4.2 2 2 1 3 x dx = x . 3 1 2 Text Within a Formula To include some normal text within an equation, use the command \text{Normal Text} or \mbox{Normal Text} along with the \quad or \qquad commands to achieve the desired spacing between the variables and text. For example, $$X=Y \qquad \text{iff} \qquad X\subseteq Y \quad \text{and} \quad X\supseteq Y$$ produces X=Y 4.4.3 X⊆Y if and only if and X ⊇ Y. Matrices and Arrays The following is an example of an array: a11 a12 · · · a1n a21 a22 · · · a2n .. .. .. ... . . . an1 an2 · · · ann Arrays are the basis for matrices and systems of equations. 9 Matrices and arrays can be produced using the array environment. The array environment is introduced with the syntax \begin{array}{Column Justification Options} \end{array} For example, $$\begin{array}{ccc} 1 & 2 & 3 \\ 4 & 5 & 6 \end{array}$$ produces 1 2 3 4 5 6 The syntax {ccc} means that the array will have three columns whose entries are centered. To give entries of a column a left or right justification, use ‘l’ or ‘r’ for that column. Note that row entries are separated by the & symbol and \\ is used at the end of each row. Matrices and can be produced in three ways: • Using the array environment. $$\left(\begin{array}{cc} 1 & 2 \\ 3 & 4 \end{array}\right)$$ • Using the pmatrix environment. $$\begin{pmatrix} 1 & 2 \\ 3 & 4 \end{pmatrix}$$ • Using the bmatrix environment. $$\begin{bmatrix} 1 & 2 \\ 3 & 4 \end{bmatrix}$$ The pmatrix environment produces a matrix in parentheses, while the bmatrix environment produces a matrix in brackets. 1 2 1 2 and 3 4 3 4 The array environment can be used for either, as well as determinants. 10 4.4.4 Multiline Equations A multiline equation is one that is developed over several lines [1]. To produce a multiline equation, use the syntax \begin{eqnarray} Left1 & Middle1 & Right1 \\ Left2 & Middle2 & Right2 \\ etc. \end{eqnarray} If equation numbers are not desired, use \begin{eqnarray*} and \end{eqnarray*}. For example, \begin{eqnarray*} (x+y)(x-y) &=& x^2-xy+xy-y^2 \\ &=& x^2-y^2 \\ (x+y)^2 &=& x^2+2xy+y^2 \end{eqnarray*} produces (x + y)(x − y) = x2 − xy + xy − y 2 = x2 − y 2 (x + y)2 = x2 + 2xy + y 2 5 5.1 Figures and Tables Figures Figures can be included in LATEX without interupting the text. This can be done with the environment \begin{figure}[Location] \includegraphics[Scale]{Figure Name} \label{Figure Label} \caption{Caption Text Here} \end{figure} The location of a figure can be specified by entering a single letter in the [Location] syntax above. For instance, h stands for “Here” and, provided there is space, the figure will appear exactly where specified. The letters t and b stand for “Top” and “Bottom” and the figure will be placed at the top or bottom of that page. The letter p stands for “Page” and the figure will be placed on its own page. 11 When using figures in a LATEX document, I suggest saving the figures as Encapsulated Post Script (.eps) files. In my experience, these figures look best when imported to a LATEX document. Other file types are permitted, but often appear low quality. Here is an example of a figure. \begin{figure}[htbp] \begin{center} \includegraphics[scale=0.5]{Fig1.eps} \end{center} \label{Figure1} \caption{This is an example figure.} \end{figure} This produces the following figure. Negative Binomial Model Host Parasite Population Size 400 300 200 100 0 0 10 20 30 40 50 Time Figure 1: This is an example figure. 5.2 Tables Tables can be included in LATEX without interupting the text. This can be done with the environment \begin{table}[Location] \begin{tabular}{Column Justification Options} \end{tabular} \label{Table Label} \caption{Caption Text Here} \end{table} 12 The location of a table can be specified by entering a single letter in the [Location] syntax above. The same syntax applies for tables as for figures. Here is an example of a table. \begin{table}[htbp] \caption{This is an example table.} \begin{center} \begin{tabular}{|l||c|r|} \hline Heading 1 & Heading 2 & Heading 3 \\ \hline \hline Entry 1 & Entry 2 & Entry 3 \\ \hline Entry 4 & Entry 5 & Entry 6 \\ \hline Entry 7 & Entry 8 & Entry 9 \\ \hline \end{tabular} \end{center} \label{Table1} \end{table} This produces the following table. Table 1: This is an example table. Heading 1 Entry 1 Entry 4 Entry 7 Heading 2 Entry 2 Entry 5 Entry 8 Heading Entry Entry Entry 3 3 6 9 References [1] H. Kopka and P.W. Daly, Guide to LATEX, Addison-Wesley (2004). 13