Extension:Math

From MediaWiki.org
Jump to: navigation, search
Translate this page; This page contains changes which are not marked for translation.

Other languages:
Deutsch • ‎English • ‎español • ‎français • ‎italiano • ‎日本語 • ‎polski • ‎português • ‎português do Brasil • ‎中文
MediaWiki extensions manual
Crystal Clear action run.png
Math

Release status: stable

Implementation Tag
Description Allows to render mathematical formulas
Author(s) Tomasz Wegrzanowski, Brion Vibber and others
Latest version 1.2.0 (2014-03-28)
Latest preview version 2.0
MediaWiki 1.19+
Database changes Yes
Tables math
mathoid
mathlatexml
License GNU General Public License 2.0
Download
Parameters
  • $wgUseTeX
  • $wgTexvc
  • $wgTexvcBackgroundColor
  • $wgMathCheckFiles
  • $wgMathPath
  • $wgMathDirectory
  • $wgMathFileBackend
  • $wgUseMathJax
Tags
<math>
Hooks used
ParserFirstCallInit

GetPreferences
LoadExtensionSchemaUpdates
ParserTestTables
ParserTestParser
UnitTestsList

Translate the Math extension if it is available at translatewiki.net

Check usage and version matrix; code metrics
Bugs: list open list all report

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 users web browser, is slow. Server-side rendering can be done at on the wiki's host server or using a public rendering host, e.g. latexml.mathweb.org. 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)
with command-line access to the server
  • server-side rendering with LaTeX (MW_MATH_PNG)
  • 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)

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]

  • If using Vagrant, install with vagrant enable-role math && vagrant provision
Manual Installation
  • Download and extract the file(s) in a directory called Math in your extensions/ folder. If you're a developer and this extension is in a Git repository, then instead you should clone the repository using:
git clone https://gerrit.wikimedia.org/r/p/mediawiki/extensions/Math.git
  • 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.
  • Done! Navigate to "Special:Version" on your wiki to verify that the extension is successfully installed.

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 no shell access[edit | edit source]

Use MathJax.

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

wget https://extdist.wmflabs.org/dist/Math-REL1_23-d0e998f.tar.gz
tar zxvf Math-REL1_23-d0e998f.tar.gz
rm Math-REL1_23-d0e998f.tar.gz
cd Math
make
rm *.o 
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
make

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]

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

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 (Math 1.1 // MW 1.19 - 1.22)
$wgUseMathJax = true; // enabeling 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 (Math 1.2 // MW 1.23+)
$wgUseMathJax = true; // enabeling MathJax as rendering option
$wgDefaultUserOptions['mathJax'] = true; // setting MathJax as default rendering option (optional)
$wgMathDisableTexFilter = true; // or compile "texvccheck"

See also How to modify the MathJax contextual menu.

To install MathJax on the same server as the wiki, download and unpack the latest distribution into $IP/extensions/MathJax. Add to LocalSittings.php the line

$wgMathJaxUrl = '$IP/extensions/MathJax/MathJax.js?config=TeX-AMS-MML_HTMLorMML';

MathJax localization[edit | edit source]

MathJax is an upstream project, but its user interface is translated at translatewiki.net, 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 Math 1.2 // 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 (Math 1.2 // 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 = '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 | edit source]

To use your local webserver (default in Math 1.0, 1.1, is now deprecated) 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
  2. 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
make

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
  2. 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
$wgUseTeX true Backward compatibility global; no longer does anything.
$wgTexvc dirname( __FILE__ ) . '/math/texvc' Location of the texvc binary
$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.
$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.

$wgMathPath false math>{$wgUploadPath}/math</>".
$wgMathDirectory false math>{$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( MW_MATH_PNG, MW_MATH_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.
$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');

$wgMathLaTeXMLTimeout 240 The timeout for the HTTP-Request sent to the LaTeXML to render an equation, in seconds.
$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.
$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.
$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 bugzilla. 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.

History[edit | edit source]

The Math extension was a part of the core MediaWiki software until MediaWiki 1.18. See the related bug #14202 on Bugzilla.

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)".

See also[edit | edit source]

External links[edit | edit source]

Wiki's 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).