From MediaWiki.org
Jump to: navigation, search

Other languages:
English • ‎日本語 • ‎polski • ‎português do Brasil

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.

Installing texvc[edit]

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

About texvc[edit]

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]

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 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.:

wget https://extdist.wmflabs.org/dist/extensions/Math-REL1_23-d0e998f.tar.gz
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]

Compile texvc and texvccheck:

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

On a (virtual) private server[edit]

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)


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]

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 'http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML'
* 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 = 'http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML';
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).

Server-side rendering with Mathoid[edit]

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 'mathml'; is added to the $wgMathValidModes array;
$wgMathValidModes[] = 'mathml';

// Set Mathoid as default rendering option;
$wgDefaultUserOptions['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]

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[] = 'latexml'; // adding LaTeXML as rendering option
// Set LaTeXML as default rendering option;
$wgDefaultUserOptions['math'] = 'latexml';
// Specify the path to your LaTeXML instance that converts the \TeX commands to MathML (optional)
$wgLaTeXMLUrl = 'http://example.com/';

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]

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; see above for how to do that.

List of all configuration settings[edit]

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 The URL path of the math directory. Defaults to "{$wgUploadPathManual:$wgUploadPath}/math".
$wgMathDirectoryManual:$wgMathDirectory false The filesystem path of the math directory. Defaults to "{$wgUploadDirectoryManual:$wgUploadDirectory}/math".
$wgUseMathJax false Enables MathJax as rendering option.
$wgMathJaxUrl '$IP/extensions/MathJax/MathJax.js?config=TeX-AMS-MML_HTMLorMML' The url to MathJax if not using the default default parser at http://cdn.mathjax.org/.
$wgMathValidModes array( 'png', 'source' ) Defines the mode allowed on the server.
$wgMathMathMLUrl 'http://mathoid.testme.wmflabs.org' The url of the mathoid server.
$wgMathMathMLTimeout 20 The timeout for the HTTP-Request sent to the MathML to render an equation, in seconds.
$wgMathLaTeXMLUrlManual:$wgMathLaTeXMLUrl 'http://gw125.iu.xsede.org:8888' 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 ( 'http://latexml.example.com/convert', 'http://latexml2.example.com/convert');

$wgMathLaTeXMLTimeoutManual:$wgMathLaTeXMLTimeout 240 The timeout for the HTTP-Request sent to the LaTeXML to render an equation, in seconds.
	'format' => 'xhtml',
	'whatsin' => 'math',
	'whatsout' => 'math',
	'preload' => array( 
		'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

CSS for the MathML with SVG fallback mode[edit]

By default, MathML is only exposed to accessibility tools. However, you can force visual rendering with the following style:

 .mwe-math-mathml-a11y {
     clip: auto !important;
     overflow: visible !important;
     position: static !important;
     width: auto !important;
     height: auto !important;
     opacity: 1 !important;
 .mwe-math-fallback-image-inline, .mwe-math-fallback-image-display {
     display: none !important;

You can then easily change the style of MathML formulas e.g. to set the math font:

 .mw-body, mtext {
     font-family: Latin Modern Roman;
 math {
     font-family: Latin Modern Math;

Note Note: .mw-body, is added to get the same font appearance for the entire article text. If you do not want to change your main font, then leave this out. Do not forget to remove the comma, as well!