phpmediawikiwikimediawiki-templates

Numbered templates (for theorems) in MediaWiki


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:

  1. 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.
  2. 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?


Solution

  • As pointed out by @Alexander Mashin, there is no hope for a solution using MediaWiki's embedded Lua engine. In the end I relied on this answer and did the following.

    First a template for theorems.

    A parameter id is stored in a data-theoremid attribute of the container. As an (unnecessary) enhancement, I did the same for a parameter type allowing for different theorem types: Theorem, Lemma, etc.

    <div class="theorembox {{{type}}}" data-theoremid="{{{id}}}" data-theoremtype="{{{type}}}">
    <div class="theoremhead">
    <span class="theoremtype">{{ucfirst:{{{type}}}}}</span>&nbsp;<span class="theoremcount"></span>
    {{#if:{{{title|}}}|<span class="theoremtitle">({{{title}}})</span>|}}
    </div>
    {{{content}}}
    </div>
    

    The usage is {{Theorem box|id=mytheorem|type=Theorem|title=Fermat's last theorem|content=For all n≥3, ...}}.

    Second a template for references.

    The identifier of the referred theorem is given as {{{1}}}.

    <span class="theoremref" data-referto="{{{1}}}">[[#theorem:{{{1}}}|<span class="theoremtype"></span>&nbsp;<span class="theoremcount"></span>]]</span>
    

    The usage is {{Theorem ref|mytheorem}}.

    Then two JavaScript functions

    The first one looks for theorems and numbers them (storing, for each identifier, the corresponding theorem type and number); the second one looks for reference calls and fills them. I put these functions in MediaWiki:Common.js.

    var theorems = {};
    
    document.querySelectorAll('.theorembox').forEach((el,i) => {
        var id = el.getAttribute('data-theoremid').replace(/\s/g, "_");
        var type = el.getAttribute('data-theoremtype');
        var number = i+1;
        el.setAttribute('id', 'theorem:' + id);
        el.querySelector('.theoremcount').innerHTML = number;
        theorems[id] = {type:type, number:number};
    });
    
    document.querySelectorAll('.theoremref').forEach((el,i) => {
        var id = el.getAttribute('data-referto').replace(/\s/g, "_");
        var href = '#theorem:' + id;
        var type = theorems[id].type;
        var number = theorems[id].number;
        el.querySelector('a').setAttribute('href', href);
        el.querySelector('.theoremtype').innerHTML = type.charAt(0).toUpperCase() + type.slice(1);
        el.querySelector('.theoremcount').innerHTML = number;
    });