This is documentation for Mathematica 3, which was
based on an earlier version of the Wolfram Language.
View current documentation (Version 11.1)
 Documentation / Mathematica / The Mathematica Book / Principles of Mathematica / Modularity and the Naming of Things  /

2.6.13 Advanced Topic: Intercepting the Creation of New Symbols

Mathematica creates a new symbol when you first enter a particular name. Sometimes it is useful to "intercept" the process of creating a new symbol. Mathematica provides several ways to do this.


Printing a message when new symbols are created.

  • This tells Mathematica to print a message whenever a new symbol is created.
  • In[1]:= On[General::newsym]

  • Mathematica now prints a message about each new symbol that it creates.
  • In[2]:= sin[k]

    General::newsym: Symbol sin is new.

    General::newsym: Symbol k is new.

    Out[2]=

  • This switches off the message.
  • In[3]:= Off[General::newsym]

    Generating a message when Mathematica creates a new symbol is often a good way to catch typing mistakes. Mathematica itself cannot tell the difference between an intentionally new name, and a misspelling of a name it already knows. But by reporting all new names it encounters, Mathematica allows you to see whether any of them are mistakes.


    Performing operations when new symbols are created.

    When Mathematica creates a new symbol, you may want it not just to print a message, but instead to perform some other action. Any function you specify as the value of the global variable $NewSymbol will automatically be applied to strings giving the name and context of each new symbol that Mathematica creates.

  • This defines a function to be applied to each new symbol which is created.
  • In[4]:= $NewSymbol = Print["Name: ", #1, " Context: ", #2]&

    Out[4]=

  • The function is applied once to v and once to w.
  • In[5]:= v + w

    Name: v Context: Global`

    Name: w Context: Global`

    Out[5]=