LaTex, and writing beautiful mathematics

• Introduction
• My first latex document
• compiling to a pdf
• error messages
• enumerated lists
• equations
• LaTex on the web in mathjax
• A simple html file
• Eq ua tions in a web page

History of LaTex

• In the 1960's there was roff, followed by troff and groff in the 80's
• In 1978, Donald Knuth introduce Tex (yes, THAT Knuth), which is freely available to all. There are still bounties waiting.
• Shortly after, Leslie Lamport extended Tex with LaTex macros and templates. Today, LaTex remains the baseline tool for preparation of mathematical and technical documents. (NOT WORD!!!)
• In the late 90's, the open-source program lyx became a popular free WYSIWYG gui for LaTex editing, though there are other options as well like kile and TeXworks (recommended).
• In 1998, MathML was recommended as web standard using XML. However, MathML has never caught on, and has been poorly supported since its creation for a number of reasons.
• In 2003, wikipedia adopted the Tex format for entering mathematical formulae. This informally established the use of LaTex on the Web. The tug-of-war between LaTex and MathML for web-publishing mathematics has pervented the emergence of a practical standard for two decades.
• In 2009, SIAM, AMS, and others created the MathJax for leveraging LaTex in combination with Javascript libraries to publish Math on the web. It's been very successful since it's introduction and is a practical emerging standard.

References

For practice, follow all of the directions below using commands in the terminal that we learned about yesterday. You may find it useful to use more than one terminal window (one for your text editor and one for shell commands).

1. Create a new directory on your Desktop called "LaTex".

3. Create a new file called "hello.tex" in your new directory. This will be your first LaTex document. LaTex files traditionally end with the ".tex" suffix.

The common work-flow for creating documents with Latex is

2. Compile your tex file with the pdflatex command.

3. If you get compile errors, fix them and recompile.

4. Inspect your pdf output in a pdf viewer.

This is the same kind of work-flow used in writing Fortran and C programs, so it is a good introduction. Here is how it goes...

4. Type the following code into your "hello.tex" file.

 1 2 3 4 5 6 7 8 9 10 11  \documentclass[]{article} \begin{document} hello world! My favorite book is Solaris by Stanislaw Lem \end{document}

Since LaTex is a "markup" language, which tells printers and other tools what you want your document to look like. None of the tags above beginning with a backslash will appear in your final pdf document.

5. At your shell command line, compile "hello.tex" using the following command.

        pdflatex hello.tex

This compiles your LaTex file. It creates a bunch of new files in the process.

• hello.pdf - this is the final document output

• hello.log - this is a log of the events that occurred during compilation, and is used for debugging

• hello.aux - this is a data file, used for tracking references to equations, figures, and bibliography citations. Latex is only a single-pass program, pdflatex has to be run 2 or 3 times to

• Sometimes there are other hello.* files, where * represents some other 3 letter suffix.

6. Use the Preview or Acrobat portable document format viewers to inspect your new "hello.pdf". Compare what you got out with what you put in and see if you understand what happened. How does the arrangement of the text you got out differ from what you put in?

7. Make a backup copy of the file "hello.pdf" for future reference.

8. Update "hello.tex" file to be the following.

    \documentclass[]{article}
\begin{document}
hello world!

\begin{enumerate}
\item
should be number one in the list
\item
should be number two in the list
\end{document}

Be sure to save your changes.

9. Compile "hello.tex" like you did above. This time, however, you are going to get an error. Read the error message, see if you can figure out what is wrong with your changes, and fix things.

10. Once you have fixed things, you can inspect "hello.pdf" to see how LaTex typesets an enumerated list. Notice that we did not have to do the numbering ourselves – LaTex handled all that.

11. Remove hello.log and hello.aux.

(you do not really need to do this - pdflatex with overwrite them as needed, but it is another chance to practice shell commands)

12. The reason we have used LaTex for decades is that it provides a free common platform for writting pretty mathematical equations, and that it allows us to focus on what we mean rather than what we see.

Create a new file "myfirstmath.tex" with the following content.

\documentclass[12pt]{report}
% comments in LaTex are started by the percent symbol
% if you want to type "%" in Latex, use \%.

\usepackage{amsmath} % this is a library package for math commands
% The "ams" in amsmath stands for American Mathematical Society

\begin{document}
my first equation is not 100\% true.
$$\label{eq1} 2 + 2 = 5 + \sqrt{3 + x^2} = 45 \leq \frac{z_6}{3 + \beta}.$$
My second equation is inline: $2 + 3 > 5 \times 1$
My 3rd equation is the system
\begin{gather}
\label{eq3a}
x + y = 5,
\\
\label{eq3b}
10 x - 10 y = 30.
\end{gather}
But we could also typeset  this system as
\begin{align}
\label{eq4a}
x + y &= 5,
\\
\label{eq4b}
10 x - 10 y &= 30.
Maybe Equations~\eqref{eq4a} and \eqref{eq4b} look better than
Equations~\eqref{eq3a} and \eqref{eq3b}
\end{document}
13. Compile "myfirstmath.tex" once and inspect "myfirstmath.pdf" What commands did what? Also, notice that there are a few question marks.

14. Compile "myfirstmath.tex" a second time and inspect "myfirstmath.pdf". What happened to the question marks?

Exercise:

That is basic LaTex. To get some practice, create your own LaTex document explaining how the quadratic formula is used to solve a quadratic equation using the commands above.

Useful LaTex packages

• amsmath - AMS's standard package for math macros and environments like "gather"
• amssymb - extra math symbols not in default LaTex
• amsthm - theorem and proposition environments for writting proofs
• graphicx - allows easy and flexible graphics inclusion in LaTex
• geometry - change margins and page geometry in output
• natbib - flexible bibliography and citation tools
• color - use colors in LaTex
• tikz - draw figures as graphics directly in LaTex
• hyperref - use urls and links with and between documents

MathJax

Set-up

Now, let's see how we can use the LaTex markup style to include mathematics in an html page. Before we get started, let's install a useful add-on in firefox for html development. In the Firefox menus, find "Add-ons". Search for and install an add-on called "Auto Reload". This will make our lives easier in a minute.

The world wide web is based on people authoring text documents in what some physicists decided to call the HyperText Markup Language -- HTML. It's basic idea is to add tags inside angle-brackets to indicate how the text should be formatted. An HTML page starts with an <html> tag, and ends with a </html> tag. There is then a preamble section in between <head> and </head>. The main part of the page is between <body> and </body>. Below is a simple example web-page. Using Canopy's text-editor, create a text-file called hello.html and save the example page there.

<html>
<body>

<!-- this is a comment, and will not be rendered by the browser -->

<h1>hello world!</h1>

<br> <!-- this tag is a line-break.  Normally, html has no line-breaks.-->

We can put text in <b>bold</b> or <em>italics</em> fonts with the right
tags.

<br>

good bye!

</body>
</html>

Now, open your file in Firefox and see what it looks like. You might want to experiment a little by changing the text around and adding other things to the page. Every time you save your changes the Auto Reload add-on should reload the page.

Inserting math into an html page using mathjax

Now, create a new html file, put the example below into it, and load the file in Firefox. Whala! Mathematics in a web page! (it only took 3 decades to make it that easy :(

 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34   hello world!
An inline equation uses dollar-signs, like $\sin(2x) = 2 \sin(x) \cos(x)$. Now, here is your first "display" equation $$x = \int_{1}^{\sin^{x}} \frac{1}{\sqrt{1-u^2}} \mathrm{d} u$$ We can show this is true by differentiating with respect to $x$, \begin{align} \frac{d}{dx} \int_{1}^{\sin^{x}} \frac{1}{\sqrt{1-u^2}} \mathrm{d} u & = \int_{0}^{\sin{\left (x \right )}} 0\, du + \frac{\cos{\left (x \right )}}{\sqrt{1 - \sin^{2}{\left (x \right )}}} \\ & = \frac{\cos(x)}{\sqrt{\cos^{2}(x)}} \\ & = \operatorname{sign}\left( \cos(x) \right) \end{align}
good bye! 

Circle area example blog entry using MathJax, in combination with Scalable Vector Graphics (SVG) images.

Homework

1. Construct a LaTex file to recreate this pdf document

2. Make a web page explaining your answers to Tuesday's homework problems.