skip to Main Content

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?

2

Answers


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

    Login or Signup to reply.
  2. 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:

    local theoremCounter = {}
    
    function theoremCounter.increment()
        local count = tonumber(mw.loadData('TheoremCounter', 'count'))
        if count == nil then
             count = 0
        end
        count = count + 1
        mw.saveData('TheoremCounter', 'count', tostring(count))
        return count
    end
    
    function theoremCounter.getLabel(id)
        local count = tonumber(mw.loadData('TheoremCounter', 'count))
        local label = 'Theorem ' .. tostring(count)
        if id ~= nil then
            label = label .. ' (' .. id .. ')'
        end
        return label
    end
    
    return theoremCounter
     
    

    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

    {{#invoke:TheoremCounter|increment}}
    <div class="theorem">
       <span class=theorem-label">{{#invoke:TheoremCounter|getLabel|{{{id}}}</span>
    {{{content}}}
    </div>
    

    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’

    {{#invoke:TheoremCounter|getLable|{{{id}}}}}
    

    Usage
    To use the templates, it’s very simple, just create a new page and add the following code.

    {{Theorem|id=MyTheorem|content=This is my theorem.}}
    

    and dynamic label to follow the code

    {{RefTheorem|id=MyTheorem}}
    

    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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search