Help:Comparison between ParserFunctions syntax and TeX syntax

From Meta, a Wikimedia project coordination wiki

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:() the power in the example can now be written 2^{{()|{{{1}}}|10}}:


{{#expr:2^{{()|c|10}}}} gives 1024

{{#tag:math|2^{{()|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.

Note that since the formula is in a separate template, changes cannot be fully previewed; at most they can be previewed for one value of its parameter, by making that the default value.


Links to other help pages

Help contents
Meta · Wikinews · Wikipedia · Wikiquote · Wiktionary · Commons: · Wikidata · MediaWiki · Wikibooks · Wikisource · MediaWiki: Manual · Google
Versions of this help page (for other languages see further)
What links here on Meta or from Meta · Wikipedia · MediaWiki
Reading
Go · Search · Namespace · Page name · Section · Backlinks · Redirect · Category · Image page · Special pages · Printable version
Tracking changes
Recent changes (enhanced) | Related changes · Watching pages · Diff · Page history · Edit summary · User contributions · Minor edit · Patrolled edit
Logging in and preferences
Logging in · Preferences
Editing
Starting a new page · Advanced editing · Editing FAQ · Export · Import · Shortcuts · Edit conflict · Page size
Referencing
Links · URL · Piped links · Interwiki linking · Footnotes
Style and formatting
Wikitext examples · CSS · Reference card · HTML in wikitext · Formula · Lists · Table · Sorting · Colors · Images and file uploads
Fixing mistakes
Show preview · Reverting edits
Advanced functioning
Expansion · Template · Advanced templates · Parser function · Parameter default · Magic words · System message · Substitution · Array · Calculation · Transclusion
Others
Special characters · Renaming (moving) a page · Preparing a page for translation · Talk page · Signatures · Sandbox · Legal issues for editors
Other languages: