Chapter 10 – Writing Advanced Mathematics

Here are the Code examples of of this chapter. You can compile them online right on this web page by pressing the Typeset / Compile button. You can also edit them for testing, and compile again.

For a better view with the online compiler, I sometimes use \documentclass[border=10pt]{standalone} instead of \documentclass{article}. Instead of having a big letter/A4 page, the standalone class crops the paper to see just the visible text without an empty rest of a page.

Any question about a code example? Post it on LaTeX.org, I will answer. As forum admin I read every single question there. (profile link).

\documentclass{article}
\pagestyle{empty}
\begin{document}
\section*{The golden ratio}
The symbol for the golden ratio is the Greek
letter \( \varphi \). Its value is the positive solution
of \( x^2 - x - 1 = 0 \).
It can be calculated to:
\[
  \varphi = \frac{1 + \sqrt{5}}{2} = 1.618 \ldots
\]   
\end{document}

\documentclass{article}
\pagestyle{empty}
\begin{document}
\[
  x^{n_1} \neq x^n_1
\]
\[
  {x^2}^3 = x^{2^3}
\]
\end{document} 

\documentclass{article}
\pagestyle{empty}
\usepackage{mathtools} 
\mathtoolsset{showonlyrefs}
\begin{document} 
\begin{gather}
  y = x^2 + 1 \label{xx}\\
  z = 0
\end{gather}
See \eqref{xx}.
\end{document}

\documentclass{article}
\usepackage{dsfont}
\begin{document}
\[
  \lim_{n\to\infty} \sup_{x\in\mathds{R}} f_n(x^2)
    < n \Big(\sum_{x\in\mathds{R}, n\in\mathds{N}}
    \big| f_n(x^2) \big| \Big)
\]
\end{document}

\documentclass{article}
\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{thmtools}
\newtheorem{thm}{Theorem}
\newtheorem{lem}[thm]{Lemma}
\theoremstyle{definition}
\newtheorem{dfn}[thm]{Definition}
\theoremstyle{remark}
\newtheorem*{note}{Note}
\pagestyle{empty}
\begin{document}
\listoftheorems
\newpage
\begin{dfn}
  The longest side of a triangle with a right angle
  is called the \emph{hypotenuse}.
\end{dfn}
\begin{note}
  The other sides are called \emph{catheti},
  or \emph{legs}.
\end{note}
\begin{thm}[Pythagoras]
  \label{pythagoras}
  In any right triangle, the square of the hypotenuse
  equals the sum of the squares of the other sides.
\end{thm}
\begin{proof}
  The proof has been given in Euclid's Elements,
  Book 1, Proposition 47. Refer to it for details.
  The converse is also true, see lemma \ref{converse}.
\end{proof}
\begin{lem}
  \label{converse}
  For any three positive numbers \(x\), \(y\),
  and \(z\) with \(x^2 + y^2 = z^2\), there is a
  triangle with side lengths \(x\), \(y\) and \(z\).
  Such triangle has a right angle, and the hypotenuse
  has the length \(z\).
\end{lem}
\begin{note}
  This is the converse of theorem \ref{pythagoras}.
\end{note}
\end{document}

\documentclass[12pt]{beamer}
\usefonttheme[onlymath]{serif}
\usepackage{breqn}
\begin{document}
\begin{frame}
\begin{dmath*}
  \left( \frac{f}{g} \right)^\prime(x)
    = \lim_{h \rightarrow 0}
      \left( \frac{1}{ g(x+h) g(x) } \right)
      \left[
             \frac{ f(x+h) - f(x) }{h} g(x)
            -\frac{ g(x+h) - g(x) }{h} f(x)
      \right]
    = \frac{f^\prime(x)g(x)-f(x)g^\prime(x)}{g^2(x)}
  \end{dmath*}
\end{frame}
\end{document} 

\documentclass{article}
\usepackage{dsfont}
\usepackage{mathtools}
\begin{document}
\[
  \adjustlimits\lim_{n\to\infty} \sup_{x\in\mathds{R}}
    f_n(\cramped{x^2})
    < n \Big( \smashoperator{\sum_{x\in\mathds{R},
    n\in\mathds{N}}}
    \big\lvert f_n(\cramped{x^2}) \big\rvert \Big)
\]
\end{document}

