5.1 Writing formulas in TeX
From Förberedande kurs i matematik 1
Theory | Exercises |
Contents:
- What is TeX/LaTeX?
- Common expressions
- Common mistakes
Learning outcomes:
After this section you will have learned how to:
- Write formulas in TeX
What is TeX/LaTeX?
Common expressions
To write mathematics efficiently on a computer, one uses some sort of coding. We have chosen to use jsMath's LaTeX-code. Below are some basic examples of useful code snippets. For more examples, see for example Wikipedia's help page.
To indicate the start of math formatting, use the tag <math>. To end math formatting, use the tag </math>. For example, if you want the formula
Example | LaTeX-code | Button | Comment | |
Simple operations | a+b | |||
a-b | ||||
![]() |
a\pm b | <img src="../images/buttons/pm.gif" /> | ||
![]() |
a\cdot b | <img src="../images/buttons/cdot.gif" /> | ||
![]() |
a/b | |||
\frac{a}{b} | <img src="../images/buttons/frac.gif" /> | Use \displaystyle\frac{a}{b} for a larger sized fraction | ||
(a) | Scalable parantheses \left(...\right) | |||
Relation signs | a=b | |||
![]() |
a\ne b | <img src="../images/buttons/ne.gif" /> | Alternatively: a\not= b | |
![]() |
a< b | NB: Space after "<" | ||
![]() |
a\le b | <img src="../images/buttons/le.gif" /> | ||
![]() |
a>b | |||
![]() |
a\ge b | <img src="../images/buttons/ge.gif" /> | ||
Powers and roots | x^{n} | <img src="../images/buttons/pow.gif" /> | ||
![]() |
\sqrt{x} | <img src="../images/buttons/sqrt.gif" /> | ||
![]() |
\sqrt[n]{x} | <img src="../images/buttons/sqrtn.gif" /> | Write \sqrt[\scriptstyle n]{x} for bigger n | |
Logarithms | \ln x | <img src="../images/buttons/ln.gif" /> | ||
\log x | ||||
\log_{a} x | <img src="../images/buttons/logn.gif" /> | |||
Trigonometry | ![]() |
30^{\circ} | <img src="../images/buttons/circ.gif" /> | |
\cos x | <img src="../images/buttons/cos.gif" /> | |||
\sin x | <img src="../images/buttons/sin.gif" /> | |||
\tan x | <img src="../images/buttons/tan.gif" /> | |||
\cot x | <img src="../images/buttons/cot.gif" /> | |||
Various symbols | ![]() |
\pi | <img src="../images/buttons/pi.gif" /> |
By combining these simple expressions, we may form more complex expressions. The table below shows some examples.
Example | Latex-code |
ax^2+bx+c | |
(a^2)^3=a^6 | |
![]() |
\sqrt{x+2} |
![]() ![]() |
\sqrt{x+\sqrt{x}} |
\displaystyle\frac{a+b}{c-d} |
Common mistakes
One of the most common mistakes when editing math in the wiki is to forget the start <math> tag and the end </math> tag.
Remember also to start commands with a backslash (\) and to add a space after the commands (unless they are followed immediately by a new command).
Another frequent mistake is to use an asterisk (*) instead of a proper multiplication sign
Example
TeX | Result | |
| sin x | |
| \sinx | Error |
| \sin x | |
| 4*3 | ![]() |
| 4\times 3 | ![]() |
| a\times b | ![]() |
| ab |
Exponents and indices
When writing exponents you use ^ followed by the exponent and to write indices you use _ followed by the index. If the exponent or index consists of more than one symbol it must be enclosed with braces {}.
A special kind of exponent is the degree sign (°) which is written as ^{\circ}.
Example
TeX | Result | |
| a2 | |
| a^2 | |
| x1 | |
| x_1 | |
| a^22 | |
| a^{22} | |
| 30^{o} | |
| 30^{0} | |
| 30^{\circ} | ![]() |
Delimiters
In more complex expressions you need to make sure to balance each opening parenthesis ( with a closing parenthesis ).
A pair of parenthesis that delimits a tall expression should be as large as the expression. You should therefore prefix the opening parenthesis with \left and the closing parenthesis with \right to get a pair of extensible parentheses that adjust its height to the expression.
Note also that braces {} and not parentheses () are used in commands to delimits arguments.
Example
TeX | Result | |
| (1-(1-x) | |
| (1-(1-x)) | |
| (\dfrac{a}{b}+c) | |
| \left(\dfrac{a}{b}+c\right) | ![]() ![]() |
| \frac(1)(2) | |
| \frac{1}{2} | |
| \sqrt(a+b) | ![]() |
| \sqrt{(a+b)} | ![]() |
| \sqrt{a+b} | ![]() |
Fractions
As a rule of thumb you should write fractions where the numerator and denominator consist only of a few digits as a small fraction (i.e. with \tfrac), while other fractions should be large (i.e. with \frac).
If an exponent or index contains a fraction then that fraction should be written in a slashed form (e.g. 2
Example
TeX | Result | |
| \dfrac{1}{2} | |
| \frac{1}{2} | |
| ||
| \frac{a}{b} | |
| \dfrac{a}{b} | |
| \frac{\sqrt{3}}{2} | ![]() |
| \dfrac{\sqrt{3}}{2} | ![]() |
| a^{\frac{1}{2}} | |
| a^{1/2} | ![]() |
Study advice
Useful web sites
- A video course in mathematical writing by Donald Knuth (A compendium accompaning the course is avalable in source form or in excerpts from Google books).