I am setting up a MediaWiki instance that will in particular contain mathematical material. I want to write a template for theorems, with the two following properties:
- The theorems are automatically numbered. This numbering should be dynamic: if an edition inserts a theorem between Theorems 1 and 2, the latter should become Theorem 3.
- The theorems can be referred to: I want a piece of code that produces "Theorem 2", given an identifier of the second theorem of the page. (Which means that the template for theorems should be given this identifier as a parameter.)
Basically, I want an equivalent of LaTeX’s theorem
environment and of its ref
command. I’m asking for a template, but mere templates may not be powerful enough, in which case any alternative is appreciated.
Do you have any hints of what is the best way to achieve this?
2
Answers
If you want to number entities in a wikipage (other than automatic numbering of headings for users, or
<ol>
tag generated by#
wiki markup and converted to numbered list by client browser), you ought to provide that number as a parametre of your template for theorem definiton — just as of the template that generates a wikilink to it.You cannot get stable automatic numbering of entities on a wikipage, because the order, in which different parts of a wikipage are parsed, is not guaranteed anymore.
This is achievable, you have to do it by combining MediaWiki templates and the ‘#invoke’ function, allowing you to use Lua scripts to generate the dynamic content.
Here are the steps.
1. create a Lua module
create a page in your Mediawiki instance, e.g ‘Module:TheoremCounter’, with the following Lua code:
the code uses the ‘mw.loadData’ and ‘mw.saveData’ functions to store and retrieve a counter value. The ‘increment’ function increments the counter and returns the new one.
2. create a template for theorems
create a template like his ‘Template:Theorem’, with the below code
The template uses the ‘#invoke’ function to call the ‘increment’ and ‘getLabel’ function from the Lua module.
3. Template referencing theorems
create template for reference ‘Template:RefTheorem’
Usage
To use the templates, it’s very simple, just create a new page and add the following code.
and dynamic label to follow the code
The above example I created only for the simple incrementing counter, which may not be suitable for all cases. But it’s a good start.