擴充功能:數學

This page is a translated version of the page Extension:Math and the translation is 5% complete.
This extension comes with MediaWiki 1.38 and above. Thus you do not have to download it again. However, you still need to follow the other instructions provided.
MediaWiki 擴充功能手冊
Math
發佈狀態: 穩定
Implementation Tag
描述 Allows to render mathematical formulas
作者
  • Moritz Schubotz,
  • Tomasz Wegrzanowski,
  • Brooke Vibber,
  • André Greiner-Petter,
  • and others
最新版本 continuous updates
Compatibility policy Snapshots releases along with MediaWiki. Master is not backward compatible.
MediaWiki >= 1.42.0
Database changes
Tables math
mathoid
mathlatexml
許可 GNU General Public License 2.0 or later
下載
  • $wgMathEnableWikibaseDataType
  • $wgMathDefaultLaTeXMLSetting
  • $wgMathUseInternalRestbasePath
  • $wgMathMathMLUrl
  • $wgMathFullRestbaseURL
  • $wgMathTexVCService
  • $wgMathInternalRestbaseURL
  • $wgMathConcurrentReqs
  • $wgMathEnableFormulaLinks
  • $wgMathValidModes
  • $wgMathEntitySelectorFallbackUrl
  • $wgMathWikibasePropertyIdQuantitySymbol
  • $wgMathDisableTexFilter
  • $wgMathMathMLTimeout
  • $wgMathWikibasePropertyIdSymbolRepresents
  • $wgMathWikibasePropertyIdHasPart
  • $wgMathWikibasePropertyIdInDefiningFormula
  • $wgMathWikibasePropertyIdDefiningFormula
  • $wgMathoidCli
  • $wgMathLaTeXMLUrl
  • $wgMathLaTeXMLTimeout
  • $wgMathEnableExperimentalInputFormats
‎<math>
Quarterly downloads 191 (Ranked 37th)
Public wikis using 7,934 (Ranked 18th)
若果有喺translatewiki.net提供嘅話翻譯Math呢個擴充功能
問題 Open tasks · 報告臭蟲

The Math extension provides support for rendering mathematical formulae. An overview of what can currently be done with this extension is available at m:Help:Displaying a formula.

More information about installing and configuring this extension, including for older versions, can be found at Extension:Math/advancedSettings .

睇數學

The type of output depends on the software that you use to read the page. If it can, the Math extension outputs MathML. Otherwise, it outputs images in SVG (converting characters to <path> elements) or PNG. Basic math support works for all browsers. But, for best results, you might need to change your settings:


安裝

  • Download and move the extracted Math folder to your extensions/ directory.
    Developers and code contributors should install the extension from Git instead, using:cd extensions/
    git clone https://gerrit.wikimedia.org/r/mediawiki/extensions/Math
  • Add the following code at the bottom of your LocalSettings.php file:
    wfLoadExtension( 'Math' );
    
  • 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.
  • If you have problems for represent formulas when trying to use RESTBase, you can try using the following patch
  •   Done – Navigate to Special:Version on your wiki to verify that the extension is successfully installed.


Vagrant installation:

  • If using Vagrant , install with vagrant roles enable math --provision

You should additionally go to the page Special:MathStatus to see whether all of the components of the Math extension now work. For any problems you see, the best option is to create a task on https://phabricator.wikimedia.org

Unfortunately, there is no well tested guide on installing Mathoid and RESTBase to work with the Math extension, but there is at least a working draft. Extension:Math/RESTBase please contribute.

Math output modes

The setting $wgMathValidModes holds an array with the names of output modes that can be used for rendering. If multiple modes are enabled, logged-in users can set a personal preference in the appearance pane of their user preferences page.

You can also use the $wgDefaultUserOptions setting to set which of these modes should be the default, e.g.:

$wgDefaultUserOptions['math'] = 'mathml';

Mathoid

Mode: 'mathml'

You can use Mathoid, an application that uses MathJax on the server side to convert texvc input to MathML+SVG rendering. This is the most recommended option; Mathoid is the Math rendering mode used on Wikipedia.

Mathoid as a service

"Mathoid as a service" is the recommended approach for the Math extension, and the default one. If you do not add any additional settings to LocalSettings.php, Math will use the Beta cluster's Mathoid and RESTBase services to do all math formula rendering. The default settings are:

// Set MathML as default rendering option
$wgDefaultUserOptions['math'] = 'mathml';
$wgMathUseInternalRestbasePath = false;
$wgMathFullRestbaseURL = 'https://wikimedia.org/api/rest_';
$wgMathMathMLUrl = 'https://mathoid-beta.wmflabs.org';

