7
\$\begingroup\$

How can I incorporate good-looking mathematical exposition into my question/answer?

Return to FAQ index

\$\endgroup\$
1
7
\$\begingroup\$

NOTE: Code Golf now supports MathJax (see the other answer), so this is no longer accurate; however, the other advice in the post is worth keeping around.


codegolf.SE does not use MathJax, because its load time is too high (see discussion). But although there is not built-in support for LaTeX, you can visualize mathematical formulae in various ways

Images

You can include mathematical formulae as images. There a basically three ways how to get such an image:

  • Use the preview of math.SE to create your formula, make a screenshot and crop it to the right size.
  • Create a LaTeX document on your computer, make a screenshot and crop it to the right size.
  • Use command line tools (see this question)
  • Use MathURL to create an image of the equation (fastest option for images)

Symbols

Many math symbols are also available as plain text. You can create your formula by using copy-and-paste:

The most basic symbols that you might need are:

  • Arrows: ← ↓ → ↑ ↔ ↵ ⇐ ⇓ ⇒ ⇑ ⇔
  • Sets: ∈ ∉ ∅ ⊆ ⊂ ⊄ ∪ ∩
  • Relations: ≠ ≤ ≥ ≈
  • Logic: ¬ ∧ ∨ ∃ ∀
  • More: ∫ ∞ ε Δ ⋅

Note that most platforms offer some kind of extended character input palette which makes this input method easier. (e.g. Character Viewer in OSX, Character Map in Windows)

Pure ASCII

While this is the least elegant-looking option, it produces results intelligible to anyone who understands the corresponding mathematical symbols. Handy if you want to put equations in your source code comments.

  • asciiTeX - Binary for Windows only, but it is available on Homebrew for OSX and should build for Linux.
\$\endgroup\$
7
  • \$\begingroup\$ Awesome! I've added a few details, will put in more later when I have more time. \$\endgroup\$ Mar 20 '14 at 15:14
  • \$\begingroup\$ Did you copy the symbols idea from me? Yay. I would like to suggest the Window's Snipping tool for making screenshots. It works just like a program I made, but it's better because it is easier to snip an image. I was overjoyed when a professor showed this program to us for use in our Labs. \$\endgroup\$
    – Justin
    Mar 31 '14 at 3:40
  • 1
    \$\begingroup\$ As far as posting images go, I think a simpler option than screenshots is MathURL \$\endgroup\$
    – Ephraim
    Jun 7 '14 at 23:02
  • \$\begingroup\$ As MathURL doesn't support https, I highly recommend (And with heavy bias) using a-ta.co \$\endgroup\$
    – ATaco
    May 16 '17 at 0:01
  • \$\begingroup\$ @ATaco Is this link outdated, or this page just down at the moment? \$\endgroup\$ Jul 13 '17 at 4:32
  • \$\begingroup\$ @musicman523 Just down for a quick maintenance, should be back up shortly. \$\endgroup\$
    – ATaco
    Jul 13 '17 at 4:32
  • 4
    \$\begingroup\$ Since MathJax is now enabled, this answer is no longer accurate in this regard. \$\endgroup\$ Aug 30 '18 at 10:16
2
\$\begingroup\$

Code Golf, as of June 20th 2018, has \$\LaTeX\$ enabled on both main and meta sites. A much more complete guide on how to use Mathjax can be found here, but here are the basics for those not familiar:

  • To include inline formula, enclose the formula with \$: \$\sin x^2\$ is \$\sin x^2\$
  • For displayed formula, use $$: $$\frac {\log n} \pi$$ is

$$\frac {\log n} \pi$$

  • For subscripts and superscripts, use _ and ^: x_2 (\$x_2\$) and 5^2 (\$5^2\$)

  • Most operations only work on the next character: 2^10 (\$2^10\$). In order to group multiple parts together, use {}: 2^{10} (\$2^{10}\$). Notice the difference between \$x_i^2\$ and \$x_{i^2}\$ (x_i^2 and x_{i^2})

  • Fractions. There are three ways to display fractions:

    • \frac{a+1}{b+1}: \$\frac{a+1}{b+1}\$
    • \over for more complex fractions: {x^2+5x-6 \over 7sin(y^3)} is \${x^2+5x-6 \over 7\sin(y^3)}\$
    • \cfrac for continued fractions: \cfrac a b is \$\cfrac a b\$
  • Sums and products are \sum and \prod respectively: \$\sum\$, \$\prod\$. Use a superscript argument for the value on top and a subscript for the value underneath: \sum_{i=0}^{10} i is \$\sum_{i=0}^{10} i\$

  • Special characters, such as {, _ etc. can be escaped by prefixing with a backslash: \$\{ \_ \$. A backslash should be \backslash

  • Some common symbols: \lt \gt \le \leq \ge \geq \times \div \pm \cdot \circ \to \infty are \$\lt \gt \le \leq \ge \geq \times \div \pm \cdot \circ \to \infty\$

  • Some common functions: \sin \tan \cos \lim are \$\sin \tan \cos \lim\$. You can use subscripts to change limits: \lim_{x \to 0} is $$\lim_{x \to 0}$$


Some slightly less common things:

Matrices

Use $$\begin{matrix}…\end{matrix}$$ In between the \begin and \end, put the matrix elements. End each matrix row with \\, and separate matrix elements with &. For example,

$$
    \begin{matrix}
    1 & x & x^2 \\
    1 & y & y^2 \\
    1 & z & z^2 \\
    \end{matrix}
$$

produces

$$ \begin{matrix} 1 & x & x^2 \\ 1 & y & y^2 \\ 1 & z & z^2 \\ \end{matrix} $$

Aligned equations

Often people want a series of equations where the equals signs are aligned. To get this, use $$\begin{align}…\end{align}$$. Each line should end with \\, and should contain an ampersand (&) at the point to align at, typically immediately before the equals sign. For example,

$$\begin{align}
x^2 + 2x - 1 & = x^2 + 2x + 1 - 2 \\
& = (x + 1)^2 - 2
\end{align}$$

produces

$$\begin{align} x^2 + 2x - 1 & = x^2 + 2x + 1 - 2 \\ & = (x + 1)^2 - 2 \end{align}$$

Definitions by cases

Use $$\begin{cases}…\end{cases}$$. End each case with a \\, and use & before parts that should be aligned. For example,

$$f(x) = \begin{cases}
f(x-1) + f(x-2), & \text{ if } x > 2 \\
1, & \text{ if } x = 1 \text{ or } x = 2
\end{cases}$$

produces

$$f(x) = \begin{cases} f(x-1) + f(x-2), & \text{ if } x > 2 \\ 1, & \text{ if } x = 1 \text{ or } x = 2 \end{cases}$$

\$\endgroup\$

You must log in to answer this question.

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