14
$\begingroup$

Mathematical notation written using MathJax (mathematical Latex) can make questions and answers much clearer and more succinct, but how do we learn to produce this output?

Feel free to improve the community wiki answer to give those of us new to MathJax and Latex the best chance of getting to grips with it.

$\endgroup$
6
  • $\begingroup$ Do we need any other FAQs? $\endgroup$
    – HDE 226868 Mod
    Dec 14, 2014 at 22:59
  • 1
    $\begingroup$ @HDE226868 Each time someone thinks of one we can add it... :) $\endgroup$ Dec 14, 2014 at 23:19
  • 1
    $\begingroup$ @HDE226868 It might be worth browsing the other meta sites to see what they found useful. I've added a link to the similar MathJax guide on meta.math.SE and I'm guessing other sites will have lots of other FAQs that we could emulate or simply link to. $\endgroup$ Dec 14, 2014 at 23:21
  • 1
    $\begingroup$ Incidentally the guide on meta.math.SE has 20 answers and is staggeringly comprehensive - I think we should focus on the basics of getting things working here and let that guide cover all the advanced stuff. $\endgroup$ Dec 14, 2014 at 23:38
  • 2
    $\begingroup$ Agreed. Let them do all the work. :-) $\endgroup$
    – HDE 226868 Mod
    Dec 14, 2014 at 23:41
  • 1
    $\begingroup$ This (the answer) is incredibly useful (for me). Thank you for the question. I think I can handle it from now on. I was afraid to ask, "how do you do the fancy math-thingy?" $\endgroup$
    – Mikey
    Sep 18, 2015 at 18:10

1 Answer 1

16
$\begingroup$

The LateX code required to give a particular output can be learned from a variety of sources, from the TeX stack exchange site to mathurl.com where you can piece together an expression and then see what code produces it. This and this are also good dictionaries for LaTeX. You can also click "edit" below any question or answer you'd like to emulate, and see what code was typed to get that result (just don't save the edit...).

To get seriously mathematical, see the similar guide on meta.math.SE.

The first thing you need to know is that when embedded in a sentence, the code is surrounded by dollar signs, and when centered as a separate paragraph the code is surrounded by double dollar signs. (Apparently, TeX uses dollar signs because formatting math used to be expensive)

For example:

Here is $x=y$ in the middle of a sentence

Here is $x=y$ in the middle of a sentence.


Here is $$x=y$$ as a separate centered paragraph

Here is $$x=y$$ as a separate centered paragraph.


As long as you know this, you can experiment to see what works, checking the preview as you go.

Since we're all keen to see you contributing, drop into chat if anything is unclear - this will also help us know what to improve in this answer...


Letters and numbers

Letters in LaTeX inside the dollar signs are easy - just type in the letter.

Example: A $\to A$

The same goes for numbers:

Example: 1 $\to 1$

Greek letters require you to spell out the letter after a slash mark. Use a lowercase first letter for a lowercase Greek letter, and uppercase for an uppercase Greek letter.

Example: \gamma $\to \gamma$

Example: \Gamma $\to \Gamma$


Text

In LaTeX, writing out words within dollar signs simply renders them as a series of letters. For example, writing out 'apple' becomes $apple$. However, you can use the syntax \text{apple} to make it different:

Example:

apple $\to apple$

\text{apple} $\to \text{apple}$

When combining words and numbers (e.g. in units after a quantity), the two parts can blend together: $560kilograms$. Use a space in the syntax: \text{ kilograms}.

Example: 560 \text{ kilograms} $\to 560 \text{ kilograms}$

Or you can use "\space" as a command.

$x\space y$ 

to $x\space y$


Changing the size of the font

You can also make the text different sizes: huge, large, small and tiny. Use a slash and then the size name before typing in your text.

Example:

$\huge \text{Huge}$

$\huge \text{Huge}$

$\large \text{Large}$

$\large \text{Large}$

$\small \text{Small}$

$\small \text{Small}$

$\tiny \text{Tiny}$

$\tiny \text{Tiny}$

These do not have to be used with the \text{} function, if you do not want the LaTeX to be words. These can be used on normal characters, too:

$\huge x=y$

$\huge x=y$


Other formatting commands

Boxed text

 $\boxed{text}$

$\boxed{boxed}$

Highlighting text

 $\bbox[color]{text}$

$\bbox[yellow]{highlight}$


Colors

You can color text like this:

 $\color{color}{text}$

You can also work with any symbol like \varnothing or \div: $$\color{red}{\varnothing}\color{blue}{\div}$$ Written by:

 $\color{red}{\varnothing}\color{blue}{\div}$ 

(Written with double dollar-signs so as to display).This only works if you use the regular command (like \div). Sizes apply as well: $\color{red}{\huge{\div}}$

by

 $\color{red}{\huge{\div}}$

You can also use fonts, positioning, etc. The "colors" are applicable to anything as long as they follow the

 $\color{color here}{text here}$ 

format and any sizes/fonts/symbols and additional opening/closing of brackets continues normally.

Examples of colors here and the colors they produce:

$\color{black}{black}\color{blue}{blue}\color{brown}{brown}\color{cyan}{cyan}\color{darkgray}{darkgrey}\color{gray}{gray}\color{green}{green}\color{lightgray}{lightgray}\color{lime}{lime}\color{magenta}{magenta}\color{olive}{olive}\color{orange}{orange}\color{pink}{pink}\color{purple}{purple}\color{red}{red}\color{teal}{teal}\color{violet}{violet}\color{yellow}{yellow}$

Lastly, it is possible to use any colors if you know the hexadecimal color code based on their RGB values, which you can find here.

 $\color{#hex color here}{text here}$

Example with 3 different gold, there is a subtle difference: $\color{#daa400}{gold}$ $\color{#f7d100}{gold}$ $\color{ #f1bb00}{gold}$


Operators

Operators such as multiplication and division aren't done the same way as they are in other applications (i.e. programming or computer calculators). Some signs are, but some have their own codes.

Addition | Subtraction | Multiplication | Division
  $+$    |     $-$     |    $\times$    |  $\div$

$+ | - | \times | \div$

You can also use indices and roots, among others.

Raising a number uses base^{index} syntax:

$3^{4}$

$3^{4}$

Square roots go \sqrt{number}:

$\sqrt{625}$

$\sqrt{625}$


Parentheses and Brackets

Parentheses and brackets are typically easy:

$(  )$
$[  ]$

Do not do this, though, because it is used in other syntaxes:

${  }$

Those are reserved for other cases, such as doing an operation on multiple characters:

$6^{7x}$

$6^{7x}$

$\int_{x+2}^xf(x)dx$

$\int_{x+2}^xf(x)dx$

You can, however, escape them by preceding them with a \:

$\{1,2,3\}$

$\{1,2,3\}$

You can adapt brackets and parentheses by using the \left and \right features:

$x \times (\frac{\frac{x}{6}}{y+3}+1)$

$x\times (\frac{\frac{x}{6}}{y+3}+1)$

can be written as

$x \times \left(\frac{\frac{x}{6}}{y+3}+1 \right)$

$x \times \left(\frac{\frac{x}{6}}{y+3}+1 \right)$

This makes LaTeX more aesthetically pleasing. Brackets ([]), escaped braces (\{\}), and pipes (||, useful for absolute value) can be used, too.

Special symbols

If you need a special symbol, the most convenient way to find it is Detexify which allows you to draw a symbol and searches for $\rm\LaTeX$ symbols that are similar. There's also the Comprehensive $\rm\LaTeX$ Symbol List which lists all symbols of $\rm\LaTeX$. However note that not all of the $\rm\LaTeX$ symbols are implemented in MathJax.


Titles

While using MathJax in titles is technically possible, please try to avoid it as it can make questions more difficult to find. Instead of having a question title that contains a mathematical formula, keep the question title a description of what you want to know. If a good question title relies on mathematical formulas, consider whether the question is really on topic on this site; maybe it's really a better fit for one of the hard science sites?

Spoilers

Normal spoilers

Don't work with LaTeX. On Mathematics, Achille Hui wrote up a basic one, which can be very helpful. Here's that example:

$$\require{action} \toggle{ \begin{array}{cl} & \bbox[2pt,color:red;border-radius:3px;box-shadow:4px 4px 8px firebrick]{ \verb/click me for hint/} \end{array} }{ \begin{array}{cl} 1. & \text{Define }x \text{ = (complex formula)}\\ & \bbox[2pt,color:red;border-radius:3px;box-shadow:4px 4px 8px firebrick]{ \verb/more hint.../} \end{array} }{ \begin{array}{cl} 1. & \text{Define }x\text{ = (complex formula)}\\ 2. & \text{Observe } f(x) = 0\\ & \bbox[2pt,color:red;border-radius:3px;box-shadow:4px 4px 8px firebrick]{ \verb/That's it, thanks for wasting your time!/} \end{array} } \endtoggle$$

The code to make this happen:

$$\require{action}
\toggle{
\begin{array}{cl}
& \bbox[2pt,color:red;border-radius:3px;box-shadow:4px 4px 8px firebrick]{
\verb/click me for hint/}
\end{array}
}{
\begin{array}{cl}
1. & \text{Define }x \text{ = (complex formula)}\\
& \bbox[2pt,color:red;border-radius:3px;box-shadow:4px 4px 8px firebrick]{
\verb/more hint.../}
\end{array}
}{
\begin{array}{cl}
1. & \text{Define }x\text{ = (complex formula)}\\
2. & \text{Observe } f(x) = 0\\
& \bbox[2pt,color:red;border-radius:3px;box-shadow:4px 4px 8px firebrick]{
\verb/That's it, thanks for wasting your time!/}
\end{array}
}
\endtoggle$$
$\endgroup$
1
  • 2
    $\begingroup$ OMG. Thank you for this. I was too afraid to ask, "how do you do the fancy math thing?" This is very perfect. $\endgroup$
    – Mikey
    Sep 18, 2015 at 18:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .