LaTex, and writing beautiful mathematics


History of LaTex

References

Traditional LaTex

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".

  2. Change your current working directory to your new "LaTex" directory.

  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

    1. Edit your tex file.

    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}

    Now, save your changes.

    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.

  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.
    \begin{equation}
        \label{eq1}
        2 + 2 = 5 + \sqrt{3 + x^2} = 45 \leq \frac{z_6}{3 + \beta}.
    \end{equation}
    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

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.

Your first web page?

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>
<head>
</head>
<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
<html>
<head>
    <script type="text/javascript" src="https://cdn.mathjax.org/mathjax/2.4-latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
    <script type="text/x-mathjax-config"> MathJax.Hub.Config({tex2jax: {inlineMath:  [['$','$'],['\\(','\\)']] },minScaleAdjust: 80,scale: 80}); </script>
</head>
<body>

hello world!  

<br>

An inline equation uses dollar-signs, like $\sin(2x) = 2 \sin(x) \cos(x)$.

Now, here is your first "display" equation

\begin{equation}
    x = \int_{1}^{\sin^{x}} \frac{1}{\sqrt{1-u^2}} \mathrm{d} u
\end{equation}
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}

<br>

good bye!

</body>
</html>

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.