A Short Introduction to Making Documents using LaTeX (continued)


EXAMPLE 2: Now we walk you through creating a tex file for a simple math quiz for a calculus course. Using a text editor of your choice (we used pico before), let us create a tex file named 'quiz.tex'. Type the following text in 'quiz.tex' (or if you are lazy enough, then you can simply copy and paste the text into the text editor):

\documentclass[letterpaper]{article}
 
\begin{document}
 
\noindent
MATH 2A / Fall Quarter of 2006 \hfill September 19, 2006
 
\vspace{10pt}
\noindent
Last Name: \hfill First Name: \hfill SID: \hfill {}
 
\vspace{20pt}
\noindent
1. (4 points) Find the derivatives of the following functions:
\begin{enumerate}
\item[i)] $f(x) = \sin^2 x + \log x^3 - \ln \frac{x}{2}$
\item[ii)] $g(x) = \alpha \cot x + \sqrt{x} + \sqrt[7]{x^{x+1}}$, where $\alpha \neq 0$
\end{enumerate}
 
\vspace{100pt}
\noindent
2. (3 points) Evaluate the following integral:
$$ \int_0^{\infty} e^{x^2} \, dx \,\,\,\,\,\, \mbox{or} \,\,\,\,\,\, \int_0^{\infty} \exp(x^2) \, dx $$
 
\vspace{100pt}
\noindent
3. (3 points) Prove that $\sin x \le x - \frac{x^3}{3!}$ for all real numbers $x$.
 
\end{document}

Now if we process the tex file 'quiz.tex' using 'pdflatex', then we will produce the PDF file 'quiz.pdf'. Let us explain the tex file line by line so that we grasp how tex file works. Basically whatever appears between \begin{document} and \end{document} will appear in the finished document, i.e. 'quiz.pdf' in the example, but there are also LaTeX commands which give special instructions. Commonly a LaTeX command starts with the backslash '\' and is followed by the command name (sometimes followed by the argument either in the pair of curly brackets '{ }' or the pair of square brackets '[ ]').

By default LaTeX would give an indentation for every paragraph, so if we don't want the indentation, we have to suppress by using the command \noindent for each paragraph. Paragraphs are separated by leaving an empty line between paragraphs in the tex file. In the line MATH 2A / Fall Quarter of 2006 \hfill September 19, 2006, the command \hfill fills the gap horizontally. Hence, in the document the part MATH 2A / Fall Quarter of 2006 will be left-justified and the part September 19, 2006 wiil be right-justified. The command \vspace{10pt} (for vertical space) will give a line (10pt is the default font size), and this command is an example of the command that requires an argument in a pair of curly brackets. Note that in the PDF file there is a line between the line that starts with MATH 2A and the line that starts with Last Name:. Note that for the line Last Name: \hfill First Name: \hfill SID: \hfill {} the command \hfill is used three times. The command \vspace{20pt} will skip the line twice since each line is sized 10pt.

The command \begin{enumerate} starts a list which is enumerated. The list must end with the command \end{enumerate}. In fact, the commands \begin and its pair \end are used not only for the enumerated list but also many other instructions such as array, table, matrix, and etc. Each item of the list starts with the command \item followed by an argument in a pair of square brackets. Here we used roman numerals to enumerate the list. If the argument and the pair of square brackets are missing, then LaTeX will enumerate with arabic numbers. If only the argument is missing, then the list becomes unenumerated.

In the line that starts with \item[i)], we have a mathematical expression which is enclosed in a pair of dollar signs, which is required for mathematical expression. The expression enclosed in a pair of dollar signs is said to be in the math mode. LaTeX requires some commands to be only used in the math mode. The command \sin, for instance, must be in the math mode. If tex file is created with the command \sin which is not enclosed in a pair of dollar signs, then LaTeX will complain. Later we will give a list of mathmatical symbols with their corresponding commands. The wedge character '^' is used to express the exponent. For instance, \sin^2 will give us a sine function raised to the second power. If the exponent involves more than one character, then the exponent has to be enclosed in a pair of curly brackets. For example, x^{x+1} as in the line that starts with \item[ii)]. The command \frac{x}{2} will give us a fraction with the numerator x and the denominator 2. In general, \frac{numerator}{denominator}. Many of LaTeX commands can be nested. Hence, we would obtain what we expect if \frac{\frac{1}{2}}{3} or x^{x+y^{z+2}} is used (just make sure that pairs of brackets are matching). The command \alpha will give the Greek letter alpha. The command \sqrt{x} will give us a square root of x. If we want 7-th root of x, then we can use \sqrt[7]{x}. The command \neq gives the not-equal sign.

For the second problem, the mathematical expression is enclosed in a pair of double dollar signs (not just a pair of dollar signs). This math mode (with double dollar signs) will be center-justified and expand the mathematical expressions vertically if necessary. Using a pair of dollar signs will force the expression to be fitted in 10pt, but using a pair of double dollar signs will not limit the vertical expansion. The following table will compare two math modes:

$ $ is used $$ $$ is used

The command \, will give a small gap (to be honest I don't know how much gap, but usually \,\, will give a gap of a letter). The command \int will give an integral sign. The lower and upper limits can be specified by \int_{lower limit}^{upper limit}. If they are just single characters, then a pair of curly brackets can be omitted as it was done in \int_0^{\infty}.

Inside the math mode, any white space will be ignored. Hence, $This is a sentence$ would be manifested as "Thisisasentence" in the PDF document, but often times we need to write nonmathematical content within the math mode. In such a case we can use \mbox{text goes here}. For instance, '\mbox' command is useful in the following case: $Q = \{ x \in R : \mbox{$x$ can be expressed as a ratio of two integers (if valid)} \}$ will give

The command \{ will give the left curly bracket '{'. Similarly the command \} will give the right curly bracket '}'. Finally the command \le will give the less-than-or-equal-to sign. Similarly, the command \ge will give the greater-than-or-equal-to sign.

As we have seen from 'quiz.tex', it is not difficult to create a document using LaTeX. Looking at the PDF file 'quiz.pdf', we wonder if we can improve the style of the document in few aspects so that the document looks even better. First of all, the outer margins are too big so that we are wasting space. Secondly, for the quizzes we can lose the page number. Lastly, it would be nice if we can emphasize the course title with bigger font size. For the following example, we will try to achieve these improvements.


EXAMPLE 3: Consider the following tex file 'quiz_improv.tex':

\documentclass[letterpaper]{article}
 
\pagestyle{empty}
 
\topmargin 0.0in
\oddsidemargin 0.0in
\textwidth 6.5in
\textheight 9.0in
\headheight 0.0in
\headsep 0.0in
 
\begin{document}
 
\noindent
\Large
\textbf{
MATH 2A} \normalsize / Fall Quarter of 2006 \hfill September 19, 2006
 
\vspace{10pt}
\noindent
Last Name: \hfill First Name: \hfill SID: \hfill {}
 
\vspace{20pt}
\noindent
1. (4 points) Find the derivatives of the following functions:
\begin{enumerate}
\item[i)] $f(x) = \sin^2 x + \log x^3 - \ln \frac{x}{2}$
\item[ii)] $g(x) = \alpha \cot x + \sqrt{x} + \sqrt[7]{x^{x+1}}$, where $\alpha \neq 0$
\end{enumerate}
 
\vspace{100pt}
\noindent
2. (3 points) Evaluate the following integral:
$$ \int_0^{\infty} e^{x^2} \, dx \,\,\,\,\,\, \mbox{or} \,\,\,\,\,\, \int_0^{\infty} \exp(x^2) \, dx $$
 
\vspace{100pt}
\noindent
3. (3 points) Prove that $\sin x \le x - \frac{x^3}{3!}$ for all real numbers $x$.
 
\vspace{250pt}
This sentence is inserted to see the bottom of the text. This sentence is inserted to see the bottom of the text.
This sentence is inserted to see the bottom of the text. This sentence is inserted to see the bottom of the text.
This sentence is inserted to see the bottom of the text. This sentence is inserted to see the bottom of the text.
This sentence is inserted to see the bottom of the text. This sentence is inserted to see the bottom of the text.
This sentence is inserted to see the bottom of the text. This sentence is inserted to see the bottom of the text.
 
\end{document}

The tex file is basically the same as the tex file 'quiz.tex'. However, there are four different parts (in blue font color). Compare the PDF file 'quiz_improv.pdf' with 'quiz.pdf'.

We could change the default font size by using the command \documentclass[letterpaper,12pt]{article} instead of \docuementclass[letterpaper]{article} at the beginning of the tex file. In this case, the normal size would be 12 points. The font size '11pt' can be used for 11 points as well, but apparently other possible sizes are not allowed. There are more commands for changing the font size besides '\Large', and here they are: '\tiny', '\scriptsize', '\footnotesize', '\small', '\normalsize', '\large', '\Large', '\LARGE', '\huge', and '\Huge'.


EXAMPLE 4: For this example, we will consider some more mathematical expression which require special packages to be loaded. Consider the following tex file 'math.tex': and its PDF file 'math.pdf':

\documentclass[letterpaper]{article}
 
\pagestyle{empty}
 
\usepackage{amssymb}
\usepackage{amsmath}
 
% USER DEFINED
 
\def\gap{\vspace{10pt}\noindent}
\def\rr{\mathbb{R}}
\newcommand{\norm}[1]{\| #1 \|}
\newcommand{\innprod}[2]{\langle #1, #2 \rangle}
 
\begin{document}
 
\noindent
The congruence symbol $\cong$ does not require an extra package, but
not-congruence symbol $\ncong$ would require the package `amssymb'.
 
\gap
It is not so hard to create a matrix:
$$ \begin{pmatrix}
1 & 0 & 1 \\
0 & 1 & 0
\end{pmatrix} $$
 
\gap
For every vector $x \in \rr^3$, we define the norm of $x$
by $\norm{x}^2 = \innprod{x}{x}$, i.e.
$$ \norm{x} = \sqrt{\innprod{x}{x}}. $$
 
\gap
Normal parentheses would not adjust their size to fit the
expression, but we have a solution for that, too:
$$ (\sum_{n=1}^{\infty} \frac{1}{n^s})^2 \,\,\,
\mbox{versus} \,\,\, \left(\sum_{n=1}^{\infty} \frac{1}{n^s}\right)^2 $$
 
\gap
Also,
$$ A = \{ \frac{m}{n} : m, n \in \rr \} \,\,\,
\mbox{versus} \,\,\, A = \left\{ \frac{m}{n} : m, n \in \rr \right\} $$
 
\gap
We can also define a function implicitly by
$$ f(x) = \left\{
\begin{array}{cl}
x - 1 & \mbox{if } x < 0 \\
x^2 & \mbox{if } 0 \le x < 1 \\
\sin x & \mbox{otherwise}
\end{array}\right. $$
 
\gap
We can also do the following:
\begin{eqnarray}
(x + y)^3 & = & (x + y)(x + y)(x + y) \\
          & = & (x^2 + 2xy + y^2)(x + y) \\
          & = & x^3 + 3x^2y + 3xy^2 + y^3
\end{eqnarray}
 
\gap
We could also do without the numberings:
\begin{eqnarray*}
(x + y)^3 & = & (x + y)(x + y)(x + y) \\
          & \le & (x^2 + 2xy + y^2)(x + y) \\
          & < & x^3 + 3x^2y + 3xy^2 + y^3 + 1
\end{eqnarray*}
 
\end{document}

Here are explanations for new features:


LAST REMARK: We finish the tutorial with the following remark. In fact, we could have completely achieved the same goal that has been described by the tutorial without connecting to the math account. That means we didn't have to deal with UNIX commands or SSH. If you are using Windows, then one can download MikTeX, an up-to-date TeX implementation for the Windows operating system, which is free and easy to use. The link is available from "An Unofficial UCI Math LaTeX Page" web site. Together with text editors supporting LaTeX (links for some of free editors are also available from "An Unofficial UCI Math LaTeX Page"), we can create and process tex files to create DVI, PS, and PDF document files. We will not explain the details, but if one is willing to follow the manual from MikTeX, then it will be quite easy. If you have questions regarding starting up your LaTeX experiences or comments about this tutorial, feel free to contact me via email (t c h o i @ m a t h . u c i . e d u).


© 2006 - 2008   Timothy Choi