5.1 Writing formulas in TeX

From Förberedande kurs i matematik 1

(Difference between revisions)
Jump to: navigation, search
(New page: __NOTOC__ {| border="0" cellspacing="0" cellpadding="0" height="30" width="100%" | style="border-bottom:1px solid #797979" width="5px" |   {{Selected tab|[[5.1 Writing formulas in TeX...)
Line 20: Line 20:
* Explain mathematics
* Explain mathematics
}}
}}
 +
 +
==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 (*) instead of a proper multiplication sign <math>\times</math> (<tt>\times</tt> in TeX).
 +
 +
<!-- Example -->
 +
<div class="exempel">
 +
''' Example '''
 +
 +
{| width="100%" cellspacing="0" cellpadding="0"
 +
||
 +
| align="center" width="35%" |TeX
 +
| 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 '''
 +
 +
{| width="100%" cellspacing="0" cellpadding="0"
 +
||
 +
| align="center" width="35%" |TeX
 +
| 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 '''
 +
 +
{| width="100%" cellspacing="0" cellpadding="0"
 +
||
 +
| align="center" width="35%" |TeX
 +
| 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>\tfrac</tt>), while other fractions should be large (i.e. with <tt>\frac</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 '''
 +
 +
{| width="100%" cellspacing="0" cellpadding="0"
 +
||
 +
| align="center" width="35%" |TeX
 +
| 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'''
 +
 +
'''Useful web sites'''
 +
 +
* [http://scpd.stanford.edu/knuth/index.jsp A video course] in mathematical writing by Donald Knuth (A compendium accompaning the course is avalable in [http://www-cs-faculty.stanford.edu/~knuth/papers/mathwriting.tex.gz source form] or in excerpts from [http://books.google.com/books?id=dDOehHMbUMcC&printsec=frontcover&dq=inauthor:Donald+inauthor:Ervin+inauthor:Knuth&lr=&ei=JbN1SZfvFZysMqPPhM8M&hl=sv#PPP9,M1 Google books]).
 +
 +
</div>

Revision as of 12:28, 27 January 2009

       Theory          Exercises      

Contents:


Learning outcomes:

After this section you will have learned how to:

  • Express mathematics
  • Explain mathematics

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

TeX 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

TeX 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

TeX 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

TeX 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

Useful web sites