LaTeX is a free and open-source software for typesetting documents. It is a preparation system for high-quality typesetting and the defacto for large technical documents, computer science, and mathematics documents. For example, you can use LaTeX to create math formulas, equations, cover letters, present assignments or thesis, or edit presentations, syllabi, and exams.
Leslie Lamport originally wrote LaTeX to extend the functionality of the TeX typesetting engine by Donald Knuth. Technically, LaTeX is a set of macros and commands for the programming language TEX.
This article is the second part of our LaTeX typesetting series. You can refer to Part 1 for a comprehensive introduction to LaTeX, learn about page designs, formatting, tables, lists, and how to work with graphics. Part 2 will cover how to typeset math formulas, cross-references, listing contents, and bibliographies.
Create a math document
To make a document with LaTeX, create a file ending in the .tex file extension. When you tell LaTeX to typeset the .tex file, it creates a .pdf file and other auxiliary files. It means that if you make changes to the .tex file, you must typeset the document again for the changes to take effect in the PDF file.
It is recommended to place every LaTeX file in a unique folder to avoid cluttering your system with auxiliary files generated by the LaTeX compiler.
Open (TeXstudio) and save a new file (i.e. math.tex). LaTeX commands typically start with a backslash (\). Command parameters are enclosed in curly braces { }. First, declare the type of the documentclass (i.e. \documentclass{book}).
Then, mark the beginning and the end of the document with begin and end (i.e., \begin{document}, \end{document}). You can then write your math formulas.
Example: Math document
\documentclass[a4paper,12pt]{book} \usepackage{blindtext} \usepackage[english]{babel} begin{document} ... % add your math formulas here \end{document}
Note: We will use this math.tex document in the article to illustrate examples.
1: Math formulas
LaTeX provides many commands to write and print mathematical symbols. You can use these commands in different mathematical environments such as inline formulas, displayed formulas, and aligned formulas. The following are some of the ways TeX treats maths.
- All letters are automatically italicized to give a visual cue of a formula instead of a normal letter.
- The spacing is tighter, with no spaces between letters.
- You can use commands for math symbols such as Greek letters and fractions.
- Blank lines are not allowed.
Inline Math formulas
Inline formulas are usually short and typeset formulas inside a sentence. To design an inline formula, enclose an expression in the symbol $.
Example: Inline math formulas
Let $x = k\cos\theta$ and $y = k\sin\theta$. Then $k = \sqrt{x^3 + y^3 }$ and $\theta = \arctan(y/x)$.
There are thousands of math commands, and you can find symbols from several websites (i.e., detexify ) with lists of LaTeX math symbols.
Example: Common inline math commands
\section{Inline math} \subsection{Example II: Inline math commands} $a^b$ $a_b$ $\log(x)$ $\alpha$ $\sqrt{x}$ $\frac{a}{b}$
Displayed Math formulas
Displayed math typesets formulas on a new line with spacing before and after. Ideal for long, tall, or important equations, expressions, and large symbols such as integrals. To design a displayed formula, enclose the expression in the symbol \[ enter displayed math formulas\].
Example: Displayed math commands
\section{Displayed math} \subsection{Example III:Displayed math commands} \[ \int_0^1 f(x)\, dx = \lim_{n\to\infty} \sum_{i=0}^{n-1} \frac{1}{n} f\left(\frac{i}{n}\right) \]
Aligned Math formulas
Aligned math typesets several formulas, each on its line, with a symbol (i.e., =) in the same place in each line. It is used for expressions that do not fit on a single line or computations with multiple steps. To use the aligned mode, add the amsmath package (\usepackage{amsmath}) to the preamble of the document. Next, use the align* environment to enter aligned mode. You can then type equations as you would for display or inline mode, but with the following modifications:
- Place & before symbols to vertically align them.
- Place \\ at the end of a line to make a new line.
Example: Aligned math
\section{Aligned math} \subsection{\texttt{Example IV:Aligned math}} \begin{align*} (m+n)^2 &= (m+n)(m+n) \\ &= m(m+n) + n(m+n) \\ &= m^2 + mn + nm + n^2 \\ &= m^2 + 2mn + n^2. \end{align*}
Mathematical symbols
Subscripts and superscript
Use _ for indexes or subscripts and ^ for exponents or superscript.
\[ \ x_1^2 + y_3^2 = 1 \\\] \[ \ \quad 2^{3^y} = 256 \\ \]
Extraction roots
A square root follows the order \sqrt[order]{value}.
\subsection{\texttt{Example VI:Extraction roots}} $\sqrt[256]{k} = \sqrt{\sqrt{\sqrt{\sqrt{\sqrt{k}}}}}$
Fractions
Use / or \frac to denote fractions.
\subsection{\texttt{Example VII:Fractions}} \[ \frac{n(n+1)}{2}, \quad \frac{\frac{\sqrt{x}+1}{3}-{\sqrt{x}+1}{3}}{y^2} \]
Greek letters
\subsection{\texttt{Example VII:Greek letters}} $\alpha -alpha\\ \lambda -lambda\\ \pi -pi\\ \omega -omega\\ \kappa -kappa\\ \epsilon -epsilon\\ \xi -xi\\ \zeta -zeta\\$ .
Script letters
Use \mathcal to design calligraphic shapes of upper case letters (A, B, C,…, Z.) in the alphabet.
\subsection{\texttt{Example VIII:Script letters}} \[ \mathcal{A},\mathcal{B},\mathcal{C},\mathcal{D}, \mathcal{E}\ldots,\mathcal{X},\mathcal{Y},\mathcal{Z} \]
Math operators and symbols
You can use the standard LaTeX symbols or find additional symbols with the latexsym and amssymb packages. In addition, there are commands available for binary operators, arrows, relation, subset, superset symbols, and so on.
\subsection{\texttt{Example VIX:Math operators and symbols}} $\amalg, \circ \\ \ddagger \diamond \odot \oplus \\ \approx \equiv \dashv \smile \\ \geq \neq \\ \sqsubseteq \subset \subseteq \\ \bigcap \biguplus \int \sum \oint \\ \Longleftarrow \rightarrow \Updownarrow \\ \imath \top \Im \exists \\ \aleph \clubsuit \clubsuit \\ \clubsuit \heartsuit \\ \spadesuit \sharp \infty \triangle \\$
Arrays, matrices, binomial expressions
\subsection{\texttt{Example: Math structures}} \texttt{Arrays} \[ A = \left( \begin{array}{cc} a_{11} & a_{12} \\ a_{21} & a_{22} \end{array} \right) \]
\texttt{Binomial expressions} \[\binom{m}{k} = \frac{m!}{k!(m-k)!}\]
\texttt{Matrices} \[ A = \begin{pmatrix} a_{13} & a_{22} \\ a_{31} & a_{52} \end{pmatrix} \]
Fun Fact:
- LaTeX is a WYSIWYM (what-you-see-is-what-you-mean) editor.
- Microsoft word is a WYSIWYG (what-you-see-is-what-you-
get) editor.
2: Cross-referencing
LaTeX provides simple mechanisms to number parts and refers to different parts of a document such as sections, subsections, tables, footnotes, figures, tables, equations, page numbers, etc. To refer to an object, first label it using the command \label{name} with the label as the argument. Then, to refer back to the object, use the \ref{name} command that takes the label as its argument and prints the object’s number.
Example: Cross-referencing
Add the following code snippet to your (math.tex) example.
\section{ Cross referencing} \label{sec:Cross referencing} Learn how to cross reference sections, objects in LaTeX. \subsection{\texttt{Examples:}} See section \ref{sec:Cross referencing} for an introduction to cross referencing in LaTeX.
Referring to a page
You can use the command \pageref{name} to print the corresponding page numbers.
Add the following commands to refer to a section on a page.
\subsection{\texttt{Page referencing:}} See section~\ref{sec:Cross referencing} on page~\pageref{sec:Cross referencing} to learn about LaTeX cross referencing.
You can also use the varioref (\usepackage{varioref}), and cleveref \usepackage{cleveref} packages to create intelligent and clever page references.
Referring to labels in other documents
Use the eXternal references {xr} package to refer to labels in several related documents. To use the XR package, add \usepackage{xr} to the preamble. For example, if you need to refer to sections in an external document (i.e., book.tex), add the following command to the preamble, \externaldocument{book}.
Moreover, to avoid conflicts arising from same \label names, declare a prefix using the argument \externaldocument:
\externaldocument[B-]{book}
This way, all references from book.tex would be prefixed by B- and you could write
\ref{B-name} to refer to name in book.tex
3: Listing content and references
We have already learned how LaTeX makes it simple to create a table of contents with the command \tableofcontents. This section will enhance our table of contents with custom headings and numbering levels, create custom lists of figures or tables, and insert arbitrary text.
Example: Manually add headings to your table of contents.
\documentclass[a4paper,13pt,twocolumn ]{book} \usepackage{blindtext} \usepackage[english]{babel} \usepackage{amsmath} \begin{document} \tableofcontents \part{First Part} \chapter*{Preface} \addcontenttsline{toc}{chapter}{Preface} … … \part{Last part} \chapter{Last chapter} \appendix \cleardoublepage \addtocontents{toc}{\bigskip} \addcontentsline{toc}{part}{Appendix} \chapter{Glossary} \chapter{Symbols} \end{document}
The \addcontentsline command added a chapter-like entry for the preface and inserted a part-like heading for the appendix. We also added space before the appendix heading with the command \addtocontents.
Create a custom list of figures
You can use the commands \listofigures and \listoftables to create a list of figures and tables. Moreover, we can customize other lists in our documents with the following steps. For illustration, we will add two diagrams to our math.tex document.
First, add the following lines to your preamble:
\renewcommand{\figurename}{Diagram} \renewcommand{\listfigurename}{List of Diagrams}
Add the command \listoffigure immediately after the table of contents. Add the following diagram in Chapter 1.
\begin{figure} \centering \fbox{Diagram placeholder} \caption{FOSSLinux Organizational Chart} \end{figure}
Add the following to chapter 2.
\begin{figure} \centering \fbox{Diagram placeholder} \caption{UML overview} \end{figure} \begin{figure} \centering \fbox{Diagram placeholder} \caption{Database Design} \end{figure}
Typeset twice to view the changes made in your document.
Additionally, you can use the following LaTeX packages that provide extensive features for customizing tables of contents, lists of tables, and figures.
- tocloft package gives you extensive control over typography.
- titletoc package is for customizing and sectioning headings.
- multitoc package generates two or more column layouts.
- minitoc package creates a small table of contents for each part, section, or chapter.
- tocbibind package automatically adds an index, bibliography, table of contents, list of figures, and list of tables to the table of contents.
Building an index
An index is a list of words and page numbers pointing to where you can find related content in a document. In addition, it provides selective pointers to relevant information.
To design an index, add the index package (\usepackage{index}) to your preamble and add, \makeindex, immediately after the preamble.
In Chapter 1 of math.tex example, index the caption of the FOSSLinux Organizational Chart diagram with the keyword FOSSLinux:
\caption{\index{FOSSLinux}FOSSLinux Organizational Chart}
Next, index UML overview and database design diagrams with the keywords UML and database, respectively:
\caption{\index{UML}UML overview} \caption{\index{Database}Database Design}
Next, create an entry for the index in the table of contents and place it before the command \end{document}:
\clearpage \addcontentsline{toc}{chapter}{Index}
Next, typeset the index with the \printindex command. Finally, to display your index in TeXstudio, click on index under the tools menu. Then, typeset your document to view the changes.
4: Bibliography
A bibliography/list of references enhances the authenticity of your document. LaTeX provides the bibliography environment to typeset a list of references. Each citation item in LaTeX is specified using the command \bibitem. This command requires the key argument that we can refer to by \cite{key} or \cite{key1,key2}. The command \cite can also take an optional argument stating a range of pages in your citation. For example, \cite[p.\,18–20]{key}. You may also choose a label as an optional argument of \bibitem.
We will add two random references to our math.tex example with the following commands.
\begin{thebibliography}{8} \bibitem{W.R.L.1984} Weaver II, R. L, \emph {Understanding business communica- tion.} Englewood Cliffs, NJ: Prentice-Hall, 1984. \bibitem{Yate.M.2014} Yate, M.\emph {Knock ’em dead: Social networking.} Avon, MA: Adams Media, 2014. \end{thebibliography}
You can also use labels to list the bibliography items instead of numbers with the following code snippet.
\begin{thebibliography}{Yate.M.2014} \bibitem[W.R.L.1984]{W.R.L.1984} Weaver II, R. L, \emph {Understanding business communication.} Englewood Cliffs, NJ: Prentice-Hall, 1984. \bibitem[Yate.M.2014}]{Yate.M.2014} Yate, M.\emph {Knock ’em dead: Social networking.} Avon, MA: Adams Media, 2014. \end{thebibliography}
Add hyperlinks and bookmarks
LaTeX provides the hyperref package, \usepackage{hyperref}, which automatically creates hyperlinks and a bookmark bar in your documents. Moreover, cross-references and table of contents entries become hyperlinks to the beginning of the corresponding chapters. After you typeset your document, hyperlinks will be marked by red frames.
Add watermarks
To enable watermarks in your document, use the draftwatermark (\usepackage{draftwatermark}) package. Add the following code snippet to demonstrates how to add a watermark.
\usepackage{draftwatermark} \SetWatermarkText{\color{red}TOP SECRET} % add top secret watermark \SetWatermarkScale{3} %specify text size
Complete math.tex example
% find attached the complete math.tex example \documentclass[a4paper,12pt,]{book} \usepackage{blindtext} \usepackage[english]{babel} \usepackage{hyperref} \usepackage{amsmath} \usepackage{graphicx} \renewcommand{\figurename}{Diagram} \renewcommand{\listfigurename}{List of Diagrams} \usepackage{index} \makeindex \usepackage{draftwatermark} \SetWatermarkText{\color{red}TOP SECRET} % add top secret watermark \SetWatermarkScale{3} %specify text size \begin{document} \tableofcontents \listoffigures \part{First Part} \chapter*{Preface} \addcontentsline{toc}{chapter}{Preface} \blindtext[2] \chapter{\textsf{LaTeX typesetting: Math Formulas}} \section{Example I} Let $x = k\cos\theta$ and $y = k\sin\theta$. Then $k = \sqrt{x^3 + y^3 }$ and $\theta = \arctan(y/x)$. \begin{figure} \centering \fbox{Diagram placeholder} \caption{\index{FOSSLinux}FOSSLinux Organizational Chart} \end{figure} \section{Inline math} \subsection{\texttt{Example II: Inline math commands}} $a^b$ $a_b$ \log(x)$ $\alpha$ $\sqrt{x}$ $\frac{a}{b}$ \section{Displayed math} \subsection{\texttt{Example III:Displayed math commands}} \[ \int_0^1 f(x)\, dx = \lim_{n\to\infty} \sum_{i=0}^{n-1} \frac{1}{n} f\left(\frac{i}{n}\right) \] \section{Aligned math} \subsection{\texttt{Example IV:Aligned math}} \begin{align*} (m+n)^2 &= (m+n)(m+n) \\ &= m(m+n) + n(m+n) \\ &= m^2 + mn + nm + n^2 \\ &= m^2 + 2mn + n^2. \end{align*} \section{Mathematical symbols } \subsection{\texttt{Example V:Subscripts and superscript}} \[ \ x_1^2 + y_3^2 = 1 \\\] \[ \ \quad 2^{3^y} = 256 \\ \] \subsection{\texttt{Example VI:Extraction roots}} $\sqrt[256]{k} = \sqrt{\sqrt{\sqrt{\sqrt{\sqrt{k}}}}}$ \subsection{\texttt{Example VII:Fractions}} \[ \frac{n(n+1)}{2}, \quad \frac{\frac{\sqrt{x}+1}{3}-{\sqrt{x}+1}{3}}{y^2} \] \subsection{\texttt{Example VII:Greek letters}} $\alpha -alpha\\ \lambda -lambda\\ \pi -pi\\ \omega -omega\\ \kappa -kappa\\ \epsilon -epsilon\\ \xi -xi\\ \zeta -zeta\\$ \subsection{\texttt{Example VIII:Script letters}} \[ \mathcal{A}, \mathcal{B}, \mathcal{C}, \mathcal{D}, \mathcal{E}\ldots, \mathcal{X}, \mathcal{Y}, \mathcal{Z} \] \subsection{\texttt{Example VIX:Math operators and symbols}} $\amalg, \circ \\ \ddagger \diamond \odot \oplus \\ \approx \equiv \dashv \smile \\ \geq \neq \\ \sqsubseteq \subset \subseteq \\ \bigcap \biguplus \int \sum \oint \\ \Longleftarrow \rightarrow \Updownarrow \\ \imath \top \Im \exists \\ \aleph \clubsuit \clubsuit \\ \clubsuit \heartsuit \\ \spadesuit \sharp \infty \triangle \\$ \subsection{\texttt{Example: Math structures}} \texttt{Arrays} \[ A = \left( \begin{array}{cc} a_{11} & a_{12} \\ a_{21} & a_{22} \end{array} \right) \] \texttt{Binomial expressions} \[\binom{m}{k} = \frac{m!}{k!(m-k)!}\] \texttt{Matrices} \[ A = \begin{pmatrix} a_{13} & a_{22} \\ a_{31} & a_{52} \end{pmatrix} \] \section{ Cross referencing} \label{sec:Cross referencing} Learn how to cross reference sections, objects in LaTeX. \subsection{\texttt{Examples:}} See section \ref{sec:Cross referencing} for an introduction to cross referencing in LaTeX. \subsection{\texttt{Page referencing:}} See section~\ref{sec:Cross referencing} on page~\pageref{sec:Cross referencing} to learn about LaTeX cross referencing. \begin{figure} \centering \fbox{Diagram placeholder} \caption{\index{UML}UML overview} \end{figure} \begin{figure} \centering \fbox{Diagram placeholder} \caption{\index{Database}Database Design} \end{figure} \part{Second part} \chapter{Last chapter} \appendix \addtocontents{toc}{\bigskip} \addcontentsline{toc}{part}{Appendix} \chapter{Glossary} \chapter{Symbols} \addcontentsline{toc}{chapter}{Index} \printindex \section*{Recommended references} To understand business communication in depth, see \cite{W.R.L.1984}. For Social networking, see \cite{Yate.M.2014} \begin{thebibliography}{8} \bibitem{W.R.L.1984} Weaver II, R. L, \emph {Understanding business communica- tion.} Englewood Cliffs, NJ: Prentice-Hall, 1984. \bibitem{Yate.M.2014} Yate, M.\emph {Knock ’em dead: Social networking.} Avon, MA: Adams Media, 2014. \end{thebibliography} \begin{thebibliography}{Yate.M.2014} \bibitem[W.R.L.1984]{W.R.L.1984} Weaver II, R. L, \emph {Understanding business communica- tion.} Englewood Cliffs, NJ: Prentice-Hall, 1984. \bibitem[Yate.M.2014]{Yate.M.2014} Yate, M.\emph {Knock ’em dead: Social networking.} Avon, MA: Adams Media, 2014. \end{thebibliography} \end{document}
Additional online resources
The article is an intermediary guide to typesetting math formulas, cross-referencing, listing content, and creating bibliographies. If you would like to learn more, the following resources are available for learning and mastering LaTeX.
- The Wikibooks LaTeX page includes basic and advanced information about LaTeX.
- LaTeX-Community is a web forum with many subforums, each dealing with topics such as Math, Fonts, and so on. You may ask questions or support, other users looking for help.
- TeX Stack Exchange is a question-and-answer site about TEX, LaTeX, and friends. The users are pros and can answer any question you have.
- The Comprehensive TEX Archive Network (CTAN) houses over 4500 packages and their documentation.
- AMS-Math FAQ is a forum managed by the American Mathematical Society and contains a list of questions and answers to the amsmath and AMS classes and packages.
- TeX Catalogue offers a topical and alphabetical index to nearly 4000 TeX and LaTeX packages and tools.
- The LaTeX Font Catalogue manages a list of all fonts available for use with LaTeX with concrete code examples.
Wrapping up
That brings us to the end of LaTeX document typesetting Part 2. The article covered typesetting math formulas, cross-referencing, listing content, creating bibliographies, bookmarks, and hyperlinks. I have used plenty of examples (math.tex) that are simple to follow and practice.
I recommend you to refer to Part 1 for an introductory to intermediary guide on mastering LaTeX document typesetting. Moreover, TeX friends are very welcoming and resourceful in whatever community website you decide to join.