\\documentclass[border=10pt]{standalone}
\usepackage{tikz-cd}
\usepackage{amsmath}
\DeclareMathOperator{\im}{im}
\pagestyle{empty}
\begin{document}
\begin{tikzcd}
      G \arrow[r, "\varphi"]
        \arrow[d, twoheadrightarrow, "\pi" left] & \im \varphi \\
      G/\ker \varphi \arrow[ru, hook, dashed, "\tilde{\varphi}" below] & \\
\end{tikzcd}
\end{document}

\documentclass[border=10pt]{standalone}
\usepackage{tikz-cd}
\usepackage{amsmath}
\DeclareMathOperator{\im}{im}
\pagestyle{empty}
\begin{document}
\begin{tikzcd}
  G \arrow[r, "\varphi"]
    \arrow[d, "\pi" left] & \im \varphi \\
  G/\ker \varphi \arrow[ru, "\tilde{\varphi}" below]
\end{tikzcd}
\end{document}

\documentclass[border=10pt]{standalone}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{fit}
\newcommand{\overlay}[2][]{\tikz[overlay,
  remember picture, #1]{#2}}
\tikzset{
  highlighted/.style = { draw, thick, rectangle,
                         rounded corners, inner sep = 0pt,
                         fill = red!15, fill opacity = 0.5
                       }
}
\newcommand{\highlight}[1]{%
  \overlay{
    \node [fit = (left.north west) (right.south east),
           highlighted] (#1) {}; }
}
\newcommand{\flag}[2]{\overlay[baseline=(#1.base)]
  {\node (#1) {$#2$};}}
\begin{document}
\[
  M = \begin{pmatrix}
    \flag{left}{1} & 2 & 3 & 4 & 5 \\
    6 & 7 & 8 & 9 & 10 \\
    11 & \flag{before}{12} & \flag{right}{13} & 14 & 15 \\
    16 & 17 & 18 & 19 & 20
  \end{pmatrix}
  \highlight{N}
  \qquad
  M^T = \begin{pmatrix}
    \flag{left}{1} & 6 & 11 & 16 \\
    2 & 7 & \flag{after}{12} & 17 \\
    3 & 8 & \flag{right}{13} & 18 \\
    4 & 9 & 14 & 19 \\
    5 & 10 & 15 & 20
  \end{pmatrix}
  \highlight{NT}
\]
\overlay{
  \draw[->, thick, red, dotted] (before) -- (after);
  \draw[->, thick, red, dashed] (N) -- (NT)
    node [pos=0.68, above] {Transpose};
  \node[above of = N ] { $N$   };
  \node[above of = NT] { $N^T$ };
}
\end{document}

\documentclass{article}
\usepackage{amsmath}
\usepackage{amsthm}
\newtheorem{thm}{Theorem}
\newtheorem{lem}{Lemma}
\theoremstyle{definition}
\newtheorem{dfn}{Definition}
\theoremstyle{remark}
\newtheorem*{note}{Note}
\begin{document}
\begin{dfn}
  The longest side of a triangle with a right angle
  is called the \emph{hypotenuse}.
\end{dfn}
\begin{note}
  The other sides are called \emph{catheti},
  or \emph{legs}.
\end{note}
\begin{thm}[Pythagoras]
  \label{pythagoras}
  In any right triangle, the square of the hypotenuse
  equals the sum of the squares of the other sides.
\end{thm}
\begin{proof}
  The proof has been given in Euclid's Elements,
  Book 1, Proposition 47. Refer to it for details.
  The converse is also true, see lemma \ref{converse}.
\end{proof}
\begin{lem}
  \label{converse}
  For any three positive numbers \(x\), \(y\),
  and \(z\) with \(x^2 + y^2 = z^2\), there is a
  triangle with side lengths \(x\), \(y\) and \(z\).
  Such triangle has a right angle, and the hypotenuse
  has the length \(z\).
\end{lem}
\begin{note}
  This is the converse of theorem \ref{pythagoras}.
\end{note}
\end{document}

\documentclass{article}
\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{amsthm}
\newtheorem{thm}{Theorem}
\newtheorem{lem}[thm]{Lemma}
\theoremstyle{definition}
\newtheorem{dfn}[thm]{Definition}
\theoremstyle{remark}
\newtheorem*{note}{Note}
\begin{document}
\begin{dfn}
  The longest side of a triangle with a right angle
  is called the \emph{hypotenuse}.
\end{dfn}
\begin{note}
  The other sides are called \emph{catheti},
  or \emph{legs}.
\end{note}
\begin{thm}[Pythagoras]
  \label{pythagoras}
  In any right triangle, the square of the hypotenuse
  equals the sum of the squares of the other sides.
\end{thm}
\begin{proof}
  The proof has been given in Euclid's Elements,
  Book 1, Proposition 47. Refer to it for details.
  The converse is also true, see lemma \ref{converse}.
\end{proof}
\begin{lem}
  \label{converse}
  For any three positive numbers \(x\), \(y\),
  and \(z\) with \(x^2 + y^2 = z^2\), there is a
  triangle with side lengths \(x\), \(y\) and \(z\).
  Such triangle has a right angle, and the hypotenuse
  has the length \(z\).
\end{lem}
\begin{note}
  This is the converse of theorem \ref{pythagoras}.
\end{note}
\end{document}

\documentclass{article}
\usepackage{amsmath}
\usepackage[amsmath,amsthm,thmmarks]{ntheorem}
\newtheorem{thm}{Theorem}
\newtheorem{lem}{Lemma}
\theoremstyle{definition}
\newtheorem{dfn}{Definition}
\theoremstyle{remark}
\newtheorem{note}{Note}
\begin{document}
\begin{dfn}
  The longest side of a triangle with a right angle
  is called the \emph{hypotenuse}.
\end{dfn}
\begin{note}
  The other sides are called \emph{catheti},
  or \emph{legs}.
\end{note}
\begin{thm}[Pythagoras]
  \label{pythagoras}
  In any right triangle, the square of the hypotenuse
  equals the sum of the squares of the other sides.
\end{thm}
\begin{proof}
  The proof has been given in Euclid's Elements,
  Book 1, Proposition 47. Refer to it for details.
  The converse is also true, see lemma \ref{converse}.
\end{proof}
\begin{lem}
  \label{converse}
  For any three positive numbers \(x\), \(y\),
  and \(z\) with \(x^2 + y^2 = z^2\), there is a
  triangle with side lengths \(x\), \(y\) and \(z\).
  Such triangle has a right angle, and the hypotenuse
  has the length \(z\).
\end{lem}
\begin{note}
  This is the converse of theorem \ref{pythagoras}.
\end{note}
\end{document}

\documentclass{article}
\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{thmtools}
\usepackage{xcolor}
\declaretheorem[shaded={bgcolor=red!15}]{Theorem}
\declaretheorem[thmbox=L]{Definition}
\begin{document}
\begin{Definition}
  The longest side of a triangle with a right angle
  is called the \emph{hypotenuse}.
\end{Definition}
\begin{Theorem}[Pythagoras]
  \label{pythagoras}
  In any right triangle, the square of the hypotenuse
  equals the sum of the squares of the other sides.
\end{Theorem}
\end{document}

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\usepackage{amsmath}
\DeclareMathOperator{\im}{im}
\begin{document}
\begin{tikzpicture}
  \matrix (m)
    [
      matrix of math nodes,
      row sep    = 3em,
      column sep = 4em
    ]
    {
      G              & \im \varphi \\
      G/\ker \varphi &             \\
    };
  \path[-stealth]
    (m-1-1) edge node [left] {$\pi$} (m-2-1)
    (m-1-1.east |- m-1-2)
      edge node [above] {$\varphi$} (m-1-2)
    (m-2-1) edge node [below] {$\tilde{\varphi}$}
      (m-1-2);
\end{tikzpicture}
\end{document}

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix,arrows.meta}
\usepackage{amsmath}
\DeclareMathOperator{\im}{im}
\begin{document}
\begin{tikzpicture}
  \matrix (m)
    [
      matrix of math nodes,
      row sep    = 3em,
      column sep = 4em
    ]
    {
      G              & \im \varphi \\
      G/\ker \varphi &             \\
    };
  \path
    (m-1-1) edge [->>] node [left] {$\pi$} (m-2-1)
    (m-1-1.east |- m-1-2)
      edge [->] node [above] {$\varphi$} (m-1-2)
    (m-2-1.east) edge [{Hooks[right,length=0.8ex]}->,
      dashed] node [below] {$\tilde{\varphi}$} (m-1-2);
\end{tikzpicture}
\end{document}

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix,calc}
\begin{document}
\begin{tikzpicture}[-stealth,
  label/.style = { font=\footnotesize }]
  \matrix (m)
    [
      matrix of math nodes,
      row sep    = 4em,
      column sep = 4em
    ]
    {
      A_0 & A_1 & A_2 & A_3 & A_4 \\
      B_0 & B_1 & B_2 & B_3 & B_4 \\
    };
  \foreach \i in {1,...,4} {
    \path
      let \n1 = { int(\i+1) } in
        (m-1-\i) edge node [above, label] {$f_\i$} (m-1-\n1)
        (m-2-\i) edge node [below, label] {$f^\prime_\i$} (m-2-\n1)
        (m-1-\i) edge node [left,  label] {$g_\i$} (m-2-\i);
  }
  \path (m-1-5) edge node [left, label] {$g_5$} (m-2-5);
\end{tikzpicture}
\end{document}

\documentclass[border=10pt]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
  \begin{axis} [axis lines=center]
    \addplot [domain=-3:3, smooth, thick] { x^3 - 5*x };
  \end{axis}
\end{tikzpicture}
\end{document}

\documentclass[border=10pt]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
  \begin{axis} [grid, xtick = {-360,-270,...,360}]
    \addplot [domain=-360:360, samples=100, thick] { sin(x) };
  \end{axis}
\end{tikzpicture}
\end{document}

\documentclass[border=10pt]{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{polar}
\begin{document}
\begin{tikzpicture}
  \begin{polaraxis}[hide axis]
    \addplot[domain=0:180,smooth] {sin(x)}; 
  \end{polaraxis}
\end{tikzpicture}
\end{document}

\documentclass[border=10pt]{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{polar}
\begin{document}
\begin{tikzpicture}
  \begin{polaraxis}
    \addplot[domain=0:360,samples=300] {sin(6*x)}; 
  \end{polaraxis}
\end{tikzpicture}
\end{document}

\documentclass[border=10pt]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis} [
    title = {$f(x,y) = \sin(x)\sin(y)$},
    xtick = {0,90,...,360},
    ytick = {90,180,...,360},
    xlabel = $x$, ylabel = $y$,
    ticklabel style = {font = \scriptsize},
	grid
]
\addplot3 [surf, domain=0:360, samples=60] 
	{ sin(x)*sin(y) };
\end{axis}
\end{tikzpicture}
\end{document}

\documentclass[border=10pt]{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{shift}
\begin{document}
\begin{tikzpicture}
  \begin{axis} [shift3d]
    \addplot3 [surf, colormap/hot2, domain = -2:2, samples = 50]
      { x/exp(x^2+y^2) };
  \end{axis}
\end{tikzpicture}
\end{document}

\documentclass[border=10pt]{standalone}
\usepackage{tkz-euclide}
\begin{document}
\begin{tikzpicture}
 \tkzDefPoint(0,0){A}
 \tkzDefPoint(4,1){B}
 \tkzInterCC(A,B)(B,A)
 \tkzGetPoints{C}{D}

 \tkzDrawPolygon(A,B,C)

 \tkzDrawPoints(A,B,C,D)
 \tkzLabelPoints[below left](A)
 \tkzLabelPoints(B,D)
 \tkzLabelPoint[above](C){$C$}

 \tkzDrawCircle[dotted](A,B)
 \tkzDrawCircle[dotted](B,A)

 \tkzCompass[color=red, very thick](A,C)
 \tkzCompass[color=red, very thick](B,C)

 \tkzCompass[color=red, very thick](A,D)
 \tkzCompass[color=red, very thick](B,D)
\end{tikzpicture}
\end{document} 

\documentclass[border=10pt]{standalone}
\usepackage{tkz-euclide}
\begin{document}
\begin{tikzpicture}
  \tkzDefPoint(0,0){A}
  \tkzDefPoint(4,1){B}
  \tkzInterCC(A,B)(B,A)
  \tkzGetPoints{C}{D}

  \tkzDrawPolygon(A,B,C)
  \tkzDrawPoints(A,B,C,D)

  \tkzLabelPoints[below left](A)
  \tkzLabelPoints(B,D)
  \tkzLabelPoint[above](C){$C$}

  \tkzDrawCircle[dotted](A,B)
  \tkzDrawCircle[dotted](B,A)

  \tkzCompass[color=red, very thick](A,C)
  \tkzCompass[color=red, very thick](B,C)

  \tkzCompass[color=red, very thick](A,D)
  \tkzCompass[color=red, very thick](B,D)

  \tkzDrawArc[fill=blue!10,thick](A,B)(C)
    \tkzDrawArc[fill=blue!10,thick](B,C)(A)
    \tkzDrawArc[fill=blue!10,thick](C,A)(B)

  \tkzInterLC(A,B)(B,A)
  \tkzGetPoints{F}{E}
  \tkzDrawPoints(E)
  \tkzLabelPoints(E)
  \tkzDrawPolygon(A,E,D)

  \tkzMarkAngles[fill=yellow,opacity=0.5](D,A,E A,E,D)
  \tkzMarkRightAngle[size=0.65,fill=red,opacity=0.5](A,D,E)
  \tkzLabelAngle[pos=0.7](D,A,E){$\alpha$}
  \tkzLabelAngle[pos=0.8](A,E,D){$\beta$}
  \tkzLabelAngle[pos=0.5,xshift=-1.4mm](A,D,D){$90^\circ$}

  \tkzLabelSegment[below=0.6cm,align=center,font=\small](A,B){Reuleaux\\triangle}
  \tkzLabelSegment[above right,sloped,font=\small](A,E){hypotenuse}
  \tkzLabelSegment[below,sloped,font=\small](D,E){opposite}
  \tkzLabelSegment[below,sloped,font=\small](A,D){adjacent}
  \tkzLabelSegment[below right=4cm,font=\small](A,E){Thales circle}
\end{tikzpicture}
\end{document} 

\documentclass[border=10pt]{standalone}
\usepackage{tkz-euclide}
\begin{document}
\begin{tikzpicture}
  \tkzDefPoints{0/0/A, 5/0/B, 1/4/C}
  \tkzDefCircle[in](A,B,C)
  \tkzGetPoint{M}
  \tkzGetLength{r}
  \tkzDefCircle[circum](A,B,C)
  \tkzGetPoint{N}
  \tkzGetLength{R}
  \tkzDefPointBy[projection=onto A--B](M)
  \tkzGetPoint{a}
  \tkzDefPointBy[projection=onto B--C](M)
  \tkzGetPoint{b}
  \tkzDefPointBy[projection=onto A--C](M)
  \tkzGetPoint{c}
  \tkzDrawCircle[R](M,\r pt)
  \tkzDrawCircle[R](N,\R pt)
  \tkzDrawPolygon[very thick](A,B,C)
  \tkzDrawLines[dotted](N,A N,B N,C)
  \tkzDrawLines[dashed](M,a M,b M,c)
  \tkzMarkRightAngles(M,a,B M,b,C M,c,C)
  \tkzDrawPoints(A,B,C,M,N,a,b,c)
  \tkzLabelPoints[below left](A,M,a,c)
  \tkzLabelPoints[below right](B)
  \tkzLabelPoints[above](C,b)
  \tkzLabelPoints[below](N)
\end{tikzpicture}

\end{document}
\documentclass[border=10pt]{standalone}
\usepackage{tkz-euclide}
\begin{document}
\begin{tikzpicture}
  \tkzDefPoints{0/0/A, 5/0/B, 1/4/C}
  \tkzDefCircle[in](A,B,C)
  \tkzGetPoint{M}
  \tkzGetLength{r}
  \tkzDefCircle[circum](A,B,C)
  \tkzGetPoint{N}
  \tkzGetLength{R}
  \tkzDefPointBy[projection=onto A--B](M)
  \tkzGetPoint{a}
  \tkzDefPointBy[projection=onto B--C](M)
  \tkzGetPoint{b}
  \tkzDefPointBy[projection=onto A--C](M)
  \tkzGetPoint{c}
  \tkzDrawCircle[R](M,\r pt)
  \tkzDrawCircle[R](N,\R pt)
  \tkzDrawPolygon[very thick](A,B,C)
  \tkzDrawLines[dotted](N,A N,B N,C)
  \tkzDrawLines[dashed](M,a M,b M,c)
  \tkzMarkRightAngles(M,a,B M,b,C M,c,C)
  \tkzDrawPoints(A,B,C,M,N,a,b,c)
  \tkzLabelPoints[below left](A,M,a,c)
  \tkzLabelPoints[below right](B)
  \tkzLabelPoints[above](C,b)
  \tkzLabelPoints[below](N)
\end{tikzpicture}
\end{document}

\documentclass{article}
\usepackage{tikz}
\pagestyle{empty}
\begin{document}
\noindent
In a right-angled triangle the two shortest sides
got widths of 3 and 7, respectively. Then, the
longest side has a width of \pgfmathparse{min(3^2 + 7^2)}\pgfmathresult.
The smallest angle is about \pgfmathparse{atan(3/7)}
\pgfmathprintnumber[precision=2]{\pgfmathresult} degrees.
\end{document}

Go to next chapter.