5.1 Skriva matematiska formler i LaTeX

Förberedande kurs i matematik 1

(Skillnad mellan versioner)
Hoppa till: navigering, sök
(Ny sida: __NOTOC__ {| border="0" cellspacing="0" cellpadding="0" height="30" width="100%" | style="border-bottom:1px solid #797979" width="5px" |   {{Mall:Vald flik|[[5.1 Skriva matematiska for...)
Rad 9: Rad 9:
{{Info|
{{Info|
'''Innehåll:'''
'''Innehåll:'''
-
*
+
* LaTeX matematik
}}
}}
Rad 16: Rad 16:
Efter detta avsnitt ska du ha lärt dig att:
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.
 +
 +
 +
 +
==How to write basic expressions ==
 +
 +
To indicate the '''start''' of math formatting, use the tag '''<nowiki><math></nowiki>'''. To '''end''' math formatting, use the tag '''<nowiki></math></nowiki>'''. For example, if you want the formula <math>a+b</math>, in the text box write <nowiki><math></nowiki>a+b<nowiki></math></nowiki>.
 +
 +
Simple mathematical formulas are written in a straight-forward manner.
 +
 +
<div class="exempel">
 +
''' Example 1'''
 +
 +
<ol type="a">
 +
<li><math>1+2-3\quad</math> is written <tt><nowiki><math></nowiki>1+2-3<nowiki></math></nowiki></tt></li>
 +
<li><math>5/2\quad</math> is written <tt><nowiki><math></nowiki>5/2<nowiki></math></nowiki></tt></li>
 +
<li><math>4/(2+x)\quad</math> is written <tt><nowiki><math></nowiki>4/(2+x)<nowiki></math></nowiki></tt></li>
 +
<li><math>4 < 5\quad</math> is written <tt><nowiki><math></nowiki>4 < 5<nowiki></math></nowiki></tt></li>
 +
</ol>
 +
</div>
 +
 +
When you need to use symbols that are not available on the keyboard or construct formulas that are not simple you use special commands that start with a backslash, e.g. <tt>\le</tt> is a command that gives you <math>\le</math>.
 +
 +
The table below shows some of the most commonly used maths commands in LaTeX.
 +
 +
 +
<table cellpadding="6" cellspacing="0" class="normalText" width="90%"
 +
align="center">
 +
<tr>
 +
<td></td>
 +
<td align="center"><b>Example</b></td>
 +
<td align="center"><b>LaTeX-code</b></td>
 +
<td align="left"><b>Comment</b></td>
 +
</tr>
 +
<tr style="background:#E6E6E6;">
 +
<td align="left">Simple operations</td>
 +
<td align="center"><span class="math">a+b</span></td>
 +
<td align="center"><tt>a+b</tt></td>
 +
<td></td>
 +
</tr>
 +
<tr bgcolor="white">
 +
<td></td>
 +
<td align="center"><span class="math">a-b</span></td>
 +
<td align="center"><tt>a-b</tt></td>
 +
<td></td>
 +
</tr>
 +
<tr style="background:#E6E6E6;">
 +
<td></td>
 +
<td align="center"><span class="math">a\pm b</span></td>
 +
<td align="center"><tt>a\pm b</tt></td>
 +
<td></td>
 +
</tr>
 +
<tr bgcolor="white">
 +
<td></td>
 +
<td align="center"><span class="math">a\times b</span></td>
 +
<td align="center"><tt>a\times b</tt></td>
 +
<td></td>
 +
</tr>
 +
<tr style="background:#E6E6E6;">
 +
<td></td>
 +
<td align="center"><span class="math">a/b</span></td>
 +
<td align="center"><tt>a/b</tt></td>
 +
<td></td>
 +
</tr>
 +
<tr>
 +
<td></td>
 +
<td align="center"><span class="math">\frac{a}{b}</span></td>
 +
<td align="center"><tt>\frac{a}{b}</tt></td>
 +
<td align="left">Use \dfrac{a}{b} for a larger sized fraction</td>
 +
</tr>
 +
<tr style="background:#E6E6E6;">
 +
<td></td>
 +
<td align="center"><span class="math">(a)</span></td>
 +
<td align="center"><tt>(a)</tt></td>
 +
<td align="left">Scalable parantheses \left(...\right)</td>
 +
</tr>
 +
<tr>
 +
<td align="left">Relation signs</td>
 +
<td align="center"><span class="math">a=b</span></td>
 +
<td align="center"><tt>a=b</tt></td>
 +
<td></td>
 +
</tr>
 +
<tr style="background:#E6E6E6;">
 +
<td></td>
 +
<td align="center"><span class="math">a\ne b</span></td>
 +
<td align="center"><tt>a\ne b</tt></td>
 +
<td>Alternatively: a\not= b</td>
 +
</tr>
 +
<tr>
 +
<td></td>
 +
<td align="center"><span class="math">a< b</span></td>
 +
<td align="center"><tt>a< b</tt></td>
 +
<td>NB: Space after "<" </td>
 +
</tr>
 +
<tr style="background:#E6E6E6;">
 +
<td></td>
 +
<td align="center"><span class="math">a\le b</span></td>
 +
<td align="center"><tt>a\le b</tt></td>
 +
<td></td>
 +
</tr>
 +
<tr>
 +
<td></td>
 +
<td align="center"><span class="math">a> b</span></td>
 +
<td align="center"><tt>a>b</tt></td>
 +
<td></td>
 +
</tr>
 +
<tr style="background:#E6E6E6;">
 +
<td></td>
 +
<td align="center"><span class="math">a\ge b</span></td>
 +
<td align="center"><tt>a\ge b</tt></td>
 +
<td></td>
 +
</tr>
 +
<tr>
 +
<td>Powers and roots</td>
 +
<td align="center"><span class="math">x^{n}</span></td>
 +
<td align="center"><tt>x^{n}</tt></td>
 +
<td></td>
 +
</tr>
 +
<tr style="background:#E6E6E6;">
 +
<td></td>
 +
<td align="center"><span class="math">\sqrt{x}</span></td>
 +
<td align="center"><tt>\sqrt{x}</tt></td>
 +
<td></td>
 +
</tr>
 +
<tr>
 +
<td></td>
 +
<td align="center"><span class="math">\sqrt[n]{x}</span></td>
 +
<td align="center"><tt>\sqrt[n]{x}</tt></td>
 +
<td>Write \sqrt[\scriptstyle n]{x} for bigger n</td>
 +
</tr>
 +
<tr style="background:#E6E6E6;">
 +
<td>Index</td>
 +
<td align="center"><span class="math">x_n</span></td>
 +
<td align="center"><tt>x_{n}</tt></td>
 +
<td></td>
 +
</tr>
 +
<tr>
 +
<td>Logarithms</td>
 +
<td align="center"><span class="math">\ln x</span></td>
 +
<td align="center"><tt>\ln x</tt></td>
 +
<td></td>
 +
</tr>
 +
<tr style="background:#E6E6E6;">
 +
<td></td>
 +
<td align="center"><span class="math">\log x</span></td>
 +
<td align="center"><tt>\log x</tt></td>
 +
<td></td>
 +
</tr>
 +
<tr>
 +
<td></td>
 +
<td align="center"><span class="math">\log_{a} x</span></td>
 +
<td align="center"><tt>\log_{a} x</tt></td>
 +
<td></td>
 +
</tr>
 +
<tr style="background:#E6E6E6;">
 +
<td>Trigonometry</td>
 +
<td align="center"><span class="math">30^{\circ}</span></td>
 +
<td align="center"><tt>30^{\circ}</tt></td>
 +
<td></td>
 +
</tr>
 +
<tr>
 +
<td></td>
 +
<td align="center"><span class="math">\cos x</span></td>
 +
<td align="center"><tt>\cos x</tt></td>
 +
<td></td>
 +
</tr>
 +
<tr style="background:#E6E6E6;">
 +
<td></td>
 +
<td align="center"><span class="math">\sin x</span></td>
 +
<td align="center"><tt>\sin x</tt></td>
 +
<td></td>
 +
</tr>
 +
<tr>
 +
<td></td>
 +
<td align="center"><span class="math">\tan x</span></td>
 +
<td align="center"><tt>\tan x</tt></td>
 +
<td></td>
 +
</tr>
 +
<tr style="background:#E6E6E6;">
 +
<td></td>
 +
<td align="center"><span class="math">\cot x</span></td>
 +
<td align="center"><tt>\cot x</tt></td>
 +
<td></td>
 +
</tr>
 +
<tr>
 +
<td>Arrows</td>
 +
<td align="center"><span class="math">\Rightarrow</span></td>
 +
<td align="center"><tt>\Rightarrow</tt></td>
 +
<td></td>
 +
</tr>
 +
<tr style="background:#E6E6E6;">
 +
<td></td>
 +
<td align="center"><span class="math">\Leftarrow</span></td>
 +
<td align="center"><tt>\Leftarrow</tt></td>
 +
<td></td>
 +
</tr>
 +
<tr>
 +
<td></td>
 +
<td align="center"><span class="math">\Leftrightarrow</span></td>
 +
<td align="center"><tt>\Leftrightarrow</tt></td>
 +
<td></td>
 +
</tr>
 +
<tr style="background:#E6E6E6;">
 +
<td>Various symbols</td>
 +
<td align="center"><span class="math">\pi</span></td>
 +
<td align="center"><tt>\pi</tt></td>
 +
<td></td>
 +
</tr>
 +
</table>
 +
 +
 +
<div class="exempel">
 +
''' Example 2'''
 +
 +
<ol type="a">
 +
<li><math>1\pm3\times 5\quad</math> is written <tt><nowiki><math></nowiki>1\pm 3\times 5<nowiki></math></nowiki></tt></li>
 +
<li><math>\tfrac{1}{2}y\ne x\le z\quad</math> is written <tt><nowiki><math></nowiki>\frac{1}{2}y\ne x\le z<nowiki></math></nowiki></tt></li>
 +
<li><math>2^{13}\sqrt{3}+\ln y\quad</math> is written <tt><nowiki><math></nowiki>2^{13}\sqrt{3}+\ln y<nowiki></math></nowiki></tt></li>
 +
<li><math>\tan 30^{\circ}+\cot\pi\quad</math> is written <tt><nowiki><math></nowiki>\tan 30^{\circ}+\cot\pi<nowiki></math></nowiki></tt></li>
 +
</ol>
 +
</div>
 +
 +
 +
== How to write complex expressions==
 +
 +
By combining simple expressions, we may form more complex expressions.
 +
 +
<div class="exempel">
 +
''' Example 3'''
 +
 +
<ol type="a">
 +
<li><math>\sqrt{x+2}\quad</math> is written <tt><nowiki><math></nowiki>\sqrt{x+2}<nowiki></math></nowiki></tt></li>
 +
<li><math>(a^2)^3=a^6\quad</math> is written <tt><nowiki><math></nowiki>(a^2)^3=a^6<nowiki></math></nowiki></tt></li>
 +
<li><math>2^{2^2}\quad</math> is written <tt><nowiki><math></nowiki>2^{2^2}<nowiki></math></nowiki></tt></li>
 +
<li><math>\sin\sqrt{x}\quad</math> is written <tt><nowiki><math></nowiki>\sin\sqrt{x}<nowiki></math></nowiki></tt></li>
 +
</ol>
 +
</div>
 +
 +
<div class="exempel">
 +
''' Example 4'''
 +
 +
<ol type="a">
 +
<li><math>\sqrt{x+\sqrt{x}}\quad</math> is written <tt><nowiki><math></nowiki>\sqrt{x+\sqrt{x}}<nowiki></math></nowiki></tt></li>
 +
<li><math>\dfrac{x-x^2}{\sqrt{3}}\quad</math> is written <tt><nowiki><math></nowiki>\dfrac{x-x^2}{\sqrt{3}}<nowiki></math></nowiki></tt></li>
 +
<li><math>\dfrac{x}{x+\dfrac{1}{x}}\quad</math> is written <tt><nowiki><math></nowiki>\dfrac{x}{x+\dfrac{1}{x}}<nowiki></math></nowiki></tt></li>
 +
<li><math>x_{1,2}=-\dfrac{p}{2}\pm\sqrt{\left(\dfrac{p}{2}\right)^2-q}\quad</math> is written <tt><nowiki><math></nowiki>x_{1,2}=-\dfrac{p}{2}\pm\sqrt{\left(\dfrac{p}{2}\right)^2-q}<nowiki></math></nowiki></tt></li>
 +
</ol>
 +
</div>
 +
 +
 +
==How to avoid common mistakes==
 +
 +
One of the most common mistakes when editing math in the wiki is to forget the start <tt><nowiki><math></nowiki></tt> tag and the end <tt><nowiki></math></nowiki></tt> 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 (<tt>*</tt>) instead of a proper multiplication sign <math>\times</math> (<tt>\times</tt> in TeX).
 +
 +
<div class="exempel">
 +
''' Example 5'''
 +
 +
{| width="100%" cellspacing="0" cellpadding="0"
 +
||
 +
| align="center" width="35%" |LaTeX
 +
| align="center" width="35%" |Result
 +
|-
 +
||<ol type="a" style="padding-top: 5px; padding-bottom: 0px; margin-bottom: 0px"><li>'''Don't''' write</li></ol>
 +
| align="center" valign="bottom" |<tt>sin x</tt>
 +
| align="center" valign="bottom" |<math>sin x</math>
 +
|-
 +
||<ol style="list-style-type: none; padding-top: 10px; padding-bottom: 0px; margin-top: 0px; margin-bottom: 0px"><li>'''Don't''' write</li></ol>
 +
| align="center" valign="bottom" |<tt>\sinx</tt>
 +
| align="center" valign="bottom" |<span style="color:red">Error</span>
 +
|-
 +
||<ol style="list-style-type: none; padding-top: 10px; padding-bottom: 0px; margin-top: 0px; margin-bottom: 0px"><li>'''Do''' write</li></ol>
 +
| align="center" valign="bottom" |<tt>\sin x</tt>
 +
| align="center" valign="bottom" |<math>\sin x</math>
 +
|-
 +
||<ol type="a" start="2" style="padding-top: 5px; padding-bottom: 0px; margin-bottom: 0px"><li>'''Don't''' write</li></ol>
 +
| align="center" valign="bottom" |<tt>4*3</tt>
 +
| align="center" valign="bottom" |<math>4*3</math>
 +
|-
 +
||<ol style="list-style-type: none; padding-top: 10px; padding-bottom: 0px; margin-top: 0px; margin-bottom: 0px"><li>'''Do''' write</li></ol>
 +
| align="center" valign="bottom" |<tt>4\times 3</tt>
 +
| align="center" valign="bottom" |<math>4\times 3</math>
 +
|-
 +
||<ol type="a" start="3" style="padding-top: 5px; padding-bottom: 0px; margin-bottom: 0px"><li>'''Don't''' write</li></ol>
 +
| align="center" valign="bottom" |<tt>a\times b</tt>
 +
| align="center" valign="bottom" |<math>a\times b</math>
 +
|-
 +
||<ol style="list-style-type: none; padding-top: 10px; padding-bottom: 0px; margin-top: 0px"><li>'''Do''' write</li></ol>
 +
| align="center" |<tt>ab</tt>
 +
| align="center" |<math>ab</math>
 +
|}
 +
</div>
 +
 +
====Exponents and indices====
 +
 +
When writing exponents you use <tt>^</tt> followed by the exponent and to write indices you use <tt>_</tt> followed by the index. If the exponent or index consists of more than one symbol it must be enclosed with braces <tt>{}</tt>.
 +
 +
A special kind of exponent is the degree sign (°) which is written as <tt>^{\circ}</tt>.
 +
 +
<div class="exempel">
 +
''' Example 6'''
 +
 +
{| width="100%" cellspacing="0" cellpadding="0"
 +
||
 +
| align="center" width="35%" |LaTeX
 +
| align="center" width="35%" |Result
 +
|-
 +
||<ol type="a" style="padding-top: 5px; padding-bottom: 0px; margin-bottom: 0px"><li>'''Don't''' write</li></ol>
 +
| align="center" valign="bottom" |<tt>a2</tt>
 +
| align="center" valign="bottom" |<math>a2</math>
 +
|-
 +
||<ol style="list-style-type: none; padding-top: 10px; padding-bottom: 0px; margin-top: 0px; margin-bottom: 0px"><li>'''Do''' write</li></ol>
 +
| align="center" valign="bottom" |<tt>a^2</tt>
 +
| align="center" valign="bottom" |<math>a^2</math>
 +
|-
 +
||<ol type="a" start="2" style="padding-top: 5px; padding-bottom: 0px; margin-bottom: 0px"><li>'''Don't''' write</li></ol>
 +
| align="center" valign="bottom" |<tt>x1</tt>
 +
| align="center" valign="bottom" |<math>x1</math>
 +
|-
 +
||<ol style="list-style-type: none; padding-top: 10px; padding-bottom: 0px; margin-top: 0px; margin-bottom: 0px"><li>'''Do''' write</li></ol>
 +
| align="center" valign="bottom" |<tt>x_1</tt>
 +
| align="center" valign="bottom" |<math>x_1</math>
 +
|-
 +
||<ol type="a" start="3" style="padding-top: 5px; padding-bottom: 0px; margin-bottom: 0px"><li>'''Don't''' write</li></ol>
 +
| align="center" valign="bottom" |<tt>a^22</tt>
 +
| align="center" valign="bottom" |<math>a^22</math>
 +
|-
 +
||<ol style="list-style-type: none; padding-top: 10px; padding-bottom: 0px; margin-top: 0px; margin-bottom: 0px"><li>'''Do''' write</li></ol>
 +
| align="center" valign="bottom" |<tt>a^{22}</tt>
 +
| align="center" valign="bottom" |<math>a^{22}</math>
 +
|-
 +
||<ol type="a" start="4" style="padding-top: 5px; padding-bottom: 0px; margin-bottom: 0px"><li>'''Don't''' write</li></ol>
 +
| align="center" valign="bottom" |<tt>30^{o}</tt>
 +
| align="center" valign="bottom" |<math>30^{o}</math>
 +
|-
 +
||<ol style="list-style-type: none; padding-top: 10px; padding-bottom: 0px; margin-top: 0px; margin-bottom: 0px"><li>'''Don't''' write</li></ol>
 +
| align="center" valign="bottom" |<tt>30^{0}</tt>
 +
| align="center" valign="bottom" |<math>30^{0}</math>
 +
|-
 +
||<ol style="list-style-type: none; padding-top: 10px; padding-bottom: 0px; margin-top: 0px"><li>'''Do''' write</li></ol>
 +
| align="center" |<tt>30^{\circ}</tt>
 +
| align="center" |<math>30^{\circ}</math>
 +
|}
 +
</div>
 +
 +
====Delimiters====
 +
 +
In more complex expressions you need to make sure to balance each opening parenthesis <tt>(</tt> with a closing parenthesis <tt>)</tt>.
 +
 +
A pair of parenthesis that delimits a tall expression should be as large as the expression. You should therefore prefix the opening parenthesis with <tt>\left</tt> and the closing parenthesis with <tt>\right</tt> to get a pair of extensible parentheses that adjust its height to the expression.
 +
 +
Note also that braces <tt>{}</tt> and not parentheses <tt>()</tt> are used in commands to delimits arguments.
 +
 +
<div class="exempel">
 +
''' Example 7'''
 +
 +
{| width="100%" cellspacing="0" cellpadding="0"
 +
||
 +
| align="center" width="35%" |LaTeX
 +
| align="center" width="35%" |Result
 +
|-
 +
||<ol type="a" style="padding-top: 3px; padding-bottom: 3px"><li>'''Don't''' write</li></ol>
 +
| align="center" valign="center" |<tt>(1-(1-x)</tt>
 +
| align="center" valign="center" |<math>(1-(1-x)</math>
 +
|-
 +
||<ol style="list-style-type: none; padding-top: 5px; padding-bottom: 5px; margin-top: 0px; margin-bottom: 0px"><li>'''Do''' write</li></ol>
 +
| align="center" valign="center" |<tt>(1-(1-x))</tt>
 +
| align="center" valign="center" |<math>(1-(1-x))</math>
 +
|-
 +
||<ol type="a" start="2" style="padding-top: 10px; padding-bottom: 10px"><li>'''Don't''' write</li></ol>
 +
| align="center" valign="center" |<tt>(\dfrac{a}{b}+c)</tt>
 +
| align="center" valign="center" |<math>(\dfrac{a}{b}+c)</math>
 +
|-
 +
||<ol style="list-style-type: none; padding-top: 10px; padding-bottom: 10px; margin-top: 0px; margin-bottom: 0px"><li>'''Do''' write</li></ol>
 +
| align="center" valign="center" |<tt>\left(\dfrac{a}{b}+c\right)</tt>
 +
| align="center" valign="center" |<math>\left(\dfrac{a}{b}+c\right)</math>
 +
|-
 +
||<ol type="a" start="3" style="padding-top: 5px; padding-bottom: 5px"><li>'''Don't''' write</li></ol>
 +
| align="center" valign="center" |<tt>\frac(1)(2)</tt>
 +
| align="center" valign="center" |<math>\tfrac(1)(2)</math>
 +
|-
 +
||<ol style="list-style-type: none; padding-top: 5px; padding-bottom: 5px; margin-top: 0px; margin-bottom: 0px"><li>'''Do''' write</li></ol>
 +
| align="center" valign="center" |<tt>\frac{1}{2}</tt>
 +
| align="center" valign="center" |<math>\tfrac{1}{2}</math>
 +
|-
 +
||<ol type="a" start="4" style="padding-top: 5px; padding-bottom: 5px"><li>'''Don't''' write</li></ol>
 +
| align="center" valign="center" |<tt>\sqrt(a+b)</tt>
 +
| align="center" valign="center" |<math>\sqrt(a+b)</math>
 +
|-
 +
||<ol style="list-style-type: none; padding-top: 5px; padding-bottom: 5px; margin-top: 0px; margin-bottom: 0px"><li>'''Don't''' write</li></ol>
 +
| align="center" valign="center" |<tt>\sqrt{(a+b)}</tt>
 +
| align="center" valign="center" |<math>\sqrt{(a+b)}</math>
 +
|-
 +
||<ol style="list-style-type: none; padding-top: 5px; padding-bottom: 5px"><li>'''Do''' write</li></ol>
 +
| align="center" valign="center" |<tt>\sqrt{a+b}</tt>
 +
| align="center" valign="center" |<math>\sqrt{a+b}</math>
 +
|}
 +
</div>
 +
 +
====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 <tt>\frac</tt>), while other fractions should be large (i.e. with <tt>\dfrac</tt>).
 +
 +
If an exponent or index contains a fraction then that fraction should be written in a slashed form (e.g. <math>5/2</math> instead of <math>\tfrac{5}{2}</math>) to enhance the legibility.
 +
 +
<div class="exempel">
 +
''' Example 8'''
 +
 +
{| width="100%" cellspacing="0" cellpadding="0"
 +
||
 +
| align="center" width="35%" |LaTeX
 +
| align="center" width="35%" |Result
 +
|-
 +
||<ol type="a" style="padding-top: 5px; padding-bottom: 5px><li>'''Don't''' write</li></ol>
 +
| align="center" valign="center" |<tt>\dfrac{1}{2}</tt>
 +
| align="center" valign="center" |<math>\dfrac{1}{2}</math>
 +
|-
 +
||<ol style="list-style-type: none; padding-top: 5px; padding-bottom: 5px; margin-top: 0px; margin-bottom: 0px"><li>'''Do''' write</li></ol>
 +
| align="center" valign="center" |<tt>\frac{1}{2}</tt>
 +
| align="center" valign="center" |<math>\tfrac{1}{2}</math>
 +
|-
 +
| colspan="3"|<ol style="list-style-type: none; padding-top: 10px; padding-bottom: 0px; margin-top: 0px; margin-bottom: 0px"><li>(Exception: If the fraction is next to a large expression you should, however, write the fraction as a large fraction.)</li></ol>
 +
|-
 +
||<ol type="a" start="2" style="padding-top: 5px; padding-bottom: 5px><li>'''Don't''' write</li></ol>
 +
| align="center" valign="center" |<tt>\frac{a}{b}</tt>
 +
| align="center" valign="center" |<math>\tfrac{a}{b}</math>
 +
|-
 +
||<ol style="list-style-type: none; padding-top: 10px; padding-bottom: 10px; margin-top: 0px; margin-bottom: 0px"><li>'''Do''' write</li></ol>
 +
| align="center" valign="center" |<tt>\dfrac{a}{b}</tt>
 +
| align="center" valign="center" |<math>\dfrac{a}{b}</math>
 +
|-
 +
||<ol type="a" start="3" style="padding-top: 5px; padding-bottom: 5px><li>'''Don't''' write</li></ol>
 +
| align="center" valign="center" |<tt>\frac{\sqrt{3}}{2}</tt>
 +
| align="center" valign="center" |<math>\tfrac{\sqrt{3}}{2}</math>
 +
|-
 +
||<ol style="list-style-type: none; padding-top: 10px; padding-bottom: 10px; margin-top: 0px; margin-bottom: 0px"><li>'''Do''' write</li></ol>
 +
| align="center" valign="center" |<tt>\dfrac{\sqrt{3}}{2}</tt>
 +
| align="center" valign="center" |<math>\dfrac{\sqrt{3}}{2}</math>
 +
|-
 +
||<ol type="a" start="4" style="padding-top: 5px; padding-bottom: 5px"><li>'''Don't''' write</li></ol>
 +
| align="center" valign="center" |<tt>a^{\frac{1}{2}}</tt>
 +
| align="center" valign="center" |<math>a^{\frac{1}{2}}</math>
 +
|-
 +
||<ol style="list-style-type: none; padding-top: 0px; padding-bottom: 0px"><li>'''Do''' write</li></ol>
 +
| align="center" valign="center" |<tt>a^{1/2}</tt>
 +
| align="center" valign="center" |<math>a^{1/2}</math>
 +
|}
 +
</div>
 +
 +
 +
<div class="inforuta" style="width:580px;">
 +
'''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 [http://en.wikipedia.org/wiki/Help:Displaying_a_formula help page]
 +
 +
*Two more thorough texts om LaTeX maths can be found in [http://www.cism.it/cism/volconts/ch8.pdf a chapter] of the book ''The LaTeX Companion'' and a [http://www.tex.ac.uk/tex-archive/info/math/voss/mathmode/Mathmode.pdf text] by Herbert Voss.
 +
 +
*If you want to know more about LaTeX you can visit these sites: [http://en.wikipedia.org/wiki/LaTeX Wikipedia], [http://www.ctan.org/tex-archive/info/lshort/english/lshort.pdf The not so Short Introduction to LaTeX] and [http://en.wikibooks.org/wiki/LaTeX LaTeX Wikibook].
 +
 +
*The actual implementation of LaTeX math that is used in the wiki is [http://www.math.union.edu/~dpvc/jsMath/ jsMath].
 +
 +
</div>

Versionen från 26 februari 2009 kl. 08.40

       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.


How to write basic expressions

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 \displaystyle a+b, in the text box write <math>a+b</math>.

Simple mathematical formulas are written in a straight-forward manner.

Example 1

  1. \displaystyle 1+2-3\quad is written <math>1+2-3</math>
  2. \displaystyle 5/2\quad is written <math>5/2</math>
  3. \displaystyle 4/(2+x)\quad is written <math>4/(2+x)</math>
  4. \displaystyle 4 < 5\quad is written <math>4 < 5</math>

When you need to use symbols that are not available on the keyboard or construct formulas that are not simple you use special commands that start with a backslash, e.g. \le is a command that gives you \displaystyle \le.

The table below shows some of the most commonly used maths commands in LaTeX.


Example LaTeX-code Comment
Simple operations a+b a+b
a-b a-b
a\pm b a\pm b
a\times b a\times b
a/b a/b
\frac{a}{b} \frac{a}{b} Use \dfrac{a}{b} for a larger sized fraction
(a) (a) Scalable parantheses \left(...\right)
Relation signs a=b a=b
a\ne b a\ne b Alternatively: a\not= b
a< b a< b NB: Space after "<"
a\le b a\le b
a> b a>b
a\ge b a\ge b
Powers and roots x^{n} x^{n}
\sqrt{x} \sqrt{x}
\sqrt[n]{x} \sqrt[n]{x} Write \sqrt[\scriptstyle n]{x} for bigger n
Index x_n x_{n}
Logarithms \ln x \ln x
\log x \log x
\log_{a} x \log_{a} x
Trigonometry 30^{\circ} 30^{\circ}
\cos x \cos x
\sin x \sin x
\tan x \tan x
\cot x \cot x
Arrows \Rightarrow \Rightarrow
\Leftarrow \Leftarrow
\Leftrightarrow \Leftrightarrow
Various symbols \pi \pi


Example 2

  1. \displaystyle 1\pm3\times 5\quad is written <math>1\pm 3\times 5</math>
  2. \displaystyle \tfrac{1}{2}y\ne x\le z\quad is written <math>\frac{1}{2}y\ne x\le z</math>
  3. \displaystyle 2^{13}\sqrt{3}+\ln y\quad is written <math>2^{13}\sqrt{3}+\ln y</math>
  4. \displaystyle \tan 30^{\circ}+\cot\pi\quad is written <math>\tan 30^{\circ}+\cot\pi</math>


How to write complex expressions

By combining simple expressions, we may form more complex expressions.

Example 3

  1. \displaystyle \sqrt{x+2}\quad is written <math>\sqrt{x+2}</math>
  2. \displaystyle (a^2)^3=a^6\quad is written <math>(a^2)^3=a^6</math>
  3. \displaystyle 2^{2^2}\quad is written <math>2^{2^2}</math>
  4. \displaystyle \sin\sqrt{x}\quad is written <math>\sin\sqrt{x}</math>

Example 4

  1. \displaystyle \sqrt{x+\sqrt{x}}\quad is written <math>\sqrt{x+\sqrt{x}}</math>
  2. \displaystyle \dfrac{x-x^2}{\sqrt{3}}\quad is written <math>\dfrac{x-x^2}{\sqrt{3}}</math>
  3. \displaystyle \dfrac{x}{x+\dfrac{1}{x}}\quad is written <math>\dfrac{x}{x+\dfrac{1}{x}}</math>
  4. \displaystyle x_{1,2}=-\dfrac{p}{2}\pm\sqrt{\left(\dfrac{p}{2}\right)^2-q}\quad is written <math>x_{1,2}=-\dfrac{p}{2}\pm\sqrt{\left(\dfrac{p}{2}\right)^2-q}</math>


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 \displaystyle \times (\times in TeX).

Example 5

LaTeX Result
  1. Don't write
sin x \displaystyle sin x
  1. Don't write
\sinx Error
  1. Do write
\sin x \displaystyle \sin x
  1. Don't write
4*3 \displaystyle 4*3
  1. Do write
4\times 3 \displaystyle 4\times 3
  1. Don't write
a\times b \displaystyle a\times b
  1. Do write
ab \displaystyle 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
  1. Don't write
a2 \displaystyle a2
  1. Do write
a^2 \displaystyle a^2
  1. Don't write
x1 \displaystyle x1
  1. Do write
x_1 \displaystyle x_1
  1. Don't write
a^22 \displaystyle a^22
  1. Do write
a^{22} \displaystyle a^{22}
  1. Don't write
30^{o} \displaystyle 30^{o}
  1. Don't write
30^{0} \displaystyle 30^{0}
  1. Do write
30^{\circ} \displaystyle 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. Don't write
(1-(1-x) \displaystyle (1-(1-x)
  1. Do write
(1-(1-x)) \displaystyle (1-(1-x))
  1. Don't write
(\dfrac{a}{b}+c) \displaystyle (\dfrac{a}{b}+c)
  1. Do write
\left(\dfrac{a}{b}+c\right) \displaystyle \left(\dfrac{a}{b}+c\right)
  1. Don't write
\frac(1)(2) \displaystyle \tfrac(1)(2)
  1. Do write
\frac{1}{2} \displaystyle \tfrac{1}{2}
  1. Don't write
\sqrt(a+b) \displaystyle \sqrt(a+b)
  1. Don't write
\sqrt{(a+b)} \displaystyle \sqrt{(a+b)}
  1. Do write
\sqrt{a+b} \displaystyle \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. \displaystyle 5/2 instead of \displaystyle \tfrac{5}{2}) to enhance the legibility.

Example 8

LaTeX Result
  1. Don't write
\dfrac{1}{2} \displaystyle \dfrac{1}{2}
  1. Do write
\frac{1}{2} \displaystyle \tfrac{1}{2}
  1. (Exception: If the fraction is next to a large expression you should, however, write the fraction as a large fraction.)
  1. Don't write
\frac{a}{b} \displaystyle \tfrac{a}{b}
  1. Do write
\dfrac{a}{b} \displaystyle \dfrac{a}{b}
  1. Don't write
\frac{\sqrt{3}}{2} \displaystyle \tfrac{\sqrt{3}}{2}
  1. Do write
\dfrac{\sqrt{3}}{2} \displaystyle \dfrac{\sqrt{3}}{2}
  1. Don't write
a^{\frac{1}{2}} \displaystyle a^{\frac{1}{2}}
  1. Do write
a^{1/2} \displaystyle 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.
  • The actual implementation of LaTeX math that is used in the wiki is jsMath.