I want to produce a Mathematica Computable Document in which N appears as a variable in my formulae. But N is a reserved word in the Mathematica language. Is there a way round this other than using a different symbol? It seems a severe limitation if you cannot use Mathematica to generate papers in which N is employed as a variable.

  • 1
    $\begingroup$ Writing up the paper and writing code to do the computations are two different things ... surely the name of the variable can't be that big an issue? Even in a CDF, the visual presentation doesn't need to match the underlying code. You can label stuff using $N$ even if in the code it's represented using a variable of a different name. $\endgroup$
    – Szabolcs
    Commented Jun 22, 2012 at 10:44
  • 3
    $\begingroup$ You can use \[FormalCapitalN] instead of N. It looks almost like N. $\endgroup$
    – Artes
    Commented Jun 22, 2012 at 10:46
  • 3
    $\begingroup$ ...or \[ScriptCapitalN], or \[CapitalNu], or... why do you need $N$ to be a variable anyway? How sure are you that your paper won't need the actual functionality of N[]? $\endgroup$ Commented Jun 22, 2012 at 10:59
  • 2
    $\begingroup$ If you want to display an equation, you could work with n in intermediate steps and in the final display step do a replacement. /.n->"N" should be save $\endgroup$ Commented Jun 22, 2012 at 11:13
  • 7
    $\begingroup$ @Ajasja That's not a gun, that's a land mine. At least a gun has a safety catch. $\endgroup$
    – Heike
    Commented Jun 22, 2012 at 11:32

7 Answers 7


You could use capital Nu, \[CapitalNu], from the Greek alphabet. It is visually almost identical to capital N from the Roman alphabet. But it has no predetermined assignment.

\[CapitalNu] = 5
2 \[CapitalNu]

The following shows how the input is displayed on screen.


  • $\begingroup$ @J.M. Oops. I only read the comments after submitting my answer. Sorry. $\endgroup$
    – DavidC
    Commented Jun 22, 2012 at 12:30
  • $\begingroup$ No apologies necessary; I upvoted your fine answer after all. :D $\endgroup$ Commented Jun 22, 2012 at 12:42

As it is often voiced here, modifying built-in variables is not a good idea most of the times, especially in case of such fundamental symbols as N. It is used heavily through millions of underlying code lines, and you will never know where your change can (and will) cause any mischief (or catastrophe).

On the other hand, the name of the variable and the way it is displayed could be completely different. You can use Format for example to give a specific formatting whenever n is printed:

n[x_] := N[x/5];
Format[n] := Style["N", Red, Italic, Bold];


Mathematica graphics



(Note, that since Format does not have attribute HoldFirst or HoldAll, n should not have any OwnValues, otherwise the assignment won't happen.)

  • 2
    $\begingroup$ I think for something this simple, where all that matters is the way something looks on output, Format is the best way to go. Though certainly several of the other suggestions are also viable. $\endgroup$ Commented Jun 22, 2012 at 17:43
  • $\begingroup$ I would have used n /: MakeBoxes[n, form_] := With[{boxes = MakeBoxes[N, form]}, InterpretationBox[boxes, n]], personally. Bad experiences with Format. $\endgroup$ Commented Jun 27, 2013 at 0:29

The methods suggested by David and István already do the job perfectly, but one could add something to the collection:

As described in the documentation on Operators without Built-in Meanings, there are some two-dimensional forms such as Subscript and UnderBar which can be re-defined to your liking. This is particularly useful if you need the 'N' with additional decorations anyway, such as $\text{N}_1$, $\text{N}_\text{total}$ or $\underline{\text{N}}$.

For these kinds of symbols you don't re-define N directly, but instead add a definition that is associated with the two-dimensional form in which the symbol appears. For example, I can set the following (note that the form Subscript["N", 0] can be entered more conveniently using keyboard shortcuts, but that wouldn't display so nicely here):


Subscript["N", 0] = 6

(* ==> 6 *)

7 Subscript["N", 0]

(* ==> 42 *)


(* ==> {HoldPattern[Subscript["N", 0]] :> 6} *)

The last line shows how the definition is stored as a definition associated with DownValues[Subscript]. The fact that we get 42 from the multiplication shows that the definition of the subscript works as expected.

To erase the definition, I can either use ClearAll[Subscript] or more specifically

Subscript["N", 0] =.

The shortcut to input the subscript is to type "N" followed by Ctrl- and then 0.

You can also use strings in the subscript, instead of numbers. That allows you to get a "variable" that displays as $N$ without decoration by entering

Subscript["N", ""]

Again you could assign things to this subscripted form as well. Another use for a string subscript would be (here I'll copy the two-dimensional form in box notation, it should look nicer when copied into the notebook):

\!\(\*SubscriptBox[\("\<N\>"\), \("\<total\>"\)]\) = 100

(* ==> 100 *)

Sum[i, {i, 
\!\(\*SubscriptBox[\("\<N\>"\), \("\<total\>"\)]\)}]

(* ==> 5050 *)

Of course one can use these re-defined operators as a substitute for Format as well. For example, here is some symbolic input and symbolic output:

nn = Subscript["N", ""]


Sum[i, {i, nn}]

$\frac{1}{2} \text{N}_{\text{}}\left(\text{N}_{\text{}}+1\right)$

You can then keep working with nn as the symbolic variable for computations but get the capital $\text{N}$ in the displayed output.

But you can also do numerical substitutions in the usual way, as here:

% /. Subscript["N", ""] -> 100

(* ==> 5050 *)

(again, all the expressions looking like Subscript above can be made to appear more naturally by using the two-dimensional keyboard input methods).

Finally, everything I said here about Subscript also applies to things like UnderBar["N"] which displays as $\underline{\text{N}}$.

The Subscript combined with strings can also be useful when defining a whole set of similarly named variables automatically using something like this:

normalVector = 
 Array[Subscript["N", {"x", "y", "z"}[[#]]] &, 3]


Norm[normalVector] /. {
\!\(\*SubscriptBox[\("\<N\>"\), \("\<x\>"\)]\) -> 1, 
\!\(\*SubscriptBox[\("\<N\>"\), \("\<y\>"\)]\) -> 0, 
\!\(\*SubscriptBox[\("\<N\>"\), \("\<z\>"\)]\) -> 0}

(* ==> 1 *)

For even better formatting, you could replace normalVector by "N" Ctrl'7' escvecesc which displays as $\vec{\text{N}}$.


Ok, I'm late here, but the first thing I would have answered hasn't been answered already.

If it's a one-cell-er, you can use Module

 N = 8;
 N + 3]

This can never bring you trouble with internal definitions since that N you see is not actually N.

If it comprises multiple cells, you can use contexts, and interpret the RED N as a reminder that you're doing weird things. You can always access the regular N function by System`N, and return to normal by either Remove[N], or $ContextPath=Rest@$ContextPath

PrependTo[$ContextPath, "prvt`"];
  • $\begingroup$ I thought about adding this, but the approach was already mentioned briefly by @István in the comments (+1). $\endgroup$
    – Jens
    Commented Jun 22, 2012 at 16:59
  • $\begingroup$ @Jens, weird he hasn't put it in his own answer... $\endgroup$
    – Rojo
    Commented Jun 22, 2012 at 17:02

There is actually an easy way to achieve this (almost) perfectly. Just start the variable name with a none printing character. I use this technique to have variables for atomic orbitals that start with numbers e.g. 3d or 4f - also not allowed by Mathematica. I just write the name as \[Null]3d. Mathematica is now fine with this as a variable. It works for \[Null]N or any other "protected" name. The output looks fine because the Null character doesn't take up space.

A variation is \[LetterSpace] - an almost-invisible space treated as a letter, e.g. in a symbol name - but it has a very faint bar showing


To continue with Ajasja's land mine theme, it's not so problematic to use N as a symbol in equations like this:

Mathematica graphics

as long as you keep the following in mind:

  • Don't try to use it in an assignment (because you can't)
  • Don't use [...] in its neighborhood, nor #&@
  • Keep it far away from Map, Apply, Fold, Nest etc.

But frankly, this probably isn't the best idea to go for. Land mines may be safer.


The question of reassigning build-in Symbols has come up a number of times. There is an approach not yet posted here, however it is not recommended. Nevertheless for the sake of completeness I shall illustrate it.

Because N is not Locked it is not the more difficult example, as one can (perilously) Unprotect and redefine it. Instead I shall use I as it is Locked, and redefinition comes up from time to time.(1)(2)

It is possible to change the Context of a System Symbol, even a Locked one. Therefore:

Context[I] = "foo`";
System`I = 3;

E^(I*Pi) + 1 // N

The original Symbol is still usable with a qualified context:

E^(foo`I*Pi) + 1

Functions that return Complex values still work:

out = FactorInteger[Prime[100], GaussianIntegers -> True]
{{-I, 1}, {10 + 21 I, 1}, {21 + 10 I, 1}}

Note the InputForm and FullForm of that output:

out // InputForm
{{-foo`I, 1}, {10 + 21*foo`I, 1}, {21 + 10*foo`I, 1}}
out // FullForm

I think messing with System Context Symbols in this manner is a Bad Idea apart from a few specific cases(3) but I also think users should be aware of the full set of tools at their disposal and make individual decisions.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.