The Science Forum - Scientific Discussion and Debate  
 
 Live Chat    FAQ  Search  Usergroups
 
Register  ::  Log in Log in to check your private messages
 
Science Forum Forum IndexMathematicsTeX tutorial

Goto page 1, 2  Next
TeX tutorial View previous topic :: View next topic
Author Message
JaneBennet
Posted: Sun Jul 06, 2008 6:05 pm Post subject: TeX tutorial Reply with quote

Forum Ph.D.
Forum Ph.D.

Joined: 06 Apr 2008
Posts: 765

So a new feature has been implemented, called TeX. A new formatting button has also been added for it. So, some of you may ask, what is TeX, and how to use it? Okay, Ill try and explain. Suggestions, comments and corrections welcome. Smile

(i) What is TeX?
TeX is a neat way of formatting mathematical equations and other expressions in posts by means of GIF or PNG images (in this forum its PNG). These images are compact and can be inserted in lines of text. Although it is mostly used in mathematics, it can also be used in other areas for example, in chemistry:
    \mbox{CaCO$_3$(s) + 2HCl(aq) $\to$ CaCl$_2$(aq) + H$_2$O($l$) + CO$_2$(g)}
(ii) How do I use TeX?
Simply wrap your TeX expression in [tex] [/tex] tags. Notice that there is a new TeX formatting button available for this. For example, if you wrap
    y=ax^2+bx+c
in TeX tags, you get this:
    y=ax^2+bx+c

Neat, huh? Notice a couple of things:

(1) The TeX formatter ignores spacing and single line breaks. It automatically parses your equation and inserts relevant spaces so you dont have to do it yourself. In the example above, there is a space before and after the = and before and after each +.

(2) TeX follows typographical convention by formatting all variables in italic\ font.

(iii) What expressions can I type using TeX?
In our example above, the caret symbol ^ is used to format superscript. Similarly the underscore _ is used to format subscript:
    x^2 gives x^2

    x_0 gives x_0
Note that only the character immediately following the ^ or _ is formatted. If you wish to format more than one character in super-/subscript, you must enclose the characters in braces { }. For example, if you type
    x^12
you would get
    x^12
If you mean x to the power of 12 instead, you must type x^{12}:
    x^{12} gives x^{12}

The backslash \ generally precedes TeX commands. Here are a few examples of commonly typed expressions in TeX:
    \frac{1}{2} gives \frac{1}{2}

    \sqrt{2} gives \sqrt{2}

    \sqrt[3]{2} gives \sqrt[3]{2} (NB: square brackets [ ] around the 3, braces { } around the 2)

    \alpha, \beta, \gamma, \delta etc give \alpha, \beta, \gamma, \delta etc

    \mathbb{Z}, \mathbb{Q}, \mathbb{R}, \mathbb{C} give
    \mathbb{Z}, \mathbb{Q}, \mathbb{R}, \mathbb{C}
It is important to note that all TeX commands are case-sensitive.
_________________
 
A problem worthy of attack
Proves its worth by fighting back.
(Piet Hein)

Did You Know?
Fact of the day: Homotopy


Last edited by JaneBennet on Thu Aug 07, 2008 3:55 am; edited 4 times in total
Back to top
View user's profile Send private message Visit poster's website
JaneBennet
Posted: Sun Jul 06, 2008 6:40 pm Post subject: Reply with quote

Forum Ph.D.
Forum Ph.D.

Joined: 06 Apr 2008
Posts: 765

Now suppose you want to type an expression like z = cos(x); + isin(x). If you simply wrap that expression in TeX tags, you would get this:
    z=cos(x)+isin(x)
Doesnt look pretty, does it? As Ive said, TeX will format all variables in italics; if you dont tell the formatter what youre doing, it will interpret what you type as variables by default. In the formula above, you dont really wnat your cos and sin to be in italics.

Fortunately, there are special TeX commands to take care of common functions like sine and cosine. Simply type \cos and \sin:
    z=\cos(x)+i\sin(x) gives z=\cos(x)+i\sin(x)
