Crash Course in Scientific Computing

$\TeX$ and $\LaTeX$

We have approached how computers display bitmaps on a screen, or store this information on a file. We have seen ASCII and this covers for text, which is already a huge part of what we need to communicate. But Latin alphabet is short of advanced applications. It's good enough for the news, maybe for literature, but how about science? Let's have a look again at one of the oldest equations which humanity has dealt with:

$$x_{1\atop 2}=-{b\pm\sqrt{b^2-4ac}\over 2a}$$

This is the modern notation. It has not always been so neat and structured, being overlayed all over the place with parts of it which extend over others, etc., but it has always required going beyond mere text (and that includes numbers). In particular, in Science, we use a lot Greek letters. We actually also need it without science, for instance, because we're Greek and want to read the news on the Internet in our language. For that, technology has developed a new technology, which is the extension of ASCII and is known as unicode. It allows to display characters from other languages, e.g., Greek, Russian, Chinese, etc. It also covers for a myriad of other characters, from chess ♙♘♗♖♕♔♚♛♜♝♞♟ to road signs ⛔ 🚫 ⚠ ☡ passing by mathematical symbols iħ∂ₜψ(r,t)=[-(∇²/(2m))+V]ψ, and a myriad of things you probably don't need (like a super-asterix ✺ or Groucho Marx's glasses ᅘ). The temptation to encode other fonts has not been resisted. 𝓘𝓼 𝓽𝓱𝓲𝓼 𝓻𝓮𝓪𝓵𝓵𝔂 𝓶𝔂 𝓳𝓸𝓫 𝓽𝓱𝓸𝓾𝓰𝓱? And this remains highly limited for proper math encoding. Compare with the real nice way to write Schrödinger's equation:

$$i\hbar\partial_t\psi(r,t)=\left(-{\nabla^2\over 2m}+V\right)\psi$$

Someone worried about computer's inability to render beautiful math equations when computers started to take up the job of typesetting. Donald Knuth was then working on the Art of Computer programming, an encyclopedia of algorithms and code writing. But when he saw how horrible was the result, he decided to put this on hold and to write a program to typeset text beautifully with a computer. He started the TeX project, which he thought would take him a few months, but turned out to take about 9 years instead![1]. His code became the standard for scientific typesetting, so much so that it is still about the dominant sector as of today, especially for complex documents, either in content (elaborate mathematics) or in size (books, volumes, theses, etc.)

In this lecture, we will learn the bases of this program because as scientists, it is our duty to produce not only nice-looking, but also accurate, intelligible and correct outputs. We will only survey the strict minimum required to produce an output but you are invited to dig in, it's a very powerful and beautiful piece of computer software.

Knuth called his program $\mathrm{\TeX}$ which is is the upcase T, E and X of the Greek letters $\tau$, $\epsilon$ and $\chi$. They happen to be the same upcases in Latin or Greek, whence the latin-looking word, which is actually the beginning of τέχνη (ΤΕΧΝΗ – technē), Greek for both "art" and "craft", which is also the root word of technical, so it should be pronounced like "Tech", not like "tex". To demonstrate the power of the program that can assemble glyphs in any way it wants, the name reads $\mathrm{\TeX}$ with a displaced E, which would be difficult to do otherwise. In ASCII, Knuth asks that we write TeX to echo that.

It really is a computer language, so we write code, compile it (asks a program, namely, $\mathrm{\TeX}$, to process it and turn it into a document. Here's a piece of code:

hello there!

Welcome to \TeX.

\bye

This is simply text and the instructions for the program come with a slash. There are two commands there, one is to typeset $\mathrm{\TeX}$ as it should, the other is to signal the end of the document (\bye). We compile this by putting it in a file, say hi.tex, and then from the terminal we run:

tex hi.tex

this will produce

This is TeX, Version 3.14159265 (TeX Live 2019/Debian) (preloaded format=tex)
(./hi.tex [1] )
Output written on hi.dvi (1 page, 268 bytes).
Transcript written on hi.log.

and produce a dvi file which one can either view directly (e.g., with xdvi) or convert into something more popular, like pdf, with dvipdf hi.dvi.

There are two modes in TeX: the text mode and the math mode. We enter the math mode by enclosing its (mathematical) content between $ (dollar sign). So for instance, here is a piece of text combining both:

The number $i$ is special in that $i^2=-1$.

That reads:

Screenshot 20210202 114456.png

It's all written in ASCII, as code, and the program does the formatting. We have just seen superscript. Subscript go with an underscore _. The simpler is to see examples:

If $x_1+x_2=y^2$ then $y=\sqrt{x_1+x_2}$.

gives

If $x_1+x_2=y^2$ then $y=\sqrt{x_1+x_2}$.
The derivative of $\int_a^x f(y)dy=f(x)$ (fundamental theorem of calculus).

gives:

The derivative of $\int_a^x f(y)dy=f(x)$ (fundamental theorem of calculus).

You see that writing the math formula inline squishes it a bit, if it takes a lot of vertical space, as integrals or fractions do. If a formula is very bulky, or if it is important, then we use the displayed mode rather than the inline mode, namely, using two $$ we write something like,

an important number is the golden ratio $$\phi=\frac{1+\sqrt{5}}{2}.$$

gives:

an important number is the golden ratio $$\phi=\frac{1+\sqrt{5}}{2}.$$

There can be a lot of coding involved. Here is the binomial formula:

$$(a+b)^n=\sum_{k=0}^n{n\choose k}a^kb^{n-k}$$

$$(a+b)^n=\sum_{k=0}^n{n\choose k}a^kb^{n-k}$$

Very pretty. Most of the syntax is straightforward. For instance, Greek letters go $\alpha\neq\beta$ $\alpha\neq\beta$ and upcases $\Gamma>\gamma$ $\Gamma>\gamma$. Note that $\mathrm{\TeX}$ also allows you to typeset text itself, for instance, writing Schr\"odinger (even if you don't have the letter) or put something in emphasis \emph{(like this!)} or bold \textbf{(like that!)}.

Be careful that functions are written in math modes but as functions, e.g., write $\sin(\pi x)$ not $sin(\pi x)$ or sin$(\pi x)$, the latter being an abomination. Even if you write sin alone, you have to write $\sin$ alone ($\sin$ alone). The difference of course is that between $\sin(\pi x)$ and $sin(\pi x)$. This happens for other symbols, for instance, > is fine for greater (which you can also write as \gt, for "greater than", but use \langle and \rangle for averages, i.e., $\langle x^2\rangle\neq<x^2>$, can you see it? $\langle x^2\rangle\neq<x^2>$.

As $\mathrm{\TeX}$ became popular, people started to write "macros", or pieces of code, to make more complicated typesetting. One became so famous that it is now the standard, even more used than $\mathrm{\TeX}$ itself. It is called $\LaTeX$ (from Leslie Lamport, who apparently didn't get the joke because it reads as Lay-Tech not latex). Anyway, the basic idea behind LaTeX is that it separates even more the structure of the document from its actual content, which are two separate things. This is in contrast to WYSIWYG (what you see is what you get) where everything is done together, giving a false sense of comfort but making you worry on the look rather than on the meaning.

Here is a minimal $\LaTeX$ input (that you compile with latex input.tex or pdflatex input.tex if you want the output directly in pdf):

\documentclass{article} 
\usepackage{amsmath} 
\title{A very short question}
\author{A. Student} 

\begin{document} 
\maketitle

We have seen in class that:
%
\begin{equation}
f(x)=\sum_{k=0}^\infty{f^{(k)}(0)\over k!}x^k
\end{equation}
%
and called that the Mac Laurin formula. Since $(1/x)'=-1/x^2$, and
also $(1/x)''=(-1/x^2)'=2/x^3$, etc., how can I obtain the expansion
for the inverse function?

\end{document}

Which produces something that you can proudly email to your lecturer as opposed to scanned handwritten scribbles or, worst still, ASCII rendered version:

Screenshot 20210202 122404.png

Practice will empower you with more sophisticated document structure, like \section, \subsection, etc. Note that instead of dollar signs to enter in math mode, the layout is to enclose the content between \begin{something} and \end{something}. This is how to typeset a matrix:

M=\begin{pmatrix}
1 & 0 & -1 \\
0 & 1 & 2 \\
-1 & 2 & 1
\end{pmatrix}

$$M=\begin{pmatrix} 1 & 0 & -1 \\ 0 & 1 & 2 \\ -1 & 2 & 1 \end{pmatrix}$$

Another thing to keep in mind is that in complex mathematical documents, one typically refers to some equations later in the text:

For instance, one definition of Euler's number $e$ is:
\begin{equation}
\label{eq:eulernumber}
e=\lim_{n\rightarrow\infty}(1+{1\over n})^n.
\end{equation}
Equation (\ref{eq:eulernumber}) is only one of the many possible definitions!

For instance, one definition of Euler's number $e$ is: \begin{equation} \tag{1} e=\lim_{n\rightarrow\infty}(1+{1\over n})^n. \end{equation} Equation (1) is only one of the many possible definitions! Now if you add another equation before this one, all the numbering will be updated, so you do not have to worry about the details and the structure, only about the content.

We have given you the very basic. As your experience will grow, you will make a better use of the tool and rely on sophisticated typesetting. For instance, but above would really be written as:

For instance, one definition of Euler's number~$e$ is:
\begin{equation}
\label{eq:eulernumber2}
e=\lim_{n\rightarrow\infty}\left(1+{1\over n}\right)^n\,.
\end{equation}
Equation~(\ref{eq:eulernumber}) is only one of the many possible definitions!

For instance, one definition of Euler's number $e$ is: \begin{equation} \tag{2} e=\lim_{n\rightarrow\infty}\left(1+{1\over n}\right)^n\,. \end{equation} Equation (2) is only one of the many possible definitions!

Do you see the differences between eq. (1) and eq. (2)? There are three:

  1. We use ~ (tilda) to insert a non-breaking space, so that there is no an unfortunate line breaking there or incorrect spacing.
  2. We use \, to add an extra spacing so that the punctuation is not too close to the equation. It's better for instance to write \int f(x)\,dx so that the dx gets some little space from the integrand.
  3. We used \left and \right so that the enclosing parentheses get stretched to match the content of what is inside. This is prettier, don't you think?

References

  1. Donald Knuth (1997). "Digital Typography (Kyoto Prize Lecture, 1996)" (pdf).