Manual:Converting between ParserFunctions syntax and TeX syntax

It would be convenient to be able to use the same wikitext in evaluating a numerical expression with ParserFunctions and as TeX code for nicely displaying it. However, there are several issues.

Same code is possible, but display could be better

edit

Example: "{{#expr:1+2e11*3/(4+5^6)}}" → "38390173.115938" [1]

"{{#tag:math|1+2e11*3/(4+5^6)}}" → " " [2]

In this example the same wikitext is suitable for ParserFunctions and for TeX. Advantages of using TeX for the display, over displaying the raw wikitext, include the nice display of a power with a one-character exponent. An advantage of the wikitext being suitable for TeX is also the possibility to incorporate the wikitext in a larger TeX string, with also parts which no not require automatic evaluation. However, preferable in displaying the expression might additionally be:

  • normal power notation instead of e-notation
  • × instead of *
  • division with a horizontal division bar

Same code is not directly possible

edit

In addition to incompatibility with postfix function calls as well as nested template calls, it is not directly possible to use the same expression for a power with a multi-character exponent, because TeX requires braces, while ParserFunctions does not allow them:

"{{#expr:2^10}}" → "1024" [3]

"{{#tag:math|2^10}}" → " " [4]

"{{#expr:2^(10)}}" → "1024" [5]

"{{#tag:math|2^(10)}}" → " " [6]

"{{#expr:2^{10}}" → "Expression error: Unrecognized punctuation character "{"." [7]}

"{{#tag:math|2^{10}}}" → " " [8]

Remedy

edit

One can put the expression in a template like Template:Example expression for common use by ParserFunctions and TeX, with a parameter that is given the value "c" for a calculation and "d" for display. Using Template:TeX grouping operator the power in the example can now be written 2^{{()|{{{1}}}|10}}:


{{#expr:2^{{TeX grouping operator|c|10}}}} gives 1024

{{#tag:math|2^{{TeX grouping operator|d|10}}}} gives  


Similarly we can use Template:Frac to produce either a division in ParserFunctions syntax, or a division with a horizontal bar in TeX syntax. Thus:

{{#expr:{{Example expression for common use by ParserFunctions and TeX|c}}}} gives 9.8134406521937

{{#tag:math|{{Example expression for common use by ParserFunctions and TeX|d}}}} gives  

Using Template:Tex number, which takes the output of #expr and converts it to a TeX string, we can create TeX output including the result of the computation:

{{#tag:math|g=\frac{GM}{R^2}={{Example expression for common use by ParserFunctions and TeX|d}}={{Tex number|{{Example expression for common use by ParserFunctions and TeX|c}}}} m/s^2}} gives:

 

The numeric expression can be changed at one place, in its template, to produce new TeX output with automatically updated numeric result.

Another example is Template:Example expression for common use by ParserFunctions and TeX: escape velocity; this time parameter {{{1}}} can also be "f" for formula, for this value the template produces the TeX code for the formula without numbers substituted in it. Also, the template uses Template:Sqrt. Thus:

{{#tag:math|v_e = {{Example expression for common use by ParserFunctions and TeX: escape velocity|f}}={{Example expression for common use by ParserFunctions and TeX: escape velocity|d}}={{Tex number|{{Example expression for common use by ParserFunctions and TeX: escape velocity|c}}}} m/s}} gives:

 

Now the formula can be changed in its template, to produce new TeX output for both the formula and the computation, and automatically updated numeric result.