Why use \[FormalN]?

A simple example of the use of formal symbols is for mathematical tasks that require the use of dummy variables. Most functions like Plot will localize their dummy variables, but not all. For example, LinearModelFit will not work with symbols that already have a value assigned to them:

x = 1;
LinearModelFit[RandomReal[1, {10, 2}], {1, x}, x]

(spits out errors)

This is where the formal symbols come in handy. Because they can never have a value assigned to them (under normal circumstances), they are ideal for this sort of thing:

LinearModelFit[RandomReal[1, {10, 2}], {1, \[FormalX]}, \[FormalX]]

The code above will always run because you can be sure that \[FormalX] is not going to have a value.

edit

As pointed out in a comment, you can just Clear[x] before using a function like LinearModelFit if you're certain that this will not be a problem elsewhere. However, you can come up with very specific situations where things can still go wrong with that. For example, if you put a slider like Slider[Dynamic[x], {0, 1}] in your notebook, manipulating that slider can affect running computations in the kernel because dynamic elements interrupt the kernel when they need to be updated. For example, try this:

Slider[Dynamic[x]]
Do[Print[x]; Pause[1], {10}]

You'll see that the printed values of x change when you manipulate the slider. You can imagine that this could happen during computation that takes several seconds to complete. It's not very likely to happen, but if you use \[FormalX] for your fit you'll know for certain that it won't.


These are typically used when you are making symbolic transformations and you don't want to risk failure from existing global definitions. For example:

Sin'[\[FormalX]]

D[Sin[\[FormalX]], \[FormalX]]

Integrate[Sin[\[FormalX]], {\[FormalX], 0, \[FormalY]}]

Sum[Sin[t], {t, 0, \[FormalX]}]

Limit[Sin[\[FormalX]]/\[FormalX], \[FormalX] -> 0]

All of these will fail if instead of \[FormalX] one uses a symbol that has an assigned numeric value.

In some cases one could use Block instead to prevent contamination, e.g.:

x = 1.23;
Block[{x},
  Limit[Sin[x]/x, x -> 0]
]

However, if the output expression needs to include a Symbol, e.g. Cos[\[FormalX]] you might as well start with Formal Symbols and handle both problems at once.

It is worth noting that while one cannot directly assign to formal symbols you can still give them values using scoping constructs like With, Block, and Table:

With[{\[FormalX] = 5}, \[FormalX] + 1]
Block[{\[FormalX] = 5}, \[FormalX] + 1]
Table[\[FormalX] + 1, {\[FormalX], 3}]
6

6

{2, 3, 4}

These are considered special and used by System` functions. Bad things could happen if values are assigned to them.

DifferenceRootReduce[n^2 + 1, n]

(* DifferenceRoot[Function[{\[FormalY], \[FormalN]}, {(-2 - 
     2 \[FormalN] - \[FormalN]^2) \[FormalY][\[FormalN]] + (1 + \[FormalN]^2) \[FormalY][1 + \[FormalN]] == 0, \[FormalY][0] == 
 1}]][n] *)

TransferFunctionModel[1]
(* TransferFunctionModel[{{{1}}, 1}, \[FormalS]] *)

If you unprotect and assign values to them, you get unexpected or even incorrect results.