5.1 Writing formulas in TeX

From Förberedande kurs i matematik 1

(Difference between revisions)
Jump to: navigation, search
m (Phil Ramsden changes)
Current revision (07:03, 6 April 2009) (edit) (undo)
(Added explanatory text in examples)
 
Line 306: Line 306:
| align="center" width="35%" |Result
| align="center" width="35%" |Result
|-
|-
-
||<ol type="a" style="padding-top: 5px; padding-bottom: 0px; margin-bottom: 0px"><li>'''Don't''' write</li></ol>
+
||<ol type="a" style="padding-top: 5px; padding-bottom: 0px; margin-bottom: 0px"><li>Don't forget backslash (\)</li></ol>
| align="center" valign="bottom" |<tt>sin x</tt>
| align="center" valign="bottom" |<tt>sin x</tt>
| align="center" valign="bottom" |<math>sin x</math>
| 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>
+
||<ol style="list-style-type: none; padding-top: 10px; padding-bottom: 0px; margin-top: 0px; margin-bottom: 0px"><li>Remember to put a space after a command</li></ol>
| align="center" valign="bottom" |<tt>\sinx</tt>
| align="center" valign="bottom" |<tt>\sinx</tt>
| align="center" valign="bottom" |<span style="color:red">Error</span>
| align="center" valign="bottom" |<span style="color:red">Error</span>
Line 318: Line 318:
| align="center" valign="bottom" |<math>\sin x</math>
| 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>
+
||<ol type="a" start="2" style="padding-top: 5px; padding-bottom: 0px; margin-bottom: 0px"><li>Don't use asterisk as a multiplication sign</li></ol>
| align="center" valign="bottom" |<tt>4*3</tt>
| align="center" valign="bottom" |<tt>4*3</tt>
| align="center" valign="bottom" |<math>4*3</math>
| align="center" valign="bottom" |<math>4*3</math>
Line 326: Line 326:
| align="center" valign="bottom" |<math>4\times 3</math>
| 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>
+
||<ol type="a" start="3" style="padding-top: 5px; padding-bottom: 0px; margin-bottom: 0px"><li>The multiplication sign is usually not inserted between variables</li></ol>
| align="center" valign="bottom" |<tt>a\times b</tt>
| align="center" valign="bottom" |<tt>a\times b</tt>
| align="center" valign="bottom" |<math>a\times b</math>
| align="center" valign="bottom" |<math>a\times b</math>
Line 350: Line 350:
| align="center" width="35%" |Result
| align="center" width="35%" |Result
|-
|-
-
||<ol type="a" style="padding-top: 5px; padding-bottom: 0px; margin-bottom: 0px"><li>'''Don't''' write</li></ol>
+
||<ol type="a" style="padding-top: 5px; padding-bottom: 0px; margin-bottom: 0px"><li>Don't omit <tt>^</tt></li></ol>
| align="center" valign="bottom" |<tt>a2</tt>
| align="center" valign="bottom" |<tt>a2</tt>
| align="center" valign="bottom" |<math>a2</math>
| align="center" valign="bottom" |<math>a2</math>
Line 358: Line 358:
| align="center" valign="bottom" |<math>a^2</math>
| 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>
+
||<ol type="a" start="2" style="padding-top: 5px; padding-bottom: 0px; margin-bottom: 0px"><li>Don't omit <tt>_</tt></li></ol>
| align="center" valign="bottom" |<tt>x1</tt>
| align="center" valign="bottom" |<tt>x1</tt>
| align="center" valign="bottom" |<math>x1</math>
| align="center" valign="bottom" |<math>x1</math>
Line 366: Line 366:
| align="center" valign="bottom" |<math>x_1</math>
| 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>
+
||<ol type="a" start="3" style="padding-top: 5px; padding-bottom: 0px; margin-bottom: 0px"><li>Remember to use braces</li></ol>
| align="center" valign="bottom" |<tt>a^22</tt>
| align="center" valign="bottom" |<tt>a^22</tt>
| align="center" valign="bottom" |<math>a^22</math>
| align="center" valign="bottom" |<math>a^22</math>
Line 374: Line 374:
| align="center" valign="bottom" |<math>a^{22}</math>
| 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>
+
||<ol type="a" start="4" style="padding-top: 5px; padding-bottom: 0px; margin-bottom: 0px"><li>Don't use "o" as the degree sign</li></ol>
| align="center" valign="bottom" |<tt>30^{o}</tt>
| align="center" valign="bottom" |<tt>30^{o}</tt>
| align="center" valign="bottom" |<math>30^{o}</math>
| 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>
+
||<ol style="list-style-type: none; padding-top: 10px; padding-bottom: 0px; margin-top: 0px; margin-bottom: 0px"><li>Don't use "0" as the degree sign</li></ol>
| align="center" valign="bottom" |<tt>30^{0}</tt>
| align="center" valign="bottom" |<tt>30^{0}</tt>
| align="center" valign="bottom" |<math>30^{0}</math>
| align="center" valign="bottom" |<math>30^{0}</math>
Line 404: Line 404:
| align="center" width="35%" |Result
| align="center" width="35%" |Result
|-
|-
-
||<ol type="a" style="padding-top: 3px; padding-bottom: 3px"><li>'''Don't''' write</li></ol>
+
||<ol type="a" style="padding-top: 3px; padding-bottom: 3px"><li>Use the correct number of brackets</li></ol>
| align="center" valign="center" |<tt>(1-(1-x)</tt>
| align="center" valign="center" |<tt>(1-(1-x)</tt>
| align="center" valign="center" |<math>(1-(1-x)</math>
| align="center" valign="center" |<math>(1-(1-x)</math>
Line 412: Line 412:
| align="center" valign="center" |<math>(1-(1-x))</math>
| 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>
+
||<ol type="a" start="2" style="padding-top: 10px; padding-bottom: 10px"><li>Brackets should be as large as the expression</li></ol>
| align="center" valign="center" |<tt>(\dfrac{a}{b}+c)</tt>
| align="center" valign="center" |<tt>(\dfrac{a}{b}+c)</tt>
| align="center" valign="center" |<math>(\dfrac{a}{b}+c)</math>
| align="center" valign="center" |<math>(\dfrac{a}{b}+c)</math>
Line 420: Line 420:
| align="center" valign="center" |<math>\left(\dfrac{a}{b}+c\right)</math>
| 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>
+
||<ol type="a" start="3" style="padding-top: 5px; padding-bottom: 5px"><li>Don't use parentheses to delimit arguments</li></ol>
| align="center" valign="center" |<tt>\frac(1)(2)</tt>
| align="center" valign="center" |<tt>\frac(1)(2)</tt>
| align="center" valign="center" |<math>\tfrac(1)(2)</math>
| align="center" valign="center" |<math>\tfrac(1)(2)</math>
Line 428: Line 428:
| align="center" valign="center" |<math>\tfrac{1}{2}</math>
| 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>
+
||<ol type="a" start="4" style="padding-top: 5px; padding-bottom: 5px"><li>Don't use parentheses to delimit arguments</li></ol>
| align="center" valign="center" |<tt>\sqrt(a+b)</tt>
| align="center" valign="center" |<tt>\sqrt(a+b)</tt>
| align="center" valign="center" |<math>\sqrt(a+b)</math>
| 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>
+
||<ol style="list-style-type: none; padding-top: 5px; padding-bottom: 5px; margin-top: 0px; margin-bottom: 0px"><li>Avoid redundant brackets</li></ol>
| align="center" valign="center" |<tt>\sqrt{(a+b)}</tt>
| align="center" valign="center" |<tt>\sqrt{(a+b)}</tt>
| align="center" valign="center" |<math>\sqrt{(a+b)}</math>
| align="center" valign="center" |<math>\sqrt{(a+b)}</math>
Line 456: Line 456:
| align="center" width="35%" |Result
| align="center" width="35%" |Result
|-
|-
-
||<ol type="a" style="padding-top: 5px; padding-bottom: 5px><li>'''Don't''' write</li></ol>
+
||<ol type="a" style="padding-top: 5px; padding-bottom: 5px><li>Don't write numerical fractions large</li></ol>
| align="center" valign="center" |<tt>\dfrac{1}{2}</tt>
| align="center" valign="center" |<tt>\dfrac{1}{2}</tt>
| align="center" valign="center" |<math>\dfrac{1}{2}</math>
| align="center" valign="center" |<math>\dfrac{1}{2}</math>
Line 466: Line 466:
| 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>
| 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>
+
||<ol type="a" start="2" style="padding-top: 5px; padding-bottom: 5px><li>Don't write symbolic fractions small</li></ol>
| align="center" valign="center" |<tt>\frac{a}{b}</tt>
| align="center" valign="center" |<tt>\frac{a}{b}</tt>
| align="center" valign="center" |<math>\tfrac{a}{b}</math>
| align="center" valign="center" |<math>\tfrac{a}{b}</math>
Line 474: Line 474:
| align="center" valign="center" |<math>\dfrac{a}{b}</math>
| 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>
+
||<ol type="a" start="3" style="padding-top: 5px; padding-bottom: 5px><li>Don't write complicated fractions small</li></ol>
| align="center" valign="center" |<tt>\frac{\sqrt{3}}{2}</tt>
| align="center" valign="center" |<tt>\frac{\sqrt{3}}{2}</tt>
| align="center" valign="center" |<math>\tfrac{\sqrt{3}}{2}</math>
| align="center" valign="center" |<math>\tfrac{\sqrt{3}}{2}</math>
Line 482: Line 482:
| align="center" valign="center" |<math>\dfrac{\sqrt{3}}{2}</math>
| 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>
+
||<ol type="a" start="4" style="padding-top: 5px; padding-bottom: 5px"><li>No stacked fractions in exponents</li></ol>
| align="center" valign="center" |<tt>a^{\frac{1}{2}}</tt>
| align="center" valign="center" |<tt>a^{\frac{1}{2}}</tt>
| align="center" valign="center" |<math>a^{\frac{1}{2}}</math>
| align="center" valign="center" |<math>a^{\frac{1}{2}}</math>

Current revision

       Theory          Exercises      

Contents:

  • LaTeX maths

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 to 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 formulae are written in a straightforward way.

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{1}{2} \frac{1}{2} Small stacked fraction
\dfrac{a}{b} \dfrac{a}{b} Large stacked 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 \lg x \lg x
\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 Implies
\Leftarrow \Leftarrow Is implied by
\Leftrightarrow \Leftrightarrow Is equivalent to
Various symbols \pi \pi
\alpha, \beta, \theta, \varphi \alpha, \beta, \theta, \varphi


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}\quad is written <math>\tan 30^{\circ}</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 forget backslash (\)
sin x \displaystyle sin x
  1. Remember to put a space after a command
\sinx Error
  1. Do write
\sin x \displaystyle \sin x
  1. Don't use asterisk as a multiplication sign
4*3 \displaystyle 4*3
  1. Do write
4\times 3 \displaystyle 4\times 3
  1. The multiplication sign is usually not inserted between variables
a\times b \displaystyle a\times b
  1. Do write
ab \displaystyle ab

Superscripts and subscripts

When writing superscripts, such as exponents, you use ^, and to write subscripts you use _. If the super- or subscript consists of more than one symbol it must be enclosed with braces {}.

A special kind of superscript is the degree sign (°) which is written as ^{\circ}.

Example 6

LaTeX Result
  1. Don't omit ^
a2 \displaystyle a2
  1. Do write
a^2 \displaystyle a^2
  1. Don't omit _
x1 \displaystyle x1
  1. Do write
x_1 \displaystyle x_1
  1. Remember to use braces
a^22 \displaystyle a^22
  1. Do write
a^{22} \displaystyle a^{22}
  1. Don't use "o" as the degree sign
30^{o} \displaystyle 30^{o}
  1. Don't use "0" as the degree sign
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 to delimit arguments (input values) of commands such as \sqrt and \frac.

Example 7

LaTeX Result
  1. Use the correct number of brackets
(1-(1-x) \displaystyle (1-(1-x)
  1. Do write
(1-(1-x)) \displaystyle (1-(1-x))
  1. Brackets should be as large as the expression
(\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 use parentheses to delimit arguments
\frac(1)(2) \displaystyle \tfrac(1)(2)
  1. Do write
\frac{1}{2} \displaystyle \tfrac{1}{2}
  1. Don't use parentheses to delimit arguments
\sqrt(a+b) \displaystyle \sqrt(a+b)
  1. Avoid redundant brackets
\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 numerical fractions large
\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 symbolic fractions small
\frac{a}{b} \displaystyle \tfrac{a}{b}
  1. Do write
\dfrac{a}{b} \displaystyle \dfrac{a}{b}
  1. Don't write complicated fractions small
\frac{\sqrt{3}}{2} \displaystyle \tfrac{\sqrt{3}}{2}
  1. Do write
\dfrac{\sqrt{3}}{2} \displaystyle \dfrac{\sqrt{3}}{2}
  1. No stacked fractions in exponents
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 Wikipedia's help page
  • Two more thorough texts on 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.