LaTeX/Mathematics

From testwiki
Revision as of 23:11, 11 February 2025 by imported>Marek5225 (Fractions and Binomials: Fixed a wrong fraction being displayed)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Template:LaTeX/Top °

One of the greatest motivating forces for Donald Knuth when he began developing the original TeX system was to create something that allowed simple construction of mathematical formulae, while it looking professional when printed. The fact that he succeeded was most probably why TeX (and later on, LaTeX) became so popular within the scientific community. Typesetting mathematics is one of LaTeX's greatest strengths. It is also a large topic due to the existence of so much mathematical notation.

If your document requires only a few simple mathematical formulas, plain LaTeX has most of the tools that you will ever need. If you are writing a scientific document that contains numerous complex formulas, the Template:LaTeX/Package package[1] introduces several new commands that are more powerful and flexible than the ones provided by basic LaTeX. The Template:LaTeX/Package package fixes some Template:LaTeX/Package quirks and adds some useful settings, symbols, and environments to amsmath.[2] To use either package, include:

\usepackage{amsmath}

or

\usepackage{mathtools}

in the preamble of the document. The Template:LaTeX/Package package loads the Template:LaTeX/Package package and hence there is no need to \usepackage{amsmath} in the preamble if Template:LaTeX/Package is used.

Mathematics environments

LaTeX needs to know when the text is mathematical. This is because LaTeX typesets math notation differently from normal text. Therefore, special environments have been declared for this purpose. They can be distinguished into two categories depending on how they are presented:

  • text — text formulas are displayed inline, that is, within the body of text where it is declared, for example, I can say that a+a=2a within this sentence.
  • displayed — displayed formulas are on a line by themselves.

As math requires special environments, there are naturally the appropriate environment names you can use in the standard way. Unlike most other environments, however, there are some handy shorthands for declaring your formulas. The following table summarizes them:

Type Inline (within text) formulas Displayed equations Displayed and automatically numbered equations
Environment Template:LaTeX/LaTeX Template:LaTeX/LaTeX Template:LaTeX/LaTeX
LaTeX shorthand Template:LaTeX/LaTeX Template:LaTeX/LaTeX
TeX shorthand Template:LaTeX/LaTeX Template:LaTeX/LaTeX
Comment Template:LaTeX/LaTeX (starred version) suppresses numbering, but requires amsmath

Suggestion: Using the Template:LaTeX/LaTeX should be avoided, as it may cause problems, particularly with the AMS-LaTeX macros. Furthermore, should a problem occur, the error messages may not be helpful.

The Template:LaTeX/LaTeX and Template:LaTeX/LaTeX environments are functionally equivalent.

If you are typing text normally, you are said to be in text mode, but while you are typing within one of those mathematical environments, you are said to be in math mode, that has some differences compared to the text mode:

  1. Most spaces and line breaks do not have any significance, as all spaces are either derived logically from the mathematical expressions or have to be specified with special commands such as Template:LaTeX/LaTeX
  2. Empty lines are not allowed. Only one paragraph per formula.
  3. Each letter is considered to be the name of a variable and will be typeset as such. If you want to typeset normal text within a formula (normal upright font with normal spacing), then you have to enter the text using dedicated commands.

Inserting "Displayed" maths inside blocks of text

In order for some operators, such as Template:LaTeX/LaTeX or Template:LaTeX/LaTeX, to be displayed correctly inside some math environments (read Template:LaTeX/LaTeX), it might be convenient to write the Template:LaTeX/LaTeX class inside the environment. Doing so might cause the line to be taller, but will cause exponents and indices to be displayed correctly for some math operators. For example, the Template:LaTeX/LaTeX will print a smaller Σ and Template:LaTeX/LaTeX will print a bigger one , like in equations.Template:Efn It is possible to force this behaviour for all math environments by declaring Template:LaTeX/LaTeX in the preamble (i.e. before Template:LaTeX/LaTeX).

Symbols

Mathematics has many symbols! The following is a set of symbols that can be accessed directly from the keyboard:

+ - = ! / ( ) [ ] < > | ' : *

Beyond those listed above, distinct commands must be issued in order to display the desired symbols. There are many examples such as Greek letters, set and relations symbols, arrows, binary operators, etc.

For example:

\forall x \in X, \quad \exists y \leq \epsilon

xX,yϵ

Template:BookCat

Fortunately, there's a tool that can greatly simplify the search for the command for a specific symbol. Look for "Detexify" in the external links section below. Another option would be to look in "The Comprehensive LaTeX Symbol List" in the external links section below.

Greek letters

Greek letters are commonly used in mathematics, and they are very easy to type in math mode. You just have to type the name of the letter after a backslash: if the first letter is lowercase, you will get a lowercase Greek letter, if the first letter is uppercase (and only the first letter), then you will get an uppercase letter. Note that some uppercase Greek letters look like Latin ones, so they are not provided by LaTeX (e.g. uppercase Alpha and Beta are just "A" and "B", respectively). Lowercase epsilon, theta, kappa, phi, pi, rho, and sigma are provided in two different versions. The alternate, or variant, version is created by adding "var" before the name of the letter:

\alpha, \Alpha, \beta, \Beta, \gamma, \Gamma, \pi, \Pi, \phi, \varphi, \mu, \Phi

α,A,β,B,γ,Γ,π,Π,ϕ,φ,μ,Φ