Also, you might want to remove the brackets. But there is some minor thing to note here: if you type \cosx and \sinx, the formatter will try and parse these as TeX commands and youll end up with a TeX error as cosx and sinx arent valid TeX commands. To tell the formatter what to do, you have to insert a space before the x:
    z=\cos x+i\sin x gives z=\cos x+i\sin x
Although the formatter generally ignores spaces, so you dont generally need to type them, this is a case where the space is important: it serves to separate a TeX command from the variables you are using.

TeX commands for other commonly used functions include \tan, \cot, \sec, \lim, \max, \min, \gcd, etc.
_________________
 
A problem worthy of attack
Proves its worth by fighting back.
(Piet Hein)

Did You Know?
Fact of the day: Homotopy


Last edited by JaneBennet on Thu Aug 07, 2008 4:01 am; edited 2 times in total
Back to top
View user's profile Send private message Visit poster's website
Guitarist
Posted: Mon Jul 07, 2008 7:28 am Post subject: Reply with quote

Forum Ph.D.
Forum Ph.D.

Joined: 08 Jun 2005
Posts: 713

\in = \in

\ni =  \ni

\subseteq = \subseteq

\subset = \subset

\supseteq =  \supseteq

\supset = \supset Actually I generally prefer

\subsetneq and its analogue

\ne = \ne

\ge = \ge

\le = \le

\to = \to

\Rightarrow =  \Rightarrow

f \cdot g = f \cdot g

f \circ g =  f \circ g

\times =  \times

\otimes =  \otimes

\oplus = \oplus

\equiv = \equiv

\cong = \cong

\mathcal{X} = \mathcal{X}

\overline{X} = \overline{X}

\hat{i} = \hat{i}

\vec{v} = \vec{v}
Back to top
View user's profile Send private message
JaneBennet
Posted: Mon Jul 07, 2008 7:31 am Post subject: Reply with quote

Forum Ph.D.
Forum Ph.D.

Joined: 06 Apr 2008
Posts: 765

Useful TeX expressions in set theory:
    x\in A gives x\in A

    A\subset B gives A\subset B

    A\subseteq B gives A\subseteq B

    A\cup B gives A\cup B (union)

    A\cap B gives A\cap B (intersection)

    A\times B gives A\times B (Cartesian product)

    f:A\to B gives f:A\to B
There are two symbols for the empty set:
    \emptyset gives \emptyset

    \O gives \O
I prefer the latter because not only is it easier to type, it also looks much nicer than the other one. Stick Out Tongue

To type the negation of a symbol, you can use the TeX command \not e.g.
    x\not\in A gives x\not\in A

To be continued

[Oops, clash of posting with Guitarist. Razz]
_________________
 
A problem worthy of attack
Proves its worth by fighting back.
(Piet Hein)

Did You Know?
Fact of the day: Homotopy


Last edited by JaneBennet on Thu Aug 07, 2008 3:57 am; edited 1 time in total
Back to top
View user's profile Send private message Visit poster's website
Guitarist
Posted: Mon Jul 07, 2008 7:56 am Post subject: Reply with quote

Forum Ph.D.
Forum Ph.D.

Joined: 08 Jun 2005
Posts: 713

No worries! I learned something. I had never encountered \O =  \O. Thanks.

You can learn something too, Jane; \notin = \notin, i.e. you don't need the extra slash.

While I am here, let's all say a HUGE thanks to In(Sanity) for making this facility available.

I mean it, man - I also think (hope) it will bring the truckers in for breakfast dinner and tea!
Back to top
View user's profile Send private message
bit4bit
Posted: Tue Jul 08, 2008 10:47 am Post subject: Reply with quote

Forum Ph.D.
Forum Ph.D.

Joined: 14 Jul 2007
Posts: 623

\mathbb{T}\in X  \mathbb{R}\O\subseteq K\int !!!