You can modify these settings to use different Mathoid and RESTBase services, potentially including your own.

Mathoid's CLI interface

You can instead run Mathoid as a command-line utility on the local server. To use this approach, the following settings are recommended:

// Set MathML as default rendering option
$wgDefaultUserOptions['math'] = 'mathml';
// Please create a config.yaml in advance. Templates are in the Mathoid repository.
$wgMathoidCli = ['/path/to/mathoid/cli.js', '-c', '/path/to/mathoid/config.yaml'];
// Raise MediaWiki's memory limit to 1.2G for mathoid.
$wgMaxShellMemory = 1228800;

If you run MediaWiki in a system containing SELinux and you get the message Mathoid cli '/path/to/mathoid/cli.js' is not executable in the error log, you can run the following to make sure that the Apache server allows to change system limits:

setsebool -P httpd_execmem 1
setsebool -P httpd_setrlimit 1

LaTeX

MediaWiki version:
1.39

Mode: 'png'

Since MediaWiki 1.32, this mode will also require a Mathoid server to work. In older versions, it requires installing texvc and texvccheck, which may require the most setup work.

LaTeXML

Mode: 'latexml'

Uses the LaTeXML utility. The rendering is done via an online service (set with $wgLaTeXMLUrl, which has a default value of https://latexml.formulasearchengine.com/convert).

Native MathML

MediaWiki version:
1.40

Mode: 'native'

This mode will generate MathML from LaTeX via PHP without Mathoid. Note this mode does not have image fallback, so formulas will only be displayed correctly if the browser supports MathML. Firefox and Safari introduced MathML support early (2006 and 2008), but support in Chromium-based browsers is a much more recent addition (Chrome 109, released in 2023).

No rendering

Mode: 'source'

This mode will forward the Latex input without rendering, presenting it in a ‎<span> element, surrounded by $ :formula $.

Make sure to set $wgMathDisableTexFilter to 'always' (see below) if you only want the raw latex formula, else an error will appear due to the failed check for PNG depictions.

List of significant configuration settings

Setting name Default value Description
$wgMathValidModes [ 'source', 'mathml' ] Defines the mode allowed on the server.
$wgMathDisableTexFilter 'never' Option to disable the tex filter. If set to true any LaTeX expression 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. Use "always" to disable this feature.
$wgMathFullRestbaseURL false The math extension gets the default config from the Visual Editor, if available. Details.
$wgMathPreferRestbaseURL true Whether to allow using of internal RESTBase path instead of $wgMathFullRestbaseURL and $wgVisualEditorFullRestbaseURL. Set false if you want to use external RESTBase in any case.

Tracking categories

The extension creates a number of tracking categories to detect errors and deprecated features. The exact name of the categories is determined by pages in the MediaWiki namespace.

MediaWiki page Default Description
MediaWiki:Math-tracking-category-error Category:Pages with math errors Pages where there is a syntax error in the formula causing the rendering to fail
MediaWiki:Math-tracking-category-render-error Category:Pages with math render errors Pages where there has been a temporary error in the rendering pipeline causing the rendering to fail. These normally fix themselves
MediaWiki:Math-tracking-category-texvc-deprecation Category:Pages that use a deprecated format of the math tags Pages which use deprecated texvc syntax like $ % \and \or \part \ang \C \H \bold \Bbb\ \pagecolor, which should be replaced by more standard LaTeX \$ \% \land \lor \partial \angle \Complex \mathbb{H} \mathbf \mathbb (removed) respectively. See Extension:Math/Roadmap .
MediaWiki:Math-tracking-category-mhchem-deprecation Category:Pages that use a deprecated format of the chem tags

Deprecated chemical markup, see Extension:Math/Roadmap#Step 1 Part C: Manual fixing of mhchem syntax.

貼士

  • You can use the Firefox MathML copy addon to copy formulae to other applications such as Microsoft Word (video), or Google docs with the g(Math) plugin (video)
  • You can override the user preferred and default rendermode by adding the forcemathmode attribute to the ‎<math> element. e.g. to force an image being rendered <math forcemathmode="png">...</math>.
  • All images of rendered formula are stored in a cache and not automatically recreated if the page is viewed or edited. To force the re-rendering of all formulas of a page, purge using the ?action=purge&mathpurge=true action. For example the URL https://en.wikipedia.org/w/index.php?title=Integral&action=purge&mathpurge=true will force re-rendering of the w:Integral article. Afterwards you need to bypass your browser cache so that the new created images of the formulas are actually downloaded.

Error reporting

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.

Further reading

See Extension:Math/advancedSettings for old versions and further information.

Subpages

睇埋