5.1 Writing formulas in TeX

From Förberedande kurs i matematik 1

(Difference between revisions)
Jump to: navigation, search
Line 20: Line 20:
After this section you will have learned how to:
After this section you will have learned how to:
-
* Write formulas in TeX
+
* Write simple maths formulas in LaTeX.
 +
* Avoid common mistakes when coding maths in LaTeX.
}}
}}
-
==What is TeX/LaTeX?==
+
To write mathematics efficiently on a computer in your individual assignment and the group task you will need write the maths in a coded form called LaTeX syntax. In this section you will learn the fundamentals of constructing LaTeX code that yields simple maths formulas.
-
==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 [http://en.wikipedia.org/wiki/Wikipedia:Editing_Math Wikipedia's help page].
 
-
To indicate the '''start''' of math formatting, use the tag <b>&lt;math&gt;</b>. To '''end''' math formatting, use the tag <b>&lt;/math&gt;</b>. For example, if you want the formula <math>a+b</math>, in the text box write &lt;math&gt;a+b&lt;/math&gt;.
 
 +
==How to write basic expressions ==
-
<table cellpadding="6" cellspacing="0" class="normalText" width="70%"
+
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 '''
 +
 
 +
<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.3\quad</math> is written <tt><nowiki><math></nowiki>4 < 5.3<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">
align="center">
<tr>
<tr>
Line 59: Line 78:
<tr bgcolor="white">
<tr bgcolor="white">
<td></td>
<td></td>
-
<td align="center"><span class="math">a\cdot b</span></td>
+
<td align="center"><span class="math">a\times b</span></td>
-
<td align="center"><tt>a\cdot b</tt></td>
+
<td align="center"><tt>a\times b</tt></td>
<td></td>
<td></td>
</tr>
</tr>
Line 73: Line 92:
<td align="center"><span class="math">\frac{a}{b}</span></td>
<td align="center"><span class="math">\frac{a}{b}</span></td>
<td align="center"><tt>\frac{a}{b}</tt></td>
<td align="center"><tt>\frac{a}{b}</tt></td>
-
<td align="left">Use \displaystyle\frac{a}{b} for a larger sized fraction</td>
+
<td align="left">Use \dfrac{a}{b} for a larger sized fraction</td>
</tr>
</tr>
<tr style="background:#E6E6E6;">
<tr style="background:#E6E6E6;">
Line 136: Line 155:
</tr>
</tr>
<tr style="background:#E6E6E6;">
<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>Logarithms</td>
<td align="center"><span class="math">\ln x</span></td>
<td align="center"><span class="math">\ln x</span></td>
Line 141: Line 166:
<td></td>
<td></td>
</tr>
</tr>
-
<tr>
+
<tr style="background:#E6E6E6;">
<td></td>
<td></td>
<td align="center"><span class="math">\log x</span></td>
<td align="center"><span class="math">\log x</span></td>
Line 147: Line 172:
<td></td>
<td></td>
</tr>
</tr>
-
<tr style="background:#E6E6E6;">
+
<tr>
<td></td>
<td></td>
<td align="center"><span class="math">\log_{a} x</span></td>
<td align="center"><span class="math">\log_{a} x</span></td>
Line 153: Line 178:
<td></td>
<td></td>
</tr>
</tr>
-
<tr>
+
<tr style="background:#E6E6E6;">
<td>Trigonometry</td>
<td>Trigonometry</td>
<td align="center"><span class="math">30^{\circ}</span></td>
<td align="center"><span class="math">30^{\circ}</span></td>
Line 159: Line 184:
<td></td>
<td></td>
</tr>
</tr>
-
<tr style="background:#E6E6E6;">
+
<tr>
<td></td>
<td></td>
<td align="center"><span class="math">\cos x</span></td>
<td align="center"><span class="math">\cos x</span></td>
Line 165: Line 190:
<td></td>
<td></td>
</tr>
</tr>
-
<tr>
+
<tr style="background:#E6E6E6;">
<td></td>
<td></td>
<td align="center"><span class="math">\sin x</span></td>
<td align="center"><span class="math">\sin x</span></td>
Line 171: Line 196:
<td></td>
<td></td>
</tr>
</tr>
-
<tr style="background:#E6E6E6;">
+
<tr>
<td></td>
<td></td>
<td align="center"><span class="math">\tan x</span></td>
<td align="center"><span class="math">\tan x</span></td>
Line 177: Line 202:
<td></td>
<td></td>
</tr>
</tr>
-
<tr>
+
<tr style="background:#E6E6E6;">
<td></td>
<td></td>
<td align="center"><span class="math">\cot x</span></td>
<td align="center"><span class="math">\cot x</span></td>
Line 183: Line 208:
<td></td>
<td></td>
</tr>
</tr>
-
<tr style="background:#E6E6E6;">
+
<tr>
-
<td>Various symbolsr</td>
+
<td>Various symbols</td>
<td align="center"><span class="math">\pi</span></td>
<td align="center"><span class="math">\pi</span></td>
<td align="center"><tt>\pi</tt></td>
<td align="center"><tt>\pi</tt></td>
Line 192: Line 217:
 +
<div class="exempel">
 +
''' Example '''
-
By combining these simple expressions, we may form more complex expressions. The table below shows some examples.
+
<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==
-
<table cellpadding="6" cellspacing="0" class="normalText" width="70%"
+
By combining simple expressions, we may form more complex expressions.
-
align="center">
+
-
<tr>
+
-
<td align="center"><b>Example</b></td>
+
-
<td align="center"><b>Latex-code</b></td>
+
-
</tr>
+
-
<tr style="background:#E6E6E6;">
+
-
<td align="center"><span class="math">ax^2+bx+c</span></td>
+
-
<td align="center"><tt>ax^2+bx+c</tt></td>
+
<div class="exempel">
-
</tr>
+
''' Example '''
-
<tr>
+
-
<td align="center"><span class="math">(a^2)^3=a^6</span></td>
+
-
<td align="center"><tt>(a^2)^3=a^6</tt></td>
+
-
</tr>
+
-
<tr style="background:#E6E6E6;">
+
-
<td align="center"><span class="math">\sqrt{x+2}</span></td>
+
<ol type="a">
-
<td align="center"><tt>\sqrt{x+2}</tt></td>
+
<li><math>\sqrt{x+2}\quad</math> is written <tt><nowiki><math></nowiki>\sqrt{x+2}<nowiki></math></nowiki></tt></li>
-
</tr>
+
<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>
-
<tr>
+
<li><math>2^{2^2}\quad</math> is written <tt><nowiki><math></nowiki>2^{2^2}<nowiki></math></nowiki></tt></li>
-
<td align="center"><span class="math">\sqrt{x+\sqrt{x}}</span></td>
+
<li><math>\sin\sqrt{x}\quad</math> is written <tt><nowiki><math></nowiki>\sin\sqrt{x}<nowiki></math></nowiki></tt></li>
-
<td align="center"><tt>\sqrt{x+\sqrt{x}}</tt></td>
+
</ol>
-
</tr>
+
</div>
-
<tr style="background:#E6E6E6;">
+
<div class="exempel">
-
<td align="center"><span class="math">\displaystyle\frac{a+b}{c-d}</span></td>
+
''' Example '''
-
<td align="center"><tt>\displaystyle\frac{a+b}{c-d}</tt></td>
+
 
-
</tr>
+
<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>
-
</table>
 
-
==Common mistakes==
+
==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.
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.
Line 235: Line 262:
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).
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 <math>\times</math> (<tt>\times</tt> in TeX).
+
Another frequent mistake is to use an asterisk (<tt>*</tt>) instead of a proper multiplication sign <math>\times</math> (<tt>\times</tt> in TeX).
<!-- Example -->
<!-- Example -->
Line 243: Line 270:
{| width="100%" cellspacing="0" cellpadding="0"
{| width="100%" cellspacing="0" cellpadding="0"
||
||
-
| align="center" width="35%" |TeX
+
| align="center" width="35%" |LaTeX
| align="center" width="35%" |Result
| align="center" width="35%" |Result
|-
|-
Line 287: Line 314:
{| width="100%" cellspacing="0" cellpadding="0"
{| width="100%" cellspacing="0" cellpadding="0"
||
||
-
| align="center" width="35%" |TeX
+
| align="center" width="35%" |LaTeX
| align="center" width="35%" |Result
| align="center" width="35%" |Result
|-
|-
Line 341: Line 368:
{| width="100%" cellspacing="0" cellpadding="0"
{| width="100%" cellspacing="0" cellpadding="0"
||
||
-
| align="center" width="35%" |TeX
+
| align="center" width="35%" |LaTeX
| align="center" width="35%" |Result
| align="center" width="35%" |Result
|-
|-
Line 393: Line 420:
{| width="100%" cellspacing="0" cellpadding="0"
{| width="100%" cellspacing="0" cellpadding="0"
||
||
-
| align="center" width="35%" |TeX
+
| align="center" width="35%" |LaTeX
| align="center" width="35%" |Result
| align="center" width="35%" |Result
|-
|-

Revision as of 09:37, 28 January 2009

       Theory          Exercises      

Contents:

  • What is TeX/LaTeX?
  • Common expressions
  • Common mistakes

Learning outcomes:

After this section you will have learned how to:

  • Write simple maths formulas in LaTeX.
  • Avoid common mistakes when coding maths in LaTeX.

To write mathematics efficiently on a computer in your individual assignment and the group task you will need write the maths in a coded form called LaTeX syntax. In this section you will learn the fundamentals of constructing LaTeX code that yields simple maths formulas.


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. \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.3\quad is written <math>4 < 5.3</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
Various symbols \pi \pi


Example

  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

  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

  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

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

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

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 \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. \displaystyle 5/2 instead of \displaystyle \tfrac{5}{2}) to enhance the legibility.

Example

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}