Thanks IS...bout time I learned to use it properly
_________________
Chance favours the prepared mind.
Back to top
View user's profile Send private message
JaneBennet
Posted: Tue Jul 08, 2008 6:20 pm Post subject: Reply with quote

Forum Ph.D.
Forum Ph.D.

Joined: 06 Apr 2008
Posts: 765

Guitarist wrote:
You can learn something too, Jane; \notin = \notin, i.e. you don't need the extra slash.

Thanks for the tip. Smile I was howeer making the point that the command \not can be used in general cases, especially when the symbol to be negated is not in common use and so doesnt have its own TeX command: e.g. \not\cong = \not\cong.

On to more examples. (NB: \displaystyle is there only to improve the appearance of some TeX expressions; it can be omitted if you dont need to be overly neat in your TeX formatting. Wink)
    \{ =\ \{

    \} =\ \}

    \sim =\ \sim

    \infty =\ \infty

    \displaystyle\sum_{n=1}^k a_n =\ \displaystyle\sum_{n=1}^k a_n

    \displaystyle\prod_{n=1}^k a_n =\ \displaystyle\prod_{n=1}^k a_n

    \displaystyle\bigcup_{\lambda\in\Lambda}C_\lambda =\ \displaystyle\bigcup_{\lambda\in\Lambda}C_\lambda

    \displaystyle\bigcap_{\lambda\in\Lambda}C_\lambda =\ \displaystyle\bigcap_{\lambda\in\Lambda}C_\lambda

    \int_a^b\sin x\:dx =\ \int_a^b\sin x\:dx
Notice the command \: in the last line? I use it to add a small space between the x and the dx otherwise the characters after the sin would be all bunched up together like this: \int_a^b\sin xdx, which isnt quite so neat. This brings us to this useful topic:

Adding and removing spaces in TeX
Although TeX parses what you type and adds relevant spaces, you can add extra spaces of your own. The command \, adds a small space, \: adds a slightly bigger space and \; adds a still slightly bigger space, while \ followed an ordinary space adds an ordinary-sized space:
    a\,b =\ a\,b

    a\:b =\ a\:b

    a\;b =\ a\;b

    a\ b =\ a\ b
To add a wide space, use \quad:
    a\quad b =\ a\quad b
And if youre a real precisionist, you can even specify exactly how wide you want your space to be by using \hspace followed by a precise measurement in curly brackets:
    a\hspace{10mm}b =\ a\hspace{10mm}b
Note however that these space commands will only work between characters in a TeX expression. They will be ignored if theyre used right at the beginning or right at the end of your expression.

Now, what if you want to remove spaces in TeX? For example, if you type a+b in TeX, you get a+b with a little space before and after the + sign. However, if you really want to bunch the three characters together without any spaces in between, then use \! before each space you want to remove:
    a\!+\!b =\ a\!+\!b
More to come. Cool
_________________
 
A problem worthy of attack
Proves its worth by fighting back.
(Piet Hein)

Did You Know?
Fact of the day: Homotopy


Last edited by JaneBennet on Thu Aug 07, 2008 4:02 am; edited 5 times in total
Back to top
View user's profile Send private message Visit poster's website
William McCormick
Posted: Tue Jul 08, 2008 6:55 pm Post subject: Reply with quote

Forum Ph.D.
Forum Ph.D.

Joined: 03 Apr 2008
Posts: 1011

NO_2 Is how I learned the formula for ammonia. I like it. I like it. Nice work.

Sincerely,


William McCormick
_________________
http://www.Rockwelder.com


Last edited by William McCormick on Wed Jul 09, 2008 7:01 pm; edited 1 time in total
Back to top
View user's profile Send private message Send e-mail Visit poster's website
(In)Sanity
Posted: Tue Jul 08, 2008 8:18 pm Post subject: Reply with quote

Forum Cosmic Wizard
Forum Cosmic Wizard

Joined: 19 Oct 2004
Posts: 2139
Location: Phoenix AZ

Guitarist wrote:
No worries! I learned something. I had never encountered \O =  \O. Thanks.