Template:BookCat

Scroll down to #List of mathematical symbols for a complete list of Greek symbols.

Operators

An operator is a function that is written as a word: e.g. trigonometric functions (sin, cos, tan), logarithms and exponentials (log, exp), limits (lim), as well as trace and determinant (tr, det). LaTeX has many of these defined as commands:

\cos (2\theta) = \cos^2 \theta - \sin^2 \theta

cos(2θ)=cos2θsin2θ

Template:BookCat

For certain operators such as limits, the subscript is placed underneath the operator:

\lim\limits_{x \to \infty} \exp(-x) = 0

limxexp(x)=0

Template:BookCat

For the modular operator there are two commands: Template:LaTeX/LaTeX and Template:LaTeX/LaTeX:

a \bmod b

amodb

Template:BookCat

x \equiv a \pmod{b}

xa(modb)

Template:BookCat

To use operators that are not pre-defined, such as argmax, see [[../Advanced Mathematics#Custom operators|custom operators]]

Powers and indices

Powers and indices are equivalent to superscripts and subscripts in normal text mode. The caret (^; also known as the circumflex accent) character is used to raise something, and the underscore (_) is for lowering. If an expression containing more than one character is raised or lowered, it should be grouped using curly braces ({ and }).

k_{n+1} = n^2 + k_n^2 - k_{n-1}

kn+1=n2+kn2kn1

Template:BookCat

For powers with more than one digit, surround the power with {}.

x^{1.01}

x1.01

Template:BookCat

An underscore (_) can be used with a vertical bar (|) to denote evaluation using subscript notation in mathematics:

f(n) = n^5 + 4n^2 + 2 |_{n=17}

f(n)=n5+4n2+2|n=17

Template:BookCat

Fractions and Binomials

A fraction is created using the Template:LaTeX/LaTeX command (for those who need their memories refreshed, that's the top and bottom respectively!). Likewise, the binomial coefficient (a.k.a, the Choose function) may be written using the Template:LaTeX/LaTeX command:Template:Efn

\frac{n!}{k!(n-k)!} = \binom{n}{k}

n!k!(nk)!=(nk)

Template:BookCat


You can embed fractions within fractions:

\frac{\frac{1}{x}+\frac{1}{y}}{y-z}

1x+1yyz

Template:BookCat

Note that when appearing inside another fraction, or in inline text ab, a fraction is noticeably smaller than in displayed mathematics. The Template:LaTeX/LaTeX and Template:LaTeX/LaTeX commandsTemplate:Efn force the use of the respective styles, Template:LaTeX/LaTeX and Template:LaTeX/LaTeX. Similarly, the Template:LaTeX/LaTeX and Template:LaTeX/LaTeX commands typeset the binomial coefficient.

For relatively simple fractions, especially within the text, it may be more aesthetically pleasing to use powers and indices instead:

^3/_7

3/7

Template:BookCat

If this looks a little "loose" (i.e., overspaced), a tightened version can be defined by inserting some negative space

%running fraction with slash - requires math mode.
\newcommand*\rfrac[2]{{}^{#1}\!/_{#2}}

\rfrac{3}{7}

3/7

Template:BookCat

If you use them throughout the document, usage of Template:LaTeX/Package package is recommended. This package provides Template:LaTeX/LaTeX command to create slanted fractions. Usage:

Take $\sfrac{1}{2}$ cup of sugar, \dots
 
  3\times\sfrac{1}{2}=1\sfrac{1}{2}
 

Take ${}^1/_2$ cup of sugar, \dots
 
  3\times{}^1/_2=1{}^1/_2

Template:BookCat

If fractions are used as an exponent, curly braces have to be used around the Template:LaTeX/LaTeX command:

 $x^\frac{1}{2}$ % no error
 $x^\sfrac{1}{2}$ % error
 $x^{\sfrac{1}{2}}$ % no error
$x^\frac{1}{2}$ % no error

t12

Template:BookCat

In some cases, using the package alone will result in errors about certain font shapes not being available. In that case, the Template:LaTeX/Package and Template:LaTeX/Package packages need to be added as well.

Alternatively, the Template:LaTeX/Package package provides the Template:LaTeX/LaTeX command, whose usage is similar to Template:LaTeX/LaTeX.

Continued fractions

Continued fractions should be written using Template:LaTeX/LaTeX command:Template:Efn

\begin{equation}
  x = a_0 + \cfrac{1}{a_1 
          + \cfrac{1}{a_2 
          + \cfrac{1}{a_3 + \cfrac{1}{a_4} } } }
\end{equation}

x=a0+1a1+1a2+1a3+1a4

Template:BookCat

Multiplication of two numbers

To make multiplication visually similar to a fraction, a nested array can be used. For example, multiplication of numbers written one below the other can be typeset as follows:

\begin{equation}
\frac{
    \begin{array}[b]{r}
      \left( x_1 x_2 \right)\\
      \times \left( x'_1 x'_2 \right)
    \end{array}
  }{
    \left( y_1y_2y_3y_4 \right)
  }
\end{equation}

(x1x2)×(x'1x'2)(y1y2y3y4)

Template:BookCat

Roots

The Template:LaTeX/LaTeX command creates a square root surrounding an expression. It accepts an optional argument specified in square brackets ([ and ]) to change magnitude:

\sqrt{\frac{a}{b}}

ab

Template:BookCat

\sqrt[n]{1+x+x^2+x^3+\dots+x^n}

1+x+x2+x3++xnn

Template:BookCat

Some people prefer writing the square root "closing" it over its content. This method arguably makes it more clear what is in the scope of the root sign. This habit is not normally used while writing with the computer, but if you still want to change the output of the square root, LaTeX gives you this possibility. Just add the following code in the preamble of your document:

% New definition of square root:
% it renames \sqrt as \oldsqrt
\let\oldsqrt\sqrt
% it defines the new \sqrt in terms of the old one
\def\sqrt{\mathpalette\DHLhksqrt}
\def\DHLhksqrt#1#2{%
\setbox0=\hbox{$#1\oldsqrt{#2\,}$}\dimen0=\ht0
\advance\dimen0-0.2\ht0
\setbox2=\hbox{\vrule height\ht0 depth -\dimen0}%
{\box0\lower0.4pt\box2}}
The new style is on left, the old one on right

Template:BookCat

This TeX code first renames the Template:LaTeX/LaTeX command as Template:LaTeX/LaTeX, then redefines Template:LaTeX/LaTeX in terms of the old one, adding something more. The new square root can be seen in the picture on the left, compared to the old one on the right. Unfortunately this code won't work if you want to use multiple roots: if you try to write ab as Template:LaTeX/LaTeX after you used the code above, you'll just get a wrong output. In other words, you can redefine the square root this way only if you are not going to use multiple roots in the whole document.

An alternative piece of TeX code that does allow multiple roots is

\usepackage{letltxmacro}
\makeatletter
\let\oldr@@t\r@@t
\def\r@@t#1#2{%
\setbox0=\hbox{$\oldr@@t#1{#2\,}$}\dimen0=\ht0
\advance\dimen0-0.2\ht0
\setbox2=\hbox{\vrule height\ht0 depth -\dimen0}%
{\box0\lower0.4pt\box2}}
\LetLtxMacro{\oldsqrt}{\sqrt}
\renewcommand*{\sqrt}[2][\ ]{\oldsqrt[#1]{#2} }
\makeatother


$\sqrt[a]{b} \quad \oldsqrt[a]{b}$

Template:BookCat

However, this requires the Template:LaTeX/LaTeX package.

Sums and integrals

The Template:LaTeX/LaTeX and Template:LaTeX/LaTeX commands insert the sum and integral symbols respectively, with limits specified using the caret (^) and underscore (_). The typical notation for sums is:

\sum_{i=1}^{10} t_i

i=110ti

Template:BookCat

or

\displaystyle\sum_{i=1}^{10} t_i

i=110ti

Template:BookCat

The limits for the integrals follow the same notation. It's also important to represent the integration variables with an upright d, which in math mode is obtained through the \mathrm{} command, and with a small space separating it from the integrand, which is attained with the \, command.

\int_0^\infty \mathrm{e}^{-x}\,\mathrm{d}x

0exdx

Template:BookCat

There are many other "big" commands which operate in a similar manner:

\sum \prod \coprod
\bigoplus \bigotimes \bigodot
\bigcup \bigcap \biguplus
\bigsqcup \bigvee \bigwedge
\int \oint \iintTemplate:Efn
\iiintTemplate:Efn \iiiintTemplate:Efn \idotsintTemplate:Efn

For more integral symbols, including those not included by default in the Computer Modern font, try the Template:LaTeX/Package package.

The Template:LaTeX/LaTeX commandTemplate:Efn allows the use of Template:LaTeX/LaTeX to write the limits over multiple lines:

\sum_{\substack{
   0<i<m \\
   0<j<n
  }} 
 P(i,j)

0<j<n0<i<mP(i,j)

Template:BookCat

If you want the limits of an integral to be specified above and below the symbol (like the sum), use the Template:LaTeX/LaTeX command:

\int\limits_a^b

bc

Template:BookCat

However, if you want this to apply to all integrals, it is preferable to specify the Template:LaTeX/Parameter option when loading the Template:LaTeX/Package package: Template:LaTeX/Usage

Subscripts and superscripts in other contexts, as well as other parameters to Template:LaTeX/Package package related to them, are described in Advanced Mathematics chapter.

For bigger integrals, you may use personal declarations, or the Template:LaTeX/Package package [3].

Brackets, braces and delimiters

How to use braces in multi line equations is described in the Advanced Mathematics chapter.

The use of delimiters such as brackets soon becomes important when dealing with anything but the most trivial equations. Without them, formulas can become ambiguous. Also, special types of mathematical structures, such as matrices, typically rely on delimiters to enclose them.

There are a variety of delimiters available for use in LaTeX:

( a ), [ b ], \{ c \}, | d |, \| e \|,
\langle f \rangle, \lfloor g \rfloor,
\lceil h \rceil, \ulcorner i \urcorner,
/ j \backslash

(a),[b],{c},|d|,e,f,g,h,i,/j

Template:BookCat

where Template:LaTeX/LaTeX and Template:LaTeX/LaTeX may be used in place of [ and ].


Automatic sizing

Very often, mathematical features will differ in size, in which case the delimiters surrounding the expression should vary accordingly. This can be done automatically using the Template:LaTeX/LaTeX, Template:LaTeX/LaTeX, and Template:LaTeX/LaTeX commands. Any of the previous delimiters may be used in combination with these:

\left(\frac{x^2}{y^3}\right)

(x2y3)

Template:BookCat

P\left(A=2\middle|\frac{A^2}{B}>4\right)

Template:BookCat

Curly braces are defined differently by using Template:LaTeX/LaTeX and Template:LaTeX/LaTeX,

\left\{\frac{x^2}{y^3}\right\}

{x2y3}

Template:BookCat

If a delimiter on only one side of an expression is required, then an invisible delimiter on the other side may be denoted using a period (.).

\left.\frac{x^3}{3}\right|_0^1

x33|01

Template:BookCat

Manual sizing

In certain cases, the sizing produced by the Template:LaTeX/LaTeX and Template:LaTeX/LaTeX commands may not be desirable, or you may simply want finer control over the delimiter sizes. In this case, the Template:LaTeX/LaTeX, Template:LaTeX/LaTeX, Template:LaTeX/LaTeX and Template:LaTeX/LaTeX modifier commands may be used:

( \big( \Big( \bigg( \Bigg(

(((((

Template:BookCat

These commands are primarily useful when dealing with nested delimiters. For example, when typesetting

\frac{\mathrm d}{\mathrm d x} \left( k g(x) \right)

ddx(kg(x))

Template:BookCat

we notice that the Template:LaTeX/LaTeX and Template:LaTeX/LaTeX commands produce the same size delimiters as those nested within it. This can be difficult to read. To fix this, we write

\frac{\mathrm d}{\mathrm d x} \big( k g(x) \big)

ddx(kg(x))

Template:BookCat

Manual sizing can also be useful when an equation is too large, trails off the end of the page, and must be separated into two lines using an align command. Although the commands Template:LaTeX/LaTeX and Template:LaTeX/LaTeX can be used to balance the delimiters on each line, this may lead to wrong delimiter sizes. Furthermore, manual sizing can be used to avoid overly large delimiters — if an Template:LaTeX/LaTeX or a similar command appears between the delimiters.

Matrices and arrays

A basic matrix may be created using the Template:LaTeX/Environment environmentTemplate:Efn: in common with other table-like structures, entries are specified by row, with columns separated using an ampersand (Template:LaTeX/LaTeX) and new rows separated with a double backslash (Template:LaTeX/LaTeX)

\[
 \begin{matrix}
  a & b & c \\
  d & e & f \\
  g & h & i
 \end{matrix}
\]

abcdefghi

Template:BookCat

To specify alignment of columns in the table, use starred versionTemplate:Efn:

\begin{matrix}
  -1 & 3 \\
  2 & -4
 \end{matrix}
 =
 \begin{matrix*}[r]
  -1 & 3 \\
  2 & -4
 \end{matrix*}

1324=1324

Template:BookCat

The alignment by default is Template:LaTeX/Parameter, but it can be any column type valid in Template:LaTeX/Environment environment.

However matrices are usually enclosed in delimiters of some kind, and while it is possible to use the \left and \right commands, there are various other predefined environments which automatically include delimiters:

Environment name Surrounding delimiter Notes
Template:LaTeX/EnvironmentTemplate:Efn () centers columns by default
Template:LaTeX/EnvironmentTemplate:Efn () allows to specify alignment of columns in optional parameter
Template:LaTeX/EnvironmentTemplate:Efn [] centers columns by default
Template:LaTeX/EnvironmentTemplate:Efn [] allows to specify alignment of columns in optional parameter
Template:LaTeX/EnvironmentTemplate:Efn {} centers columns by default
Template:LaTeX/EnvironmentTemplate:Efn {} allows to specify alignment of columns in optional parameter
Template:LaTeX/EnvironmentTemplate:Efn || centers columns by default
Template:LaTeX/EnvironmentTemplate:Efn || allows to specify alignment of columns in optional parameter
Template:LaTeX/EnvironmentTemplate:Efn centers columns by default
Template:LaTeX/EnvironmentTemplate:Efn allows to specify alignment of columns in optional parameter

When writing down arbitrary sized matrices, it is common to use horizontal, vertical and diagonal triplets of dots (known as ellipses) to fill in certain columns and rows. These can be specified using the Template:LaTeX/LaTeX, Template:LaTeX/LaTeX and Template:LaTeX/LaTeX respectively:

A_{m,n} = 
 \begin{pmatrix}
  a_{1,1} & a_{1,2} & \cdots & a_{1,n} \\
  a_{2,1} & a_{2,2} & \cdots & a_{2,n} \\
  \vdots  & \vdots  & \ddots & \vdots  \\
  a_{m,1} & a_{m,2} & \cdots & a_{m,n} 
 \end{pmatrix}

Am,n=(a1,1a1,2a1,na2,1a2,2a2,nam,1am,2am,n)

Template:BookCat

In some cases, you may want to have finer control of the alignment within each column, or to insert lines between columns or rows. This can be achieved using the Template:LaTeX/Environment environment, which is essentially a math-mode version of the [[../Tables#The tabular environment|tabular environment]], which requires that the columns be pre-specified:

\begin{array}{c|c}
  1 & 2 \\ 
  \hline
  3 & 4
 \end{array}

1234

Template:BookCat

You may see that the AMS matrix class of environments doesn't leave enough space when used together with fractions resulting in output similar to this:

M=[561605601605616]

To counteract this problem, add additional leading space with the optional parameter to the Template:LaTeX/LaTeX command:

M = \begin{bmatrix}
       \frac{5}{6} & \frac{1}{6} & 0           \\[0.3em]
       \frac{5}{6} & 0           & \frac{1}{6} \\[0.3em]
       0           & \frac{5}{6} & \frac{1}{6}
     \end{bmatrix}

M=[561605601605616]

Template:BookCat

If you need "border" or "indexes" on your matrix, plain TeX provides the macro Template:LaTeX/LaTeX

M = \bordermatrix{~ & x & y \cr
                  A & 1 & 0 \cr
                  B & 0 & 1 \cr}

Template:BookCat


Matrices in running text

To insert a small matrix without increasing leading in the line containing it, use Template:LaTeX/Environment environment:

A matrix in text must be set smaller:
$\bigl(\begin{smallmatrix}
a&b \\ c&d
\end{smallmatrix} \bigr)$
to not increase leading in a portion of text.

Template:BookCat

Adding text to equations

The math environment differs from the text environment in the representation of text. Here is an example of trying to represent text within the math environment:

50 apples \times 100 apples = lots of apples^2

50apples×100apples=lotsofapples2

Template:BookCat

There are two noticeable problems: there are no spaces between words or numbers, and the letters are italicized and more spaced out than normal. Both issues are simply artifacts of the maths mode, in that it treats it as a mathematical expression: spaces are ignored (LaTeX spaces mathematics according to its own rules), and each character is a separate element (so are not positioned as closely as normal text).

There are a number of ways that text can be added properly. The typical way is to wrap the text with the Template:LaTeX/LaTeX commandTemplate:Efn (a similar command is Template:LaTeX/LaTeX, though this causes problems with subscripts, and has a less descriptive name). Let's see what happens when the above equation code is adapted:

50 \text{apples} \times 100 \text{apples} 
 = \text{lots of apples}^2

50apples×100apples=lots of apples2

Template:BookCat

The text looks better. However, there are no gaps between the numbers and the words. Unfortunately, you are required to explicitly add these. There are many ways to add spaces between math elements, but for the sake of simplicity we may simply insert space characters into the Template:LaTeX/LaTeX commands.

50 \text{ apples} \times 100 \text{ apples}
 = \text{lots of apples}^2

50 apples×100 apples=lots of apples2

Template:BookCat

Formatted text

Using the Template:LaTeX/LaTeX is fine and gets the basic result. Yet, there is an alternative that offers a little more flexibility. You may recall the introduction of font formatting commands, such as Template:LaTeX/LaTeX, Template:LaTeX/LaTeX, Template:LaTeX/LaTeX, etc. These commands format the argument accordingly, e.g., Template:LaTeX/LaTeX gives bold text. These commands are equally valid within a maths environment to include text. The added benefit here is that you can have better control over the font formatting, rather than the standard text achieved with Template:LaTeX/LaTeX.

50 \textrm{ apples} \times 100
 \textbf{ apples} = \textit{lots of apples}^2

50apples×100apples=lotsofapples2

Template:BookCat

Formatting mathematics symbols

See also: w:Mathematical Alphanumeric Symbols, w:Help:Displaying a formula#Alphabets and typefaces and w:Wikipedia:LaTeX symbols#Fonts

We can now format text; what about formatting mathematical expressions? There are a set of formatting commands very similar to the font formatting ones just used, except that they are specifically aimed at text in math mode (requires Template:LaTeX/Package)

LaTeX command Sample Description Common use
Template:LaTeX/LaTeX
(or simply omit any command)
ABCDEFabcdef123456 The default math font Most mathematical notation
Template:LaTeX/LaTeX ABCDEFabcdef123456 This is the default or normal font, unitalicised Units of measurement, one word functions
Template:LaTeX/LaTeX 𝐴𝐵𝐶𝐷𝐸𝐹𝑎𝑏𝑐𝑑𝑒𝑓123456 Italicised font Multi-letter function or variable names. Compared to \mathnormal, words are spaced more naturally and numbers are italicized as well.
Template:LaTeX/LaTeX 𝐀𝐁𝐂𝐃𝐄𝐅𝐚𝐛𝐜𝐝𝐞𝐟𝟏𝟐𝟑𝟒𝟓𝟔 Bold font Vectors
Template:LaTeX/LaTeX 𝖠𝖡𝖢𝖣𝖤𝖥𝖺𝖻𝖼𝖽𝖾𝖿𝟣𝟤𝟥𝟦𝟧𝟨 Sans-serif Categories
Template:LaTeX/LaTeX 𝙰𝙱𝙲𝙳𝙴𝙵𝚊𝚋𝚌𝚍𝚎𝚏𝟷𝟸𝟹𝟺𝟻𝟼 Monospace (fixed-width) font
Template:LaTeX/LaTeX
(requires the Template:LaTeX/Package or Template:LaTeX/Package packageTemplate:Efn)
𝔄𝔅𝔇𝔈𝔉𝔞𝔟𝔠𝔡𝔢𝔣123456 Fraktur Almost canonical font for Lie algebras, ideals in ring theory
Template:LaTeX/LaTeX 𝒜𝒞𝒟 Calligraphy (uppercase onlyTemplate:Efn) Often used for sheaves/schemes and categories, used to denote cryptological concepts like an alphabet of definition (𝒜), message space (), ciphertext space (𝒞) and key space (𝒦); Kleene's 𝒪; naming convention in description logic; Laplace transform () and Fourier transform ()
Template:LaTeX/LaTeX
(requires the Template:LaTeX/Package or Template:LaTeX/Package packageTemplate:Efn)
𝔸𝔹𝔻𝔼𝔽 Blackboard bold (uppercase onlyTemplate:Efn) Used to denote special sets (e.g. real numbers)
Template:LaTeX/LaTeX
(requires the Template:LaTeX/Package packageTemplate:Efn)
Script (uppercase onlyTemplate:Efn) An alternative font for categories and sheaves.

These formatting commands can be wrapped around the entire equation, and not just on the textual elements: they only format letters, numbers, and uppercase Greek, and other math commands are unaffected.

To bold lowercase Greek or other symbols use the Template:LaTeX/LaTeX commandTemplate:Efn; this will only work if there exists a bold version of the symbol in the current font. As a last resort there is the Template:LaTeX/LaTeX commandTemplate:Efn (poor man's bold): this prints multiple versions of the character slightly offset against each other.

\boldsymbol{\beta} = (\beta_1,\beta_2,\dotsc,\beta_n)

β=(β1,β2,,βn)

Template:BookCat

To change the size of the fonts in math mode, see [[../Advanced Mathematics#Changing font size|Changing font size]].

Accents

So what to do when you run out of symbols and fonts? Well, the next step is to use accents:

a' or a^{\prime} a a'' a
\hat{a} a^ \bar{a} a¯
\grave{a} a` \acute{a} a´
\dot{a} a˙ \ddot{a} a¨
\not{a} a \mathring{a} å
\overrightarrow{AB} AB \overleftarrow{AB} AB
a''' a a'''' a
\overline{aaa} aaa \check{a} aˇ
\breve{a} a˘ \vec{a} a
\dddot{a}Template:Efn \ddddot{a}Template:Efn
\widehat{AAA} AAA^ \widetilde{AAA} AAA~
\stackrel\frown{AAA} AAA
\tilde{a} a~ \underline{a} a_

Color

The package Template:LaTeX/Package, described in [[../Colors#Adding_the_color_package|Colors]], allows us to add color to our equations. For example,

k = {\color{red}x} \mathbin{\color{blue}-} 2

k=x2

Template:BookCat

The only problem is that this disrupts the default Template:LaTeX formatting around the Template:LaTeX/LaTeX operator. To fix this, we enclose it in a Template:LaTeX/LaTeX environment, since Template:LaTeX/LaTeX is a binary operator. This process is described here.

Plus and minus signs

LaTeX deals with the + and − signs in two possible ways. The most common is as a binary operator. When two maths elements appear on either side of the sign, it is assumed to be a binary operator, and as such, allocates some space to either side of the sign. The alternative way is a sign designation. This is when you state whether a mathematical quantity is either positive or negative. This is common for the latter, as in math, such elements are assumed to be positive unless a − is prefixed to it. In this instance, you want the sign to appear close to the appropriate element to show their association. If you put a + or a − with nothing before it but you want it to be handled like a binary operator you can add an invisible character before the operator using Template:LaTeX/LaTeX. This can be useful if you are writing multiple-line formulas, and a new line could start with a − or +, for example, then you can fix some strange alignments adding the invisible character where necessary.

A plus-minus sign is written as:

\pm

±

Template:BookCat

Similarly, there exists also a minus-plus sign:

\mp

Template:BookCat

Controlling horizontal spacing

LaTeX is obviously pretty good at typesetting maths—it was one of the chief aims of the core TeX system that LaTeX extends. However, it can't always be relied upon to accurately interpret formulas in the way you did. It has to make certain assumptions when there are ambiguous expressions. The result tends to be slightly incorrect horizontal spacing. In these events, the output is still satisfactory, yet any perfectionists will no doubt wish to fine-tune their formulas to ensure spacing is correct. These are generally very subtle adjustments.

There are other occasions where LaTeX has done its job correctly, but you just want to add some space, maybe to add a comment of some kind. For example, in the following equation, it is preferable to ensure there is a decent amount of space between the maths and the text.

\[ f(n) =
  \begin{cases}
    n/2       & \quad \text{if } n \text{ is even}\\
    -(n+1)/2  & \quad \text{if } n \text{ is odd}
  \end{cases}
\]

f(n)={n/2if n is even(n+1)/2if n is odd

Template:BookCat

This code produces errors with Miktex 2.9 and does not yield the results seen on the right. Use \mathrm instead of just \text.


(Note that this particular example can be expressed in more elegant code by the Template:LaTeX/Environment construct provided by the Template:LaTeX/Package package described in Advanced Mathematics chapter.)

LaTeX has defined two commands that can be used anywhere in documents (not just maths) to insert some horizontal space. They are Template:LaTeX/LaTeX and Template:LaTeX/LaTeX

A Template:LaTeX/LaTeX is a space equal to the current font size. So, if you are using an 11pt font, then the space provided by Template:LaTeX/LaTeX will also be 11pt (horizontally, of course.) The Template:LaTeX/LaTeX gives twice that amount. As you can see from the code from the above example, Template:LaTeX/LaTeXs were used to add some separation between the maths and the text.

OK, so back to the fine tuning as mentioned at the beginning of the document. A good example would be displaying the simple equation for the indefinite integral of y with respect to x:

ydx

If you were to try this, you may write:

\int y \mathrm{d}x

ydx

Template:BookCat

However, this doesn't give the correct result. LaTeX doesn't respect the white-space left in the code to signify that the y and the dx are independent entities. Instead, it lumps them altogether. A Template:LaTeX/LaTeX would clearly be overkill in this situation—what is needed are some small spaces to be utilized in this type of instance, and that's what LaTeX provides:

Command Description Size
Template:LaTeX/LaTeX small space 3/18 of a quad
Template:LaTeX/LaTeX medium space 4/18 of a quad
Template:LaTeX/LaTeX large space 5/18 of a quad
Template:LaTeX/LaTeX negative space -3/18 of a quad

NB you can use more than one command in a sequence to achieve a greater space if necessary.

So, to rectify the current problem:

\int y\, \mathrm{d}x

ydx

Template:BookCat

\int y\: \mathrm{d}x

ydx

Template:BookCat

\int y\; \mathrm{d}x

ydx

Template:BookCat

The negative space may seem like an odd thing to use, however, it wouldn't be there if it didn't have some use! Take the following example:

\left(
    \begin{array}{c}
      n \\
      r
    \end{array}
  \right) = \frac{n!}{r!(n-r)!}

(nr)=n!r!(nr)!

Template:BookCat


The matrix-like expression for representing binomial coefficients is too padded. There is too much space between the brackets and the actual contents within. This can easily be corrected by adding a few negative spaces after the left bracket and before the right bracket.

\left(\!
    \begin{array}{c}
      n \\
      r
    \end{array}
  \!\right) = \frac{n!}{r!(n-r)!}

(nr)=n!r!(nr)!

Template:BookCat

In any case, adding some spaces manually should be avoided whenever possible: it makes the source code more complex and it's against the basic principles of a What You See is What You Mean approach. The best thing to do is to define some commands using all the spaces you want and then, when you use your command, you don't have to add any other space. Later, if you change your mind about the length of the horizontal space, you can easily change it modifying only the command you defined before. Let us use an example: you want the d of a dx in an integral to be in roman font and a small space away from the rest. If you want to type an integral like Template:LaTeX/LaTeX, you can define a command like this: Template:LaTeX/Usage in the preamble of your document. We have chosen Template:LaTeX/LaTeX just because it reminds the "d" it replaces and it is fast to type. Doing so, the code for your integral becomes Template:LaTeX/LaTeX. Now, whenever you write an integral, you just have to use the Template:LaTeX/LaTeX instead of the "d", and all your integrals will have the same style. If you change your mind, you just have to change the definition in the preamble, and all your integrals will be changed accordingly.

Manually Specifying Formula Style

To manually display a fragment of a formula using text style, surround the fragment with curly braces and prefix the fragment with Template:LaTeX/LaTeX. The braces are required because the Template:LaTeX/LaTeX macro changes the state of the renderer, rendering all subsequent mathematics in text style. The braces limit this change of state to just the fragment enclosed within. For example, to use text style for just the summation symbol in a sum, one would enter Template:LaTeX/Usage The same thing as a command would look like this: Template:LaTeX/Usage Note the extra braces. Just one set around the expression won't be enough. That would cause all math after Template:LaTeX/LaTeX to be displayed using text style.

To display part of a formula using display style, do the same thing, but use Template:LaTeX/LaTeX instead.

Advanced Mathematics: AMS Math package

The AMS (American Mathematical Society) mathematics package is a powerful package that creates a higher layer of abstraction over mathematical LaTeX language; if you use it it will make your life easier. Some commands Template:LaTeX/Package introduces will make other plain LaTeX commands obsolete: in order to keep consistency in the final output you'd better use Template:LaTeX/Package commands whenever possible. If you do so, you will get an elegant output without worrying about alignment and other details, keeping your source code readable. If you want to use it, you have to add this in the preamble: Template:LaTeX/Usage

Introducing dots in formulas

Template:LaTeX/Package defines also the Template:LaTeX/LaTeX command, that is a generalization of the existing Template:LaTeX/LaTeX. You can use Template:LaTeX/LaTeX in both text and math mode and LaTeX will replace it with three dots "…" but it will decide according to the context whether to put it on the bottom (like Template:LaTeX/LaTeX) or centered (like Template:LaTeX/LaTeX).

Dots

LaTeX gives you several commands to insert dots (ellipses) in your formulae. This can be particularly useful if you have to type big matrices omitting elements. First of all, here are the main dots-related commands LaTeX provides:

Code Output Comment
Template:LaTeX/LaTeX generic dots (ellipsis), to be used in text (outside formulae as well). It automatically manages whitespaces before and after itself according to the context, it's a higher level command.
Template:LaTeX/LaTeX the output is similar to the previous one, but there is no automatic whitespace management; it works at a lower level.
Template:LaTeX/LaTeX These dots are centered relative to the height of a letter. There is also the binary multiplication operator, \cdot, mentioned below.
Template:LaTeX/LaTeX vertical dots
Template:LaTeX/LaTeX diagonal dots
Template:LaTeX/LaTeX inverse diagonal dots (requires the Template:LaTeX/Package package)
Template:LaTeX/LaTeX to be used in matrices, it creates a row of dots spanning n columns.

Instead of using Template:LaTeX/LaTeX and Template:LaTeX/LaTeX, you should use the semantically oriented commands. It makes it possible to adapt your document to different conventions on the fly, in case (for example) you have to submit it to a publisher who insists on following house tradition in this respect. The default treatment for the various kinds follows American Mathematical Society conventions.

Code Output Comment
Template:LaTeX/LaTeX for "dots with commas"
Template:LaTeX/LaTeX for "dots with binary operators/relations"
Template:LaTeX/LaTeX for "multiplication dots"
Template:LaTeX/LaTeX for "dots with integrals"
Template:LaTeX/LaTeX for "other dots" (none of the above)

Write an equation with the align environment

How to write an equation with the align environment with the Template:LaTeX/Package package is described in Advanced Mathematics.

List of mathematical symbols

All the pre-defined mathematical symbols from the \TeX\ package are listed below. More symbols are available from extra packages.

Relation Symbols
Symbol Script   Symbol Script   Symbol Script   Symbol Script   Symbol Script
< < > > = = \parallel \nparallel
\leq \geq \doteq \asymp \bowtie
\ll \gg \equiv \vdash \dashv
\subset \supset \approx \in \ni
\subseteq \supseteq \cong \smile \frown
\nsubseteq \nsupseteq \simeq \models \notin
\sqsubset \sqsupset \sim \perp \mid
\sqsubseteq \sqsupseteq \propto \prec \succ
\preceq \succeq \neq \sphericalangle \measuredangle
\therefore \because
Binary Operations
Symbol Script   Symbol Script   Symbol Script   Symbol Script
± \pm \cap \diamond \oplus
\mp \cup \bigtriangleup \ominus
× \times \uplus \bigtriangledown \otimes
÷ \div \sqcap \triangleleft \oslash
\ast \sqcup \triangleright \odot
\star \vee \bigcirc \circ
\dagger \wedge \bullet \setminus
\ddagger \cdot \wr ⨿ \amalg
Set and/or Logic Notation
Symbol Script   Symbol Script
\exists \rightarrow or \to
\nexists \leftarrow or \gets
\forall \mapsto
¬ \neg \implies
\cap
\cup
\subset \impliedby
\supset \Rightarrow or \implies
\in \leftrightarrow
\notin \iff
\ni \Leftrightarrow (preferred for equivalence (iff))
\land \top
\lor \bot
\angle and \emptyset and \varnothingTemplate:Ref
\rightleftharpoons
Delimiters
Symbol Script   Symbol Script   Symbol Script   Symbol Script
| | or \mid (difference in spacing) \| / / \backslash
{ \{ } \} \langle \rangle
\uparrow \Uparrow \lceil \rceil
\downarrow \Downarrow \lfloor \rfloor

Note: To use the Greek Letters in LaTeX that have the same appearance in the Latin alphabet, just use Latin: e.g., A instead of Alpha, B instead of Beta, etc.

Greek Letters
Symbol Script   Symbol Script
A and α A and \alpha N and ν N and \nu
B and β B and \beta Ξ and ξ \Xi and \xi
Γ and γ \Gamma and \gamma O and o O and o
Δ and δ \Delta and \delta Π, π and ϖ \Pi, \pi and \varpi
E, ϵ and ε E, \epsilon and \varepsilon P, ρ and ϱ P, \rho and \varrho
Z and ζ Z and \zeta Σ, σ and ς \Sigma, \sigma and \varsigma
H and η H and \eta T and τ T and \tau
Θ, θ and ϑ \Theta, \theta and \vartheta Y, Υ and υ Y, \Upsilon and \upsilon
I and ι I and \iota Φ, ϕ, and φ \Phi, \phi and \varphi
K, κ and ϰ K, \kappa and \varkappa X and χ X and \chi
Λ and λ \Lambda and \lambda Ψ and ψ \Psi and \psi
M and μ M and \mu Ω and ω \Omega and \omega
Other symbols
Symbol Script   Symbol Script   Symbol Script   Symbol Script   Symbol Script
\partial ı \imath \Re \nabla \aleph
ð \eth ȷ \jmath \Im \Box \beth
\hbar \ell \wp \infty \gimel

Template:NoteNot predefined in LATEX 2. Use one of the packages latexsym, amsfonts, amssymb, txfonts, pxfonts, or wasysym

Trigonometric Functions
Symbol Script   Symbol Script   Symbol Script   Symbol Script
sin \sin arcsin \arcsin sinh \sinh sec \sec
cos \cos arccos \arccos cosh \cosh csc \csc
tan \tan arctan \arctan tanh \tanh
cot \cot arccot \arccot coth \coth

If LaTeX does not include a command for the mathematical operator you want to use, for example \cis (cosine plus i times sine), add to your preamble:

\DeclareMathOperator\cis{cis}

You can then use \cis in the document just like \cos or any other mathematical operator.


Template:Clear

Summary

As you begin to see, typesetting math can be tricky at times. However, because LaTeX provides so much control, you can get professional quality mathematics typesetting with relatively little effort (once you've had a bit of practice, of course!). It is possible to elaborate further on the nitty-gritty of mathematics because the possibilities seem endless. However, with this tutorial, you should be able to get along with it sufficiently.

Template:TODO

References

Template:Reflist

Notes

Template:Notelist

Further reading


Template:LaTeX/Bottom


pl:LaTeX/Matematyka