24
\$\begingroup\$

Several times I had the need to write mathematical formulas in my answers on CodeReview.SE. Sometimes, this can be simulated by sup and/or sub HTML tags, as I realized when syb0rg edited my answer here. However, sometimes it is not as easy, for example here, where I had to use code block for one formula (to achieve alignment) and LaTeX formulas off-site and attach them as images (for summations).

Formulas make sense in the design of various (for example, numerical) algorithms, as well as in the analysis of the algorithm's complexity.

My suggestion is to include MathJax in CodeReview.SE, as it was included in Math.SE (where it obviously makes even more sense to be included), unless there is some big drawback here that I'm failing to notice.

\$\endgroup\$
  • 5
    \$\begingroup\$ @Malachi Technically, it's not a problem to implement MathJax. It's just a bit of JavaScript code in the pages' headers. Of course, SE may have made some extra adjustments, but these can be copied from Math.SE. The problem that may be introduced is that $ is used in shell programming as well, so some extra editing may be needed in new users' posts (who don't use ticks and/or code blocks). I don't see that as a problem, but I might be wrong. \$\endgroup\$ – Vedran Šego Jan 20 '14 at 12:40
  • \$\begingroup\$ This may be of interest: meta.stackexchange.com/a/4159/199647 It lists a place where you can generate images using MathML that you then hotlink... though since you can't rely on that site always hosting your image it may not be a good idea. \$\endgroup\$ – Daniel Jan 24 '14 at 20:33
  • \$\begingroup\$ @DanielCook I've used this one's "Download Image" (as PNG) and uploaded it to CR.SE. \$\endgroup\$ – Vedran Šego Jan 24 '14 at 21:23
  • 2
    \$\begingroup\$ It would be a bad idea to use $ as a MathJax delimiter on a programming site. If we use \$ or $$, as in Electrical Engineering, then no existing posts would get misinterpreted. In fact, one user tried to write MathJax unsuccessfully. \$\endgroup\$ – 200_success Feb 28 '14 at 9:54
  • 2
    \$\begingroup\$ I doubt we need MathJax for question titles, though. If an option to allow MathJax only on post bodies and not question titles is available, then perhaps it might be possible to avoid loading MathJax support on the front page and other question lists. \$\endgroup\$ – 200_success Apr 1 '14 at 8:22
  • \$\begingroup\$ For the record... disadvantages of MathJax \$\endgroup\$ – 200_success Apr 3 '14 at 20:06
  • \$\begingroup\$ @200_success You're right. I didn't expect such a long list. As for the disadvantages, it is unrealistic to expect something like MathJax to not produce a performance penalty. I support your suggestion to enable it on posts, but not on the front page (even though it might produce some undesirable effects, but nothing that editing wouldn't fix). \$\endgroup\$ – Vedran Šego Apr 3 '14 at 21:52

14 Answers 14

20
\$\begingroup\$

Enough! Enough! If you post any more answers here, we're going to run out of room in the database!

Er, I mean, you've done a good job of presenting evidence, and have convinced us on the Community Team. As of about ten minutes ago, MathJax has been enabled for Code Review. Use it well!

\$\endgroup\$
  • 6
    \$\begingroup\$ MathJax is currently enabled with $ … $ as delimiters. However, that breaks existing posts that contain two dollar signs within the same paragraph, but where the code is not formatted using backticks or a code block. We would like to change it to use \$ … \$ as delimiters, as on Electrical Engineering. Please refrain from using MathJax while we wait for the change. \$\endgroup\$ – 200_success Apr 7 '14 at 19:27
  • 4
    \$\begingroup\$ @200_success Delimiters changed. Enjoy! \$\endgroup\$ – Pops Apr 7 '14 at 19:47
  • \$\begingroup\$ Thank you and everyone who helped this happen. \$\endgroup\$ – Vedran Šego Apr 9 '14 at 8:00
  • \$\begingroup\$ See MathJax feature requires external JavaScript for SE’s editor/preview \$\endgroup\$ – unor Apr 19 '14 at 10:33
14
\$\begingroup\$

Based on Space Exploration SE's similar feature request, the SE devs would want to see a list of questions that can benefit from this feature. We may not use this feature a whole lot, but we at least need several good examples of posts that would benefit from this. Even then, they may not feel that it's a good fit for CR, but that's something they will have to determine based on whatever criteria they use.

\$\endgroup\$
  • 5
    \$\begingroup\$ This. Unless there's a large population of posts that could benefit extensively from it, there's no need to impose the cost on the rest of the posts. A few subscripts don't really necessitate this. \$\endgroup\$ – Shog9 Jan 20 '14 at 20:56
13
\$\begingroup\$

The following sites support MathJax (there may be more I'm unaware of):

If even Biology supports MathJax, then leaving MathJax disabled on Code Review looks more like an oversight than a deliberate rejection. Let's enable it!

(Interestingly, TeX — LaTeX does not support MathJax.)

\$\endgroup\$
13
\$\begingroup\$

This answer to the Knight's Distance required taking a 'snip' screenshot to get the formula in.

Screen-Shot Snip

Screen-shot snip

\$\endgroup\$
12
\$\begingroup\$

In Optimizing unique partitions of integers, I just want to write the quadratic equation nicely. Having to resort to ASCII art to do so is just ridiculously frustrating!

              _________
        -1 ± √ 1 + 8 n
x    = —————————————————
 max          2

Have I mentioned the mad keyboard skills necessary to type symbols ± √ —?

The lazy workaround would be to write

x = (-1 + sqrt(1 + 8 * n)) / 2

but that just doesn't make the answer nearly as easy to understand.

Also, getting the equals signs to line up in

n = 1 + 2 + 3 + … + xmax

    = xmax (xmax + 1) / 2

was no mean feat!

\$\endgroup\$
8
\$\begingroup\$

Here's an answer that would have been a lot easier to write if MathJax had been available:

\$\endgroup\$
8
\$\begingroup\$

This answer could have been a lot less awkward if proper mathematical notation were a reasonable option.

You should find an algorithm finding the root square of 1 module n.

x^2 = 1 mod n

To do that:

  • Factorize n in prime factor. n = p1^e1 * p2^e2 * ... * pk^ek
  • Find the root for all prime xi^2 = 1 mod pi^ei. easy, it is 1 and -1 (or pi - 1)
  • Then use the Chinese theorem to find the solution for x (x = x1 mod x1^e1, x = ..., x = xk mod pk^ek (xi can take 2 value so you will have k^2 answer)

you just need to take the solution which meet your condition

I don't blame the poster for the poor typesetting. It's an unreasonable burden to have to type

_n_ = _p_<sub>1</sub><sup>_e_<sub>1</sub></sup> ⋅ _p_<sub>2</sub><sup>_e_<sub>2</sub></sup> ⋅ … ⋅ _p_<sub>_k_</sub><sup>_e_<sub>_k_</sub></sup>

to obtain

n = p1e1p2e2 ⋅ … ⋅ pkek

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

This answer was a pain to type without MathJax, with all the italics, subscripts, and the Greek alpha character:

St = α Yt-1 + (1 - α) St-1

where

  • α is the decay rate
  • Yt is the value at time t
  • St is the exponential moving average at time t.

That equation was taken from the linked Wikipedia article, so I wanted to copy it faithfully into the answer for discussion.

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

Even using subscripts and superscripts, formulas still look odd and can be hard to read. An example would be from this answer:

Is it reasonable to assume that most numbers will be non-palindromes? If not, I would suggest something like this:

  1. Determine the approximate number of digits in your number a using mpz_sizeinbase() and call this number n.

  2. Compute a / d(n-30), then compute a % d30. Print both in base d and compare. If they are unequal, reject as non-palindrome.

    Computing a / d(n-30) will be fast using mpz_tdiv_q(). Unfortunately, computing d30 is not fast. Perhaps a rough approximation of that would also increase efficiency.

MathJax would make that a lot easier to read IMO.

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

Answers which use Landau-notation benefit from MathJax as well, since it is often necessary to perform algebraic manipulations to reach the final expression for the asymptotic complexity.

For example, when discussing optimal search implementations.

\$\endgroup\$
6
\$\begingroup\$

If MathJax were available, the author of this question might have been able to describe what the code was trying to accomplish:

Also, if a reviewer chooses to overhaul the code, it would also be helpful to write an explanation using MathJax.

\$\endgroup\$
6
\$\begingroup\$

Since we're giving the usage examples to show how much is this needed, here are those linked in the question:

Here, I had to use code block for one formula (to achieve alignment) and LaTeX formulas off-site and attach them as images (for summations).

Here are the formulas:

    sum = (a2 - a1) + (a3 - a1) + (a4 - a1) + ... + (an - a1) +
                    + (a3 - a2) + (a4 - a2) + ... + (an - a2) +
                                + (a4 - a3) + ... + (an - a3) +
                                            ...

\sum_{k=1}^n (k-1)a_k - \sum_{k=1}^n (n-k)a_k = \sum_{k=1}^n (2k-n-1)a_k

\sum_{k=1}^4 (2k-4-1)a_k = -3 \cdot 1 + (-1 \cdot 2) + 1 \cdot 3 + 3 \cdot 4 = -3 - 2 + 3 + 12 = 10.

\$\endgroup\$
5
\$\begingroup\$

In Counting occurrences of values in C Array (Shannon Entropy), the entropy formula was posted as an image.

Entropy

A MathJax expression would have been better.

\$\endgroup\$
5
\$\begingroup\$

In this answer, the author took the effort to write

sqrt(⌈log10(B)⌉*81)

The subscript takes a little bit of effort. The brackets for the ceiling function aren't easy to type at all. Then there's still the sqrt(…) which is impossible to nicely typeset using regular Markdown.

\$\endgroup\$

You must log in to answer this question.

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