You can learn something too, Jane; \notin = \notin, i.e. you don't need the extra slash.

While I am here, let's all say a HUGE thanks to In(Sanity) for making this facility available.

I mean it, man - I also think (hope) it will bring the truckers in for breakfast dinner and tea!


Welcome, sorry it took so long. I spent about 12 hours of my long weekend trying to get the thing working. It should have been about an hours work, but Murphy stepped in and made it about 12 Sad
_________________
"I don't know, I was really drunk at the time"
Back to top
View user's profile Send private message
JaneBennet
Posted: Tue Jul 08, 2008 9:18 pm Post subject: Reply with quote

Forum Ph.D.
Forum Ph.D.

Joined: 06 Apr 2008
Posts: 765

William McCormick wrote:
NO2 Is how I learned the formula for ammonia. I like it. I like it. Nice work.

Not exactly, William. If you want the formula for ammonia, you should type this:
    \mbox{NH}_3 =\ \mbox{NH}_3
Well, Im glad to know youre enjoying TeX as well. Smile

And this brings us nicely to another topic:

Text formatting in TeX
TeX format raw letters of the alphabet in italics: wrapping the tags around abc de gives abcde (with the space removed). Suppose you want regular font, no italics? Well, there are at least four ways of formatting regular font in TeX. One of them, as Ive used above, is the command \mbox. The others are \text, \textrm and \mathrm.
    \mbox{abc de} =\ \mbox{abc de}

    \text{abc de} =\ \text{abc de}

    \textrm{abc de} =\ \textrm{abc de}

    \mathrm{abc de} =\ \mathrm{abc de}
Notice that the typed space is ignored in the last one, \mathrm. In fact, with \mathrm, the whole input is parsed like any TeX expression, with relevant spaces added and redundant spaces removed, the sole exception being that variables are not italicized. Example:
    G=6.674\times10^{-11}\ \mathrm{m^3\:kg^{-1}\:s^{-2}} = G=6.674\times10^{-11}\ \mathrm{m^3\:kg^{-1}\:s^{-2}}
With the other three, what you type is parsed as plain text. If you need to format a small amout of your text as a math or math-type expression, it is handy to enclose it within a pair of dollar ($) signs:
    \textrm{Water is H$_2$O.} =\ \textrm{Water is H$_2$O.}
In a moment, we shall see that \mbox together with $ has a very special property of its own.

To format TeX expressions in italics, use \textit or \mathit. Since letters of the alphabet are already formatted in italics by default, this will probably be used for formatting other characters, particularly numerals.
    \textit{123 45} =\ \textit{123 45}

    \mathit{123 45} =\ \mathit{123 45}
Bold is \textbf and \mathbf.
    \textibf{abc 123} =\ \textbf{abc 123}

    \mathbf{abc 123} =\ \mathbf{abc 123}
And with a bit of ingenuity, you can combine the two.
    \textbf{$\textit{123 45}$} =\ \textbf{$\textit{123 45}$}
Finally, a word about \mbox{$$}. This has the property that it preserves the size of whatever it is formatting. Take an example: X^X gives X^X. The superscripted X is slightly smaller than the other X. Suppose you want both Xs to be of the same size? Then use \mbox{$$}! Laughing
    X^X \ =X^X

    X^\mbox{$X$} \ =X^\mbox{$X$}
Note: the dollar signs can be omitted if only numerals are being formatted. This is particularly handy when you have fractions within a fraction and you dont want TeX to make things too small to be legible:
    \frac{1}{1+\frac{1}{2}} =\ \frac{1}{1+\frac{1}{2}}
    \frac{\mbox{1}}{\mbox{$1+\frac{\mbox{1}}{\mbox{2}}}$} =\ \frac{\mbox{1}}{\mbox{$1+\frac{\mbox{1}}{\mbox{2}}}$}
The down side, obviously, is that your expression can quickly become too complicated with too many mboxes, so this will probably be something youll only want to use occasionally.

EDIT: Ive just learnt that with fractions, you can actually use \dfrac rather than \frac:
    \dfrac{1}{1+\dfrac{1}{2}} =\ \dfrac{1}{1+\dfrac{1}{2}}

_________________
 
A problem worthy of attack
Proves its worth by fighting back.
(Piet Hein)

Did You Know?
Fact of the day: Homotopy


Last edited by JaneBennet on Thu Aug 07, 2008 4:00 am; edited 2 times in total
Back to top
View user's profile Send private message Visit poster's website
bit4bit
Posted: Wed Jul 09, 2008 6:19 am Post subject: Reply with quote

Forum Ph.D.
Forum Ph.D.

Joined: 14 Jul 2007
Posts: 623

JaneBennet wrote:
William McCormick wrote:
NO2 Is how I learned the formula for ammonia. I like it. I like it. Nice work.

Not exactly, William. If you want the formula for ammonia, you should type this:
    \mbox{NH}_3 =\ \mbox{NH}_3
Well, I’m glad to know you’re enjoying TeX as well. Smile


Whats more, "NO2" is not a valid molecular formula. It should be NO_2 for nitrogen dioxide, N_2O for nitrous oxide (laughing gas), or 2NO for two moles of Nitric oxide.
_________________
Chance favours the prepared mind.
Back to top
View user's profile Send private message
Ophiolite
Posted: Thu Jul 10, 2008 3:05 am Post subject: Reply with quote

Forum Radioactive Isotope
Forum Radioactive Isotope

Joined: 25 Jul 2005
Posts: 4486
Location: Scotland

Irrelevant posts by William McCormick have been moved to the Chemistry section.
_________________
The Universe is not only weirder than we imagine it is weirder than we can imagine. J.B.S.Haldane.
Back to top
View user's profile Send private message
JaneBennet
Posted: Thu Jul 10, 2008 3:52 am Post subject: Reply with quote

Forum Ph.D.
Forum Ph.D.

Joined: 06 Apr 2008
Posts: 765

Thanks Ophi! Smile

Williams off-topic posts, originally posted here, have been moved to here. So lets crack on here.

Variable-sized brackets
Consider this
    (x-1)(\dfrac{1}{x-2)+\dfrac{1}{x-3})

    (x-1)(\dfrac{1}{x-2}+\dfrac{1}{x-3})
The first pair of brackets are fine, but the second pair look funny: theyre a bit too small! To rectify the problem, use the commands \left and \right.
    (x-1)\left(\dfrac{1}{x-2)+\dfrac{1}{x-3}\right)

    (x-1)\left(\dfrac{1}{x-2}+\dfrac{1}{x-3}\right)
\left and \right can also be used with square brackes [ and ], angle brackets < and > and curly brackets \{ and \} (note that you need a backslash for curly brackets); they also work with the pipe |.

You can even use one or other of them on its own (use \left\. if you dont want the left bracket to show).
    y=\left\{\begin{array}{ll}1&x\ge0\\0&x<0\end{array}

    y=\left\{\begin{array}{ll}1&x\ge0\\0&x<0\end{array}


    \left.\begin{array}{c}ax+by=0\\cx+dy=0\end{array}\right\}

    \left.\begin{array}{c}ax+by=0\\cx+dy=0\end{array}\right\}
Which introduces this gadget \begin{array}. Thats the tool used for doing matrices but well leave it for next time. Wink
_________________
 
A problem worthy of attack
Proves its worth by fighting back.
(Piet Hein)

Did You Know?
Fact of the day: Homotopy


Last edited by JaneBennet on Thu Aug 07, 2008 4:03 am; edited 1 time in total
Back to top
View user's profile Send private message Visit poster's website
JaneBennet
Posted: Sat Jul 12, 2008 7:20 am Post subject: Reply with quote

Forum Ph.D.
Forum Ph.D.

Joined: 06 Apr 2008
Posts: 765

Arrays and tables
This is going to be the most complicated of all the things discussed in this tutorial so far, so pay attention. Wink

