# Help:Comparison 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

Example:

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

{{#tag:math|1+2e11*3/(4+5^6)}}${\displaystyle 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
• division with a horizontal division bar

## Same code is not directly possible

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}}${\displaystyle 2^{1}0}$ [4]

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

{{#tag:math|2^(10)}}${\displaystyle 2^{(}10)}$ [6]

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

{{#tag:math|2^{10}}}${\displaystyle 2^{10}}$ [8]

## Remedy

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 ${\displaystyle 2^{10}}$

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 ${\displaystyle {\frac {6.67\times 10^{-11}\times 5.97\times 10^{24}}{(6.37\times 10^{6})^{2}}}}$

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:

${\displaystyle g={\frac {GM}{R^{2}}}={\frac {6.67\times 10^{-11}\times 5.97\times 10^{24}}{(6.37\times 10^{6})^{2}}}=9.81m/s^{2}}$

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:

${\displaystyle v_{e}={\sqrt {\frac {2GM}{R}}}={\sqrt {\frac {2\times 6.67\times 10^{-11}\times 5.97\times 10^{24}}{6.37\times 10^{6}}}}=1.12\times 10^{4}m/s}$

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
Go · Search · Stop words · 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 · User style
Editing
Starting a new page · Advanced editing · Editing FAQ · Edit toolbar · Export · Import · Shortcuts · Edit conflict · Page size
Referencing