LaTeX - A Crash Course

Often on this forum you will see equations embedded into the text of posts. Some are quite complex, others are simple. They are not produced by magical elves that live in the poster's computer - they are produced using a typesetting language called Latex (often written LaTeX or , but pronounced as "Latec"), and what is produced might look like this:

or even:

Latex itself is actually designed for writing whole documents - you can set out paragraph structure, tables, footnotes and all sorts of other things that normally go into a text document. This forum, however, borrows some of the Latex language, specifically that part used for typesetting mathematical equations.

This post will give you an introduction to the basics of Latex and is looseley grouped into two parts. The first two chapters cover the basic syntax (what the input should be like) and structure for Latex. This is obviously the sort of stuff you need to know before you can use Latex, so do take the time to look at the examples and see if you can understand what's going on. After the syntax has been covered, all that's left is the specifics of what you can do with Latex - how to insert symbols, equations, text and so forth, and this is the subject of the remaining chapters.

1) Using LaTeX

To use Latex on this forum, you have to place the Latex code between opening and closing math tags - you would type something in the format that's in the code box:

[math]<Latex commands go in between these tags>[/math]

Note that normally, writing the above would have made something that looks like this:

the only reason it didn't is because it's in the code box - to get the output for the following examples, type exactly what you see inside the code boxes, remembering that Latex is case sensitive.

Two important things about how Latex will format your equations:

1) It ignores spaces, and spaces characters how it thinks best - so you can spread out the comands you type to make them easier to read whilst you are typing, but they will produce the same output.

2) Latex also ignores carriage returns, where you start a new line - to start a new line in Latex, use the command:

\\

For example, the following two codes will both give the same output:

[math]
v = u + a t \\
F = m a
[/math]

[math]v=u+at\\F=ma[/math]

Shown here:

and:

Note that the first line will be indented by default - if you want to remove this, simply place the "\" command before the first line. So:

[math]
\\ v = u + a t \\
F = m a
[/math]

Gives:

2) Basic Expressions

As you've seen from above, basic expressions are typed in exactly as you would do when normally typing. You can use brackets, equals, minus, vertical bars "|", and so on.

Some things you cannot use directly are curled braces, like "{" or "}". Other characters that cannot be directly used are:

# $ % & ~ _ ^ \

as these all mean something special in the Latex language. If you want your output to display one of these characters, for example:

you have to preceed that character with a backslash, "". The above was produced using:

[math]a = 50 \%[/math]

The one exception to this is if you want to output the backslash character itself. This is done using the following command:

\backslash

.

3) Superscript and Subscript

The superscript and subscript commands in Latex are "^" and "_" respectively. So the following code will give the output below:

[math]x_1 = y^2 = z^4_1[/math]

Note that you can attatch both a sub- and superscript to the same charachter as in the above example.

The superscript and subscript characters normally just read the next character along, then go back to normal text formatting. For example, typing x_ijk in your code would give you

. To include more than one character, enclose them in curly braces "{" and "}". So:

[math]x_{ijk}[/math]

gives:

You can embed sub/superscripts within sub/superscripts, as in

- just remember to use braces where neccessary. The previous example was generated from:

[math]e^{x^{2_i}}[/math]

.

4) Fractions

Fractions such as

are done using the function:

[math]\frac{numerator}{denominator}[/math]

Just as with sub/superscripts, they can be embedded within other fractions.

5) Special Characters

Special characters include the following:

They are input using commands - words preceeded by backslash charachters. For example, typing "\pi" would output , "\sum" would produce , etc. A fairly thorough list of symbols can be found here.

6) Text in Latex

Text can be input in math mode, so you don't have to come out of it to say something. Text is done within the "\mbox{}" command, so:

[math]V_{sphere} = \frac{4}{3} \pi r^3 \mbox{ is a formula for the volume of the sphere with radius }r[/math]

First of all, basic trig functions - if you were to write just "cos x" in part of your code, it would look a bit funny. Like this:

. However, by using the special symbol "\cos", the cos in the output will not be in italics, it will look like this: . The same applies to tan, sin, and some other functions, as listed in the list of symbols, which can be found here.

Note that, if you use the "\lim" function followed by a subscript, the subscript will be placed directly underneath the "lim" text when it is output. For example:

[math]\lim_{x \rightarrow \infty} \frac{1}{x} = 0[/math]

Gives:

Now onto integrals. The text "\int" will give an integral sign,

, which can then be used with super/subscripts in the normal way to give boundaries. For example:

[math]\int_0^{\pi} \cos x dx = 0[/math]

Gives:

When the sumation symbol, "\sum", is used with super/subscripts, the text is placed directly above or below the sumation symbol. For example:

[math]\sum_{n=0}^{\infty} n[/math]

Gives:

Roots can be done using the "\sqrt{}" command, where whatever you want to contain within the square root sign goes in the curled braces. For example:

[math]r = \sqrt{ x^2 + y^2 }[/math]

Gives:

You can also utilise cube roots, nth roots. To do this, you still use the "\sqrt" command, but you put the expression you want before the root in square braces before the curled braces, so would be given by "\sqrt[a]{b}". The expression in the square braces, as well as in the curly braces, can be as complicated as you like. So:

[math]r^{ \frac{ 1 }{ n^2 + 2 } } = \sqrt[ n^2 + 2 ]{r}[/math]

Gives:

8) Dynamic Braces

Dynamic braces are handy when you have large fractions or large symbols (e.g. integrals, sums, etc). They automatically adjust to the size they should be. If you wanted braces around a fraction, you would normally just type something like:

[math]( \frac{n+2}{3} )[/math]

But this looks mildly strange, like this:

Dynamic braces provide a "fix" for this, so your output can look like this instead:

The above braces were produced using the "\left(" command for the opening brace, and the "\right)" command for the closing brace. This works in a similar fasion for square braces, "[]" and straight braces are treated the same, "||". If you want to produce dynamic curled braces, you still have to put a backslash before you type in the brace symbol itself (the same as with normal braces), like "\left\{" and "\right\}".You can "nest" braces within braces, but dynamic braces must be paired up (each opeing brace must have a closing brace and vice versa, though they do not have to be of the same type) or you will get an error as an output. If you wish to only have an opening - or closing - brace next to an expression, you can use one of the null braces, which are "\left." and "\right.". So, for example:

[math] \left. \frac{ n^2 + n}{ n^2 - n} \right\} [/math]

Gives:

9) Matrices

Matrices in Latex are done as arrays inside dynamic braces (see part (8), above). (They do not have to be done inside braces, but they almost always are. Either way, you get to choose what type of braces to put them in, if any.)When you type in your Latex code, first you'll put an opening brace (if you so wish) and then use the command "\begin{array}".Next, you have to define how many columns you want, and whether you want them aligned by the left, right, or center. The alignment is only important when you have equations and other long-winded expressions as elements in your matrix, for the following examples we'll keep everything aligned to the center for simplicity.To define how many columns you want, use an opening brace, followed by some letters - use "r", "l" or "c" for each column, indicating how you wish to align each column. For example, if you wanted a matrix with five columns, all aligned to the centre, the opening statement for your matrix will look like this:

\left(
\begin{array}{c c c c c}
...

If you wanted three columns with the first two aligned to the right, and the last one aligned to the center, you'd use "{r r c}" instead of the "{c c c c c}" command above.Now we start putting elements into our array. The first element that you put in will go into the first row of the first column. You then go along the row, putting elements in subsequent columns, seperating each element with an "&" symbol. You then indicate that you're moving onto the next row by using a double backslash "\" command. After you've entered the last element there is no need to close it with a "&" or "\", simply use the "\end{array}" command and, if applicable, close your braces.Here's two examples. Bear in mind that the formatting that's going into the code (the carriage returns and spaces that the user puts in, not the "\" and "&" commands) are for ease of reading only - you could take them all out and have one long string of text if you wanted. It would look messy and if you made a mistake, it'd be hard to spot, but Latex would give the same output.The code:

Also note that, although it's the case in the above examples, the number of rows in your matrix does not need to be the same as the number of columns. Arrays can be tricky to get the hang of as they can become fairly cumbersome quite easily, but take your time getting to grips with the basics and you'll soon master them.

Re: LaTeX - A Crash Course

justlookingforthemoment wrote:

I just changed all the 'charachters' in your post to 'characters'. If you actually meant to write 'charachters' in the first place, I will happily change them all back, and sorry if this is what you were intending!

Ah, no... that's just my rubbish spelling! Thanks for changing it so I look less of a fool

And Pi Man - next to the "submit" button when you go to post, there's a button that says "preview". As the name suggests, it'll give you a preview of your post, though it won't go up on the forum and you can still edit it.

I've still got some more stuff to do with this - I've been a little bogged down of late, but I should be able to get some more stuff put down.

Re: LaTeX - A Crash Course

Practice Using/Abusing LaTeX!

Here are some examples for you to practice using Latex. Simply reply to this post, and you can then start a new post - no need to actually post what you practice, though, all you need to do is press the "preview" button and you'll see what your post will look like, and you can mess around to your heart's content!

One other very handy hint: this forum has a very handy feature, that when a Latex output appears in a post, you are able to see the code that produced the output simply by clicking on the output itself. Try and copy the outputs given below by replying/previewing a post. You don't have to post them, and you don't have to check that your code is the same as my code - indeed, they may in some cases differ signifigantly, the important thing is that the outputs are the same.

Re: LaTeX - A Crash Course

Thanks for this great tutorial, hope its ok to 'practice' on this thread.

But i wonder, im getting some odd behaviour: if i add lots of newlines inside the math tag, the interpreter gives me something like 'math?' in latex font. e.g. this does not work because of the newlines inside the math tag:

Re: LaTeX - A Crash Course

Re: LaTeX - A Crash Course

Just added, hot-off-the-press sections to the original post above:

1) Roots (tagged on the end of the "special functions" section)2) Dynamic braces3) Matrices

I think that's pretty much everything covered. Is there anything anyone thinks should be included? Any LaTeX old-hands spot something I've missed out? Has anybody seen something in a post and wondered how it's done? Or are they wondering how they would do something if they needed to? Anything need clarifying/clearing up?

Re: LaTeX - A Crash Course

You can put more spacing between charachters using (in increacing order of the amount of space it'll give you) "\,", "\:", "\;", "\quad" and "\qquad".

a b \, c \: d \; e \quad f \qquad g

Edit: Oh yeah, strange spacing is before the first line, but only if there's more than one line It's just a forum thing - put a double-backslash as the first thing in your math-tags and it's not a problem.