Here is a sample matrix:
    \left(
    \begin{array}
    {rcl}
    1 & 2 & 3 \\
    11 & 22 & 33 \\
    111 & 222 & 333
    \end{array}
    \right)



    \left(
\begin{array}
{rcl}
1 & 2 & 3 \\
11 & 22 & 33 \\
111 & 222 & 333
\end{array}
\right)
I have added spaces and linebreaks to help you see better whats going on. When you type the code yourself, you can ignore some or all of the spaces and linebreaks. Basically, you follow these steps:

Step 1: Type \begin{array} to start.

Step 2: Specify the number of columns and alignment of text within each column. This is done by typing l, r and/or c within curly brackets { and }; l = left, r = right, c = centred. The number of l/r/cs will be the number of columns in your array.

In the example above, there are three columns: text is right-aligned in the first column, centre-aligned in the middle column, and left-aligned in the third column.

Step 3: Type the first row of your array, separating the contents of different columns with an ampersand &. If you have N columns, there should N−1 &s. You can type {} for a blank cell in a column of your row.

Step 4: If you have another row to enter, type \\ (double backslash). You can type \\\\ (double double backslash) for a bigger line spacing, \\\\\\ for an even bigger line spacing, etc. The number of backslashes should always be even.

Step 5: Type \end{array} to finish.

You can enclose an array in brackets. Use \left and \right with the type of brackets you want to use; you can also use the pipe | to enclose an array within vertical lines:
    \left|
    \begin{array}{cc}a & b\\c & d\end{array}
    \right|
    =ad-bc



    \left|
\begin{array}{cc}a & b\\c & d\end{array}
\right|
=ad-bc
The array method can also be used to build tables. In a table, you may often want to separate columns and/or rows with lines.

(i) To separate adjacent columns with a vertical line, use the pipe | in Step 2 in the instructions for doing an array above.
(ii) To separate adjacent rows with a horizontal line, type \hline after \\ in Step 4 above.

The following example (a table of the multiplication of the nonzero integers modulo 5) will demonstrate how its done.
    \begin{array}
    {c|cccc}
    \times_5 & 1 & 2 & 3 & 4 \\
    \hline
    1 & 1 & 2 & 3 & 4 \\
    2 & 2 & 4 & 1 & 3 \\
    3 & 3 & 1 & 4 & 2 \\
    4 & 4 & 3 & 2 & 1
    \end{array}



    \begin{array}
{c|cccc}
\times_5 & 1 & 2 & 3 & 4 \\
\hline
1 & 1 & 2 & 3 & 4 \\
2 & 2 & 4 & 1 & 3 \\
3 & 3 & 1 & 4 & 2 \\
4 & 4 & 3 & 2 & 1\end{array}

_________________
 
A problem worthy of attack
Proves its worth by fighting back.
(Piet Hein)

Did You Know?
Fact of the day: Homotopy


Last edited by JaneBennet on Thu Aug 07, 2008 4:04 am; edited 3 times in total
Back to top
View user's profile Send private message Visit poster's website
Guitarist
Posted: Tue Jul 15, 2008 1:08 pm Post subject: Reply with quote

Forum Ph.D.
Forum Ph.D.

Joined: 08 Jun 2005
Posts: 713

For straightforward matrices, the following is a nice shortcut:

\begin{pmatrix}
a & b &c\\
d & e & f\\
g & h & i
\end{pmatrix}

Giving \begin{pmatrix}
a & b &c\\
d & e & f\\
g & h & i
\end{pmatrix}

I believe, though I haven't checked, that bmatrix uses [ ] as delimiters, Bmatrix uses { } and vmatrix uses | |
Back to top
View user's profile Send private message
Display posts from previous:
Goto page 1, 2  Next Page 1 of 2

Science Forum Forum IndexMathematicsTeX tutorial
Jump to:



You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Google
 

© 2004-2008 Thescienceforum.com

Sponsored by EnluxLED

Partner Forums
Politics Forum  Radar Detector