Difference between revisions of "Module:Math templates"

From Feed The Beast Wiki
Jump to: navigation, search
m (Testing)
(Underp)
Line 24: Line 24:
 
str = str .. " display=\"" .. f.args["display"] .. "\""
 
str = str .. " display=\"" .. f.args["display"] .. "\""
 
end
 
end
str = str .. ")" .. str:gsub('($%b{})', replace) .. "(/math)"
+
str = str .. ")" .. f.args[1]:gsub('($%b{})', replace) .. "(/math)"
 
 
 
return f:preprocess(str)
 
return f:preprocess(str)

Revision as of 04:52, 23 July 2016

{{#invoke:Math templates|subst|math|arg1|arg2|...}}

Prints math inside math tags, with the arguments substituted in. Use ${n} to invoke substitution of parameter n, drawn from the arguments to #invoke.

For instance,

{{#invoke:Math templates|subst|${2} + ${3} = ${4}|a|b|c}}

expands to

<math>a + b = c</math>

display is special: it becomes the same-named attribute of the math tags.


local p = {}

function p.ROCSpeed(f)
    return f:preprocess("<math display=\"inline\">Time = " .. f.args[1] .. " - ".. f.args[2] .. " \\times \\log_{2}(x+1)</math>")
end

function p.subst(f)
    local base = 0
    local fp = f:getParent()
    if fp then
        base = 1
    else
        fp = f
    end

    local function replace(marker)
        local name = marker:sub(3, -2)
        name = (tonumber(name) + base) or name
        return fp.args[name] or marker
    end

    local str = "(math"
    if f.args["display"] then
        str = str .. " display=\"" .. f.args["display"] .. "\""
    end
    str = str .. ")" .. f.args[1]:gsub('($%b{})', replace) .. "(/math)"
    
    return f:preprocess(str)
end

return p