5.1 Skriva matematiska formler i LaTeX
Förberedande kurs i matematik 1
Teori | Övningar |
Innehåll:
- LaTeX matematik
Lärandemål:
Efter detta avsnitt ska du ha lärt dig att:
- Skriva formler i LaTeX
- Undvika vanliga misstag när man kodar matematik i LaTeX
För att effektivt kunna skriva matematik på din dator i den individuella uppgiften och gruppuppgiften så behöver du koda matematiken med hjälp av LaTeX. I detta avsnitt kommer du få lära dig grunderna i att konstruera LaTeX-kod för att skriva matematiska formler.
Att skriva enkla uttryck i LaTex
För att markera starten för den matematiska formateringen används taggen <math>. För att sluta den matematiska formateringen används taggen </math>.
Till exempel skrivs formeln
Matematiska grundekvationer är skrivna enkelt och direkt.
Exempel 1
1+2−3 skrivs <math>1+2-3</math>5 skrivs <math>5/2</math>2
4 skrivs <math>4/(2+x)</math>(2+x)
4 skrivs <math>4 < 5</math>5
När du behöver använda symboler som inte är tillgängliga på ett tangentbord eller konstruera avancerade formler behöver du använda dig av special kommandon. Kommandona startar alltid med "backslash" t ex \le är kommandot för
I tabellen nedan har vi listat de vanligaste använda matematiska kommandona i LaTeX.
Exempel | LaTeX-code | Kommentar | |
Enkla räknesätt | a+b | ||
a-b | |||
![]() |
a\pm b | ||
![]() |
a\times b | ||
![]() |
a/b | ||
\frac{a}{b} | Använd \dfrac{a}{b} för att skapa större storlek på bråket. | ||
(a) | Skalbara parenteser \left(...\right) | ||
Jämförelsetecken | a=b | ||
![]() |
a\ne b | Alternativt: a\not= b | |
![]() |
a< b | OBS: mellanslag efter "<" | |
![]() |
a\le b | ||
![]() |
a>b | ||
![]() |
a\ge b | ||
Potenser och rötter | x^{n} | ||
![]() |
\sqrt{x} | ||
![]() |
\sqrt[n]{x} | Skriv \sqrt[\scriptstyle n]{x} för större n | |
Index | x_{n} | ||
Logaritmer | \ln x | ||
\log x | |||
\log_{a} x | |||
Trigonometri | ![]() |
30^{\circ} | |
\cos x | |||
\sin x | |||
\tan x | |||
\cot x | |||
Pilar | ![]() |
\Rightarrow | |
![]() |
\Leftarrow | ||
![]() |
\Leftrightarrow | ||
Diverse symboler | ![]() |
\pi |
Example 2
1 is written <math>1\pm 3\times 5</math>3
5
21y is written <math>\frac{1}{2}y\ne x\le z</math>=x
z
213 is written <math>2^{13}\sqrt{3}+\ln y</math>3+lny
tan30 is written <math>\tan 30^{\circ}+\cot\pi</math>+cot
Att skriva komplicerade uttryck
By combining simple expressions, we may form more complex expressions.
Example 3
is written <math>\sqrt{x+2}</math>x+2
(a2)3=a6 is written <math>(a^2)^3=a^6</math>222 is written <math>2^{2^2}</math>sin is written <math>\sin\sqrt{x}</math>x
Example 4
is written <math>\sqrt{x+\sqrt{x}}</math>x+
x
is written <math>\dfrac{x-x^2}{\sqrt{3}}</math>3x−x2
xx+x1 is written <math>\dfrac{x}{x+\dfrac{1}{x}}</math>x1 is written <math>x_{1,2}=-\dfrac{p}{2}\pm\sqrt{\left(\dfrac{p}{2}\right)^2-q}</math>2=−2p
2p
2−q
How to avoid 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 5
LaTeX | 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 6
LaTeX | 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 7
LaTeX | 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 \frac), while other fractions should be large (i.e. with \dfrac).
If an exponent or index contains a fraction then that fraction should be written in a slashed form (e.g. 2
Example 8
LaTeX | 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
A tip is to try out your maths formulas in the forum or in the wiki where you work on your individual assignment.
Useful web sites
- A more thorough list of LaTeX maths commands can be found on Wikipedias help page
- Two more thorough texts om LaTeX maths can be found in a chapter of the book The LaTeX Companion and a text by Herbert Voss.
- If you want to know more about LaTeX you can visit these sites: Wikipedia, The not so Short Introduction to LaTeX and LaTeX Wikibook.
- The actual implementation of LaTeX math that is used in the wiki is jsMath.