Phabricator project: #Math


(Redirected from Manual:Enable TeX)
Jump to: navigation, search

Other languages:
català • ‎Deutsch • ‎English • ‎español • ‎suomi • ‎français • ‎italiano • ‎日本語 • ‎한국어 • ‎polski • ‎português • ‎português do Brasil • ‎sicilianu • ‎Tiếng Việt • ‎中文
MediaWiki extensions manual
Crystal Clear action run.png

Release status: stable

Implementation Tag
Description Allows to render mathematical formulas
Author(s) Tomasz Wegrzanowski, Brion Vibber and others
Latest version continuous updates
MediaWiki 1.19+
Database changes Yes
Tables math
License GNU General Public License 2.0
  • $wgUseTeX
  • $wgTexvc
  • $wgTexvcBackgroundColor
  • $wgMathCheckFiles
  • $wgMathPath
  • $wgMathDirectory
  • $wgMathFileBackend
Hooks used


Translate the Math extension if it is available at

Check usage and version matrix; code metrics


Open tasks · Report a bug

The Math extension provides support for rendering mathematical formulas on-wiki via texvc and other backends. An overview of what can currently be done with this extension is found at the English Wikipedia's documentation.

Math output modes[edit | edit source]

The choice of the rendering mode heavily depends on the amount of math the wiki uses as well as the possibility to install required prerequisites and dependencies.

Client-side rendering, e.g. via JavaScript in the user's web browser, is slow.

Server-side rendering can be done on the wiki's host server or using a public rendering host, e.g.

There are the following possible configuration options:

without command-line access to the server
  • no rendering (MW_MATH_SOURCE)
  • client-side rendering with MathJax (MW_MATH_MATHJAX)
  • server-side rendering with Mathoid (MW_MATH_MATHML), which is the rendering mode used on Wikimedia projects in future.
  • server-side rendering with LaTeXML (MW_MATH_LATEXML)
with command-line access to the server
  • server-side rendering with LaTeX (MW_MATH_PNG)

If multiple modes are enabled, logged in users can set a personal preference in the appearance pane of their user preferences page.

Installation[edit | edit source]

Manual installation
  • Download and place the file(s) in a directory called Math in your extensions/ folder.
  • Add the following code at the bottom of your LocalSettings.php:
require_once "$IP/extensions/Math/Math.php";
// See below for installation requirements and configuration settings:
  • Run the update script which will automatically create the necessary database tables that this extension needs.
  • To see math beyond a plain text output, follow instructions below for enabling various math output modes.
  • YesY Done - Navigate to Special:Version on your wiki to verify that the extension is successfully installed.

On a shared host with no shell access[edit | edit source]

Use MathJax or MathML (with SVG fallback).

Additional steps[edit | edit source]

The following steps are required for server-side LaTeX to PNG conversion.

A quick note about texvc[edit | edit source]

The TeX validator and converter texvc takes formulae and produces formatted output in HTML, MathML, and PNG images. Input data is parsed and scrutinized for safety.

On a shared host with a limited shell access[edit | edit source]

On a shared host with limited command line access, (e.g. under jailshell) compiling with all the dependencies won't be possible. As a workaround compiling texvc will have to be done offline. Use uname -a to to identify the OS of the shared host, and install the same offline. This can be done via a USB boot drive, live CD, virtual machine on a desktop PC or whichever you prefer. Then for example in Ubuntu, all dependencies can be installed using:

sudo apt-get install build-essential dvipng ocaml texlive-fonts-recommended texlive-lang-greek texlive-latex-recommended

Get the Math extension and make texvc etc.:

tar zxvf Math-REL1_23-d0e998f.tar.gz
rm Math-REL1_23-d0e998f.tar.gz
cd Math
rm math/*.o
cd ..
tar -pczf texvc-files.tar.gz Math/ # archive the files for easy upload

Upload the texvc-files.tar.gz archive to the host and extract the required files into the $IP/extensions/Math folder.

To test texvc is working (see texvc usage), e.g.:

texvc /home/wiki/tmp /home/wiki/math "y=x+2" iso-8859-1 "rgb 1.0 1.0 1.0"

On a host with full shell access[edit | edit source]

Compile texvc and texvccheck:

cd $IP/extensions/Math/
# install the dependencies listed in the README file e.g.:
sudo apt-get install build-essential ocaml

On a (virtual) private server[edit | edit source]

In some Linux distributions texvc and texvccheck come already built in the MediaWiki Math packages.

See Comparison of extensions in distributions.

For example in Debian or Ubuntu to install it and any other packages on which it depends:

sudo apt-get install mediawiki-math

(MediaWiki, Math, texvc and texvccheck ) or

sudo apt-get install mediawiki-math-texvc

(just texvc)

Configuration[edit | edit source]

Please make sure to put the configuration after require_once "$IP/extensions/Math/Math.php";, otherwise the MW_MATH_XXX constants will be undefined.

Client-side rendering with MathJax[edit | edit source]

Starting with MediaWiki 1.26 (REL1_26) MathJax is no longer supported as a rendering service. See Task 99369 (Gerrit:212270) for further information. If it's required (e.g. if you are on shared hosting) an older version of this extension will have to be installed. Alternately use extension SimpleMathJax.

From 1.19/1.20, the Math extension supports rendering via MathJax, a client-side JavaScript library for parsing latex or mathml code and producing in-browser representation using the browser's native support for HTML+CSS, or MathML, or SVG.

This can render math expressions more sharply (using subpixel antialiasing) and larger (scaling up for zoomed view, printing, or high-resolution screens).

Set $wgUseMathJax to enable MathJax rendering mode; it is still somewhat experimental. Users will have a check box on their preferences that will enable MathJax, or set MathJax to the default mode as shown below.

Settings (MW 1.19 - 1.22)
$wgUseMathJax = true; // enabling MathJax as rendering option
$wgDefaultUserOptions['math'] = MW_MATH_MATHJAX; // setting MathJax as default rendering option (optional)
* The default parser for MathJax is ''
* If you don't want to or can't use the default and your run your own locally
* replace the below url with something like:
* $wgMathJaxUrl = '/mathjax/MathJax.js?config=TeX-AMS-MML_HTMLorMML';
$wgMathJaxUrl = '';
Settings (MW 1.23 - 1.25)
$wgMathValidModes[] = MW_MATH_MATHJAX; // Define MathJax as one of the valid math rendering modes
$wgUseMathJax = true; // Enable MathJax as a math rendering option for users to pick
$wgDefaultUserOptions['math'] = MW_MATH_MATHJAX; // Set MathJax as the default rendering option for all users (optional)
$wgDefaultUserOptions['mathJax'] = true; // Enable the MathJax preference option by default (optional)
$wgMathDisableTexFilter = true; // or compile "texvccheck"

See also How to modify the MathJax contextual menu.

Forcing MathJax as the only rendering option (MW 1.23+)

If you have a wiki where MathJax is the only available rendering mode, and wish to force it to be enabled, add the following to LocalSettings.php instead of the above:

$wgMathValidModes = array(MW_MATH_MATHJAX); // Define MathJax as the only valid math rendering mode
$wgUseMathJax = true; // Enable MathJax
$wgDefaultUserOptions['math'] = MW_MATH_MATHJAX; // Set MathJax as the default rendering option
$wgDefaultUserOptions['mathJax'] = true; // Enable the MathJax checkbox option
$wgHiddenPrefs[] = 'math'; // Hide math preference
$wgHiddenPrefs[] = 'mathJax'; // Hide MathJax checkbox
$wgMathDisableTexFilter = true; // or compile "texvccheck"

This will switch on MathJax by default for all anonymous users and those without preferences saved for the Math extension. Note that if registered users had an alternate rendering mode selected, it will be necessary for them to open Preferences > Appearance and click Save for the new forced setting to take effect. Articles should advise users about the requirement for JavaScript to be enabled in order to see formulae (e.g. through a template for articles including TeX formulae).

MathJax localization[edit | edit source]

MathJax is an upstream project, but its user interface is translated at, the same website where MediaWiki and its extension are translated. Translations made there are exported to the MathJax source code, and they will be used in the MediaWiki extension, too. For more information about translating MathJax's menus see translatewiki:Translating:MathJax.

Server-side rendering with Mathoid[edit | edit source]

Beginning from MW 1.23+ you can use a Mathoid server that uses MathJax to convert texvc input on the server side to MathML+SVG rendering. Mathoid is the rendering mode that is going to be used on future Wikipedia. For that the following settings are recommended:

// ensure MW_MATH_MATHML; is added to the $wgMathValidModes array;
$wgMathValidModes[] = MW_MATH_MATHML;

// Set Mathoid as default rendering option;
$wgDefaultUserOptions['math'] = MW_MATH_MATHML;

Check out Mathoid to find instructions how to run your own Mathoid server. After enabling the MathML rendering mode you have to run the database update script again to create the required table.

Server-side rendering with LaTeXML[edit | edit source]

A convenient option to render LaTeX is to use the LaTeXML web service. This service converts LaTeX to high quality MathML.

Settings (MW 1.22) most or all of the 1.23+ settings also work
// Enable LaTeXML as rendering option
$wgUseLaTeXML = true; // this became an unused setting around 1.23
Settings (MW 1.23+)
$wgMathValidModes[] = MW_MATH_LATEXML; // adding LaTeXML as rendering option
// Set LaTeXML as default rendering option;
$wgDefaultUserOptions['math'] = MW_MATH_LATEXML;
// Specify the path to your LaTeXML instance that converts the \TeX commands to MathML (optional)
$wgLaTeXMLUrl = '';

A short guide to install LaTeXML on your own server can be found here: Install LaTeXML

After enabling the LaTeXML rendering mode you have to run the database update script again to create the required table.

Server-side rendering with LaTeX[edit | edit source]

To use your local webserver to convert the texvc input to PNG images via a local LaTeX installation and OCAML based translation from texvc to LaTeX you have to install texvc.

Installation of texvc[edit | edit source]

  1. If MediaWiki does not have write permissions to $wgUploadDirectory, see config items below $wgMathPath, $wgMathDirectory and $wgMathPath you will have to grant them with e.g. *)
    sudo chown -R www-data:www-data
  1. Compile texvc
Install the required prerequisites and dependencies
sudo apt-get install build-essential texlive texlive-bibtex-extra texlive-font-utils texlive-fonts-extra texlive-latex-extra texlive-math-extra texlive-pictures texlive-pstricks texlive-publishers dvipng gsfonts make ocaml ploticus
Switch the directory
cd install/path/extensions/Math/math
Compile texvc

Note Note: To make the rendering of mathematical formulas possible with the Collection extension texvc must be installed in PATH (see the alternative installation methods just below)

Alternatively install the mediawiki math packages
  1. sudo apt-get install mediawiki-math
    (MediaWiki, Math, texvc and texvccheck) or
  1. sudo apt-get install mediawiki-math-texvc
    (just texvc)

List of all configuration settings[edit | edit source]

The following variables can be defined in LocalSettings.php after calling require_once "$IP/extensions/Math/Math.php";:

On the individual pages for these settings, it says deprecated in 1.18, since this extension was split from core mediawiki in that version. If you've installed the extension, these settings apply regardless
Setting name Default value Description
$wgUseTeXManual:$wgUseTeX true Backward compatibility global; no longer does anything.
$wgTexvcManual:$wgTexvc dirname( __FILE__ ) . '/math/texvc' Location of the texvc binary
$wgTexvcBackgroundColorManual:$wgTexvcBackgroundColor 'transparent' Texvc background color; use LaTeX color format as used in \special function. For transparent background use value 'Transparent' for alpha transparency or 'transparent' for binary transparency.
$wgMathCheckFilesManual:$wgMathCheckFiles true Normally when generating math images, we double-check that the directories we want to write to exist, and that files that have been generated still exist when we need to bring them up again.

This lets us give useful error messages in case of permission problems, and automatically rebuild images that have been lost.

On a big site with heavy NFS traffic this can be slow and flaky, so sometimes we want to short-circuit it by setting this to false.

$wgMathPathManual:$wgMathPath false math>{$wgUploadPathManual:$wgUploadPath}/math</>".
$wgMathDirectoryManual:$wgMathDirectory false math>{$wgUploadDirectoryManual:$wgUploadDirectory}/math</>".
$wgUseMathJaxManual:$wgUseMathJax false Enables MathJax as rendering option.
$wgMathJaxUrlManual:$wgMathJaxUrl '$IP/extensions/MathJax/MathJax.js?config=TeX-AMS-MML_HTMLorMML' The url to MathJax if not using the default default parser at
$wgMathValidModesManual:$wgMathValidModes array( MW_MATH_PNG, MW_MATH_SOURCE ) Defines the mode allowed on the server.
$wgMathMathMLUrlManual:$wgMathMathMLUrl '' The url of the mathoid server.
$wgMathMathMLTimeoutManual:$wgMathMathMLTimeout 20 The timeout for the HTTP-Request sent to the MathML to render an equation, in seconds.
$wgMathLaTeXMLUrlManual:$wgMathLaTeXMLUrl '' Use of LaTeXML.

If you want or need to run your own server, follow these installation instructions and override $wgMathLaTeXMLUrl: [1]

If you expect heavy load you can specify multiple servers. In that case one server is randomly chosen for each rendering process. Specify the list of servers in an array e.g $wgMathLaTeXMLUrl = array ( '', '');

$wgMathLaTeXMLTimeoutManual:$wgMathLaTeXMLTimeout 240 The timeout for the HTTP-Request sent to the LaTeXML to render an equation, in seconds.
$wgMathDefaultLaTeXMLSettingManual:$wgMathDefaultLaTeXMLSetting array(

'format' => 'xhtml', 'whatsin' => 'math', 'whatsout' => 'math', 'pmml', 'cmml', 'nodefaultresources', 'preload' => array( 'LaTeX.pool', 'article.cls', 'amsmath.sty', 'amsthm.sty', 'amstext.sty', 'amssymb.sty', 'eucal.sty', '[dvipsnames]xcolor.sty', 'url.sty', 'hyperref.sty', '[ids]latexml.sty', 'texvc' ), )

Setting for the LaTeXML renderer for details.
$wgMathDisableTexFilterManual:$wgMathDisableTexFilter false Option to disable the tex filter. If set to true any LaTeX espression is parsed this can be a potential security risk. If set to false only a subset of the TeX commands is allowed. See the wikipedia page Help:Math for details.
$wgMathEnableExperimentalInputFormatsManual:$wgMathEnableExperimentalInputFormats false Enables experimental MathML and AsciiMath input format support

Purging pages that contain equations[edit | edit source]

Specifying the URL-parameter action=purge does not force the math equation to re-render the math tags. If a complete re-rendering of the math tags is desired the URL-parameter action=purge&mathpurge=true has to be passed. Be aware that this might cause side effects with other pages since the math extension uses the hash of the input TeX-input-string as the caching key. e.g. if you would force the re-rending with mathpurge option for a page containing the equation $E=mc^2$, the new rendering result would be used on all other pages containing $E=mc^2$ after the individual page caches are invalidated.

Error reporting[edit | edit source]

If something is wrong with the math extension you can report that at Phabricator. In addition you should check how your problem relates to the automated unit tests that are generated from the page CoverageTest.

MathJax errors[edit | edit source]

MathJax has more bugs than latex. So please check if it's an MediaWiki Bug or a MathJax problem. If it's a MathJax problem that has been fixed you might consider to port the change to MediaWiki version of MathJax. Therefore a development environment is required. Just cherrypick the change from MathJax and apply it to the modules/MathJax folder in the Math extension. Afterwards you can submit your change for review.

CSS for the MathML with SVG fallback mode[edit | edit source]

 /* By default, MathML is exposed to accessibility tools and displayed in Gecko
    browsers. You can force visual rendering with the following style: */
 .mwe-math-mathml-a11y {
     clip: auto;
     overflow: visible;
     position: static;
     width: auto;
     height: auto;
     opacity: 1;
 .mwe-math-fallback-image-inline, .mwe-math-fallback-image-display {
     display: none !important;

 /* To force the SVG fallback in Gecko browsers, use the following style: */
 span.mwe-math-mathml-inline, div.mwe-math-mathml-display {
     display: none !important;
 span.mwe-math-mathml-inline + .mwe-math-fallback-image-inline {
     display: inline !important;
 div.mwe-math-mathml-display + .mwe-math-fallback-image-display {
     display: block !important;

 /* When MathML is displayed, you can easily style it via CSS (font-family,
    font-size, color etc). Below are some examples of math font families,
    with some suggested text fonts to go with. */

 /* Asana Math (Palatino style) */
 .mw-body, mtext {
     font-family: Palatino Linotype, URW Palladio L, Asana Math;
 math {
     font-family: Asana Math;

 /* Cambria (Microsoft Office style) */
 .mw-body, mtext {
     font-family: Cambria;
 math {
     font-family: Cambria Math;

 /* Latin Modern (Computer Modern style) */
 .mw-body, mtext {
     font-family: Latin Modern Roman;
 math {
     font-family: Latin Modern Math;

 /* STIX/XITS (Times New Roman style) */
 .mw-body, mtext {
     font-family: XITS, STIX;
 math {
     font-family: XITS Math, STIX Math;

 /* TeX Gyre Bonum (Bookman style) */
 .mw-body, mtext {
     font-family: TeX Gyre Bonum;
 math {
     font-family: TeX Gyre Bonum Math;

 /* TeX Gyre Pagella (Palatino style) */
 .mw-body, mtext {
     font-family: TeX Gyre Pagella;
 math {
     font-family: TeX Gyre Pagella Math;

 /* TeX Gyre Schola (Century Schoolbook style) */
 .mw-body, mtext {
     font-family: TeX Gyre Schola;
 math {
     font-family: TeX Gyre Schola Math;

 /* TeX Gyre Termes (Times New Roman style) */
 .mw-body, mtext {
     font-family: TeX Gyre Termes;
 math {
     font-family: TeX Gyre Termes Math;

History[edit | edit source]

The Math extension was a part of the core MediaWiki software until MediaWiki 1.18. See the related bug T16202 on Phabricator.

MW_MATH_MATHML/MW_MATH_LATEXML was implemented to perform MathML generation instead of texvc because texvc is very poor at creating MathML. See the historic option "MathML if possible (experimental)".

Developing[edit | edit source]

A introduction to code review for the Math extension is available from

From version wmf1.25 composer is required. If you can not run composer in your development environment make copy the required modules to your mediawiki/vendor folder. There should be at least mediawiki/vendor/psr/log-folder which enables auto completion for the debug commands in your IDE.

See also[edit | edit source]

External links[edit | edit source]

Wikis rendering formulae with MathJax[edit | edit source]

This lists MediaWiki wiki's using the math extension's MathJax option, (but not those using other MathJax extensions).

  • SklogWiki, thermodynamics and statistical mechanics