12

I know that some websites use services like MathJax for displaying math that look like LaTeX but are not really. What's the case on the Wikipedia?

  • Do they use actual LaTeX or something else?
  • Can I just copy math code from Wikipedia to a LaTeX document, or vice versa, and expect it to work?
schtandard
  • 14,892
  • Welcome! =) Why is there an é in your spelling? Are you talking about French Wikipedia perhaps (which is of course Wikipédia)? In any case, I think Wikipedia (across all of its different language versions) does use LaTeX commands for a lot of its maths but I believe it's usually converted to an image behind the scenes for display, assuming the correct wikipedia-internal markdown is used. But Wikipedia is not really on-topic for this site, and nor really are the differences in how computers and smartphones render webpages, even if LaTeX commands are part of the issue – Au101 May 02 '17 at 03:35
  • 1
    When your browser (either on your computer or on your phone) cannot load an image (because of issues either on the network or at the server), it often displays the alt-text for the image. When Wikipedia generates images for math markup, it uses the original source as alt-text. – ShreevatsaR May 02 '17 at 03:37
  • 1
    Doesn't it use MathJax? Or MathML? I doubt it is using LaTeX - it would be massive overkill. – cfr May 02 '17 at 03:49
  • @cfr not sure, but it's not the usual MathJax output I'm familiar with, but that don't necessarily mean much :) – Au101 May 02 '17 at 03:53
  • 7
    @cfr It doesn't use MathJax; they had it for a while but turned it off. The full syntax is described here (current version). The implementation uses texvc which wraps actual latex + dvipng. So yes it's using a real LaTeX engine. :-) – ShreevatsaR May 02 '17 at 04:52
  • @ShreevatsaR Wow. That seems overkill, but I suppose they must have found a need for it if they've replaced MathJax with it. Thanks for the info. – cfr May 03 '17 at 00:17
  • 1
    @cfr Oh actually this latex way is the old one, from before MathJax existed. (Has been around on Wikipedia for over a decade now.) They had Mathjax as an additional option for registered users from 2012–2015, but turned it off for utterly stupid reasons. (Long story.) It's not that they need true LaTeX; it's just that a decade ago there was no better way of rendering equations. – ShreevatsaR May 03 '17 at 02:17

1 Answers1

25

Wikipedia indeed uses LaTeX, indirectly.

The Wikipedia editors (users who write/edit articles on Wikipedia) enter text like <math>\sqrt{1-e^2}</math> into the source code of the Wikipedia page, and save the page. Of course what is supported is not arbitrary LaTeX, but a pretty extensive syntax suitable for math formulas, which is documented on this page.

Then when the page is viewed, the software (Mediawiki) uses texvc which is used to generate the images. This is a wrapper around an actual latex binary: it verifies that the input fits some restricted LaTeX syntax (documented above), creates a latex file containing that input, runs latex on it, and uses dvipng to generate an image. (This is not done every time a viewer views the page, but is cached on the server after the first time: which is probably when whoever was writing the page looked at the preview.) When the Wikipedia page is viewed, each such image is placed on the page, with the alt-tag being the original LaTeX-style source. For example, the HTML source may say:

<img
   src="https://wikimedia.org/api/rest_v1/media/math/render/svg/1a1835e62332fad1e2fa7f2c6e34972788e59cb3" 
   class="mwe-math-fallback-image-inline" 
   alt="{\sqrt {1-e^{2}}}\!">

If you're curious, you can directly visit that url (https://wikimedia.org/api/rest_v1/media/math/render/svg/1a1835e62332fad1e2fa7f2c6e34972788e59cb3) and view the generated image.

Now when you try to view the page, if there are network issues, or if the Wikipedia servers are slow, your browser may not be able to load the image. When this happens, your browser (whether on your phone or your computer) will probably display the alt-text. In the above case, in place of the image you will see {\sqrt {1-e^{2}}}\!. If you reload the page on your browser, the "problem" may fix itself, and you may see the images again.


One way that all this is useful to us TeX/LaTeX users is that if we're not sure how to enter some formula into TeX, as a shortcut we could visit a relevant Wikipedia page and see how the Wikipedia editors entered it. For example if you visit the page for quadratic formula and copy text around the image, your browser will copy

{\displaystyle x={\frac {-b\pm {\sqrt {b^{2}-4ac}}}{2a}}.}

Of course though useful as a reminder sometimes, this is not something that can be highly recommended: random Wikipedia editors may not always follow best practices, and they may need hacks for their restrictive syntax that do not apply when you're using LaTeX "for real".

ShreevatsaR
  • 45,428
  • 10
  • 117
  • 149
  • You can also get the alt text/LaTeX code from a right click in most browsers. I think there's also a Wikipedia option to make it easier to grab if you're logged in. Noting the warning in this answer (which mainly applies to complex constructions), copy-pasting from Wikipedia can be very useful for unusual symbols or notations, or to make a personal formula book. – Chris H May 02 '17 at 11:01
  • @ChrisH Which Wikipedia option does that? – Santropedro Jul 07 '19 at 20:24