Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

What characters are allowed in mathematica variable names? i.e. how

3,017 views
Skip to first unread message

Jesse Perla

unread,
Apr 12, 2012, 4:44:22 AM4/12/12
to
In other languages like C++, _ is a kosher character to be in variable names. It is not in mathematica, so things like "my_first_variable" don't work. I have been using a ` to separate out the words to get things like my`first`variable, but now realize it is creating other issues due to the scoping it introduces.

Are there are other characters I can use to separate out words in variable names?

Thanks

djmpark

unread,
Apr 13, 2012, 4:49:10 AM4/13/12
to
Jesse,

Here are several possibilities, which you can track down on the following
Help pages.

guide/SpecialCharacters
guide/NonPrintingCharacters
ref/character/LetterSpace
guide/TextualForms
ref/character/UnderBracket

my\[LetterSpace]first\[LetterSpace]variable

my\[UnderBracket]first\[UnderBracket]variable

These are both Symbols and can be used as variables.

LetterSpace has the alias esc ls esc.
UnderBracket has the alias esc u[ esc.

Here is another "special form" that can be used for primed and double primed
symbols.

x\:02b9 which gives x'
x\:02ba which gives x"

These have the Head Symbol and can be used for variables. They are not
confused with derivatives and the primes are at the proper level. For some
reason no one seems to mind the lack of regular primed symbols in
Mathematics despite the fact that they are extremely common in the
mathematical literature. In any case, these particular forms are on the
Presentations Application palette.


David Park
djm...@comcast.net
http://home.comcast.net/~djmpark/index.html

Jesse Perla

unread,
Apr 13, 2012, 4:50:11 AM4/13/12
to

Thanks,

Are there any characters that are simply in the ASCII set and hence won't
expand to something complicated when I use a .m file or use mathematica on
the commandline?

-Jesse

Murray Eisenberg

unread,
Apr 13, 2012, 4:52:14 AM4/13/12
to
Why not just camel-case? E.g.: myFirstVariable.

On 4/12/12 2:35 AM, Jesse Perla wrote:
> In other languages like C++, _ is a kosher character to be in variable names. It is not in mathematica, so things like "my_first_variable" don't work. I have been using a ` to separate out the words to get things like my`first`variable, but now realize it is creating other issues due to the scoping it introduces.
>
> Are there are other characters I can use to separate out words in variable names?

--
Murray Eisenberg murraye...@gmail.com
80 Fearing Street phone 413 549-1020 (H)
Amherst, MA 01002-1912

Jesse Perla

unread,
Apr 13, 2012, 4:52:45 AM4/13/12
to
Purely aesthetics. I hate camel case because it is inconsistent with how I
program in other languages, and when translating math I sometimes want
small or large letters in names. (e.g. x is an element, X is the set}

Murray Eisenberg

unread,
Apr 13, 2012, 4:53:15 AM4/13/12
to
If you insist on using a character as separator, you could use breve,
typed as Esc bv Esc (without the spaces, of course).

On 4/12/12 2:35 AM, Jesse Perla wrote:
> In other languages like C++, _ is a kosher character to be in variable names. It is not in mathematica, so things like "my_first_variable" don't work. I have been using a ` to separate out the words to get things like my`first`variable, but now realize it is creating other issues due to the scoping it introduces.
>
> Are there are other characters I can use to separate out words in variable names?
>
> Thanks

Jesse Perla

unread,
Apr 13, 2012, 4:53:46 AM4/13/12
to

Thanks. How would I type that in ascii and would it show up escaped in
\[....]in a text file on the server? Assume that I am editing things
purely in a text file.

Yi Wang

unread,
Apr 13, 2012, 4:54:16 AM4/13/12
to
It's an interesting question. I also tried to find the answer. The only character that I found is $. Say you can write my$first$variable. But anyway I am more used to myFirstVariable than $.

Best,
Yi

David Reiss

unread,
Apr 13, 2012, 4:55:48 AM4/13/12
to
For your main issue -- how to make your variables readable -- simply
use camel case: I.e.,

myFirstVariable or MyFirstVariable. However, remember that
Mathematica uses camel case for all of its parameters, with the key
convention that all of them begin with a capital character. So, if
you begin yours with a lowercase character you will not encounter name
conflicts. If you do start with an Uppercase character then you need
to be careful that you are not encountering such conflicts.

--David

Ray Koopman

unread,
Apr 13, 2012, 4:58:21 AM4/13/12
to
No. The "solution" is camelCase: in multiWordNames,
capitalize the initial letter of each word except the first.
(If you capitalize the initial letter of the first word too
then your name may conflict with a built-in symbol.)

Murray Eisenberg

unread,
Apr 13, 2012, 4:59:22 AM4/13/12
to
\[Breve]

For such things, in the Mathematica front end, just open the Special
Characters palette. When you locate the character you want and mouse
over it, you'll see a display of its short-cut abbreviation (with Esc).
To see the "purely ascii"

On 4/12/12 9:45 AM, Jesse Perla wrote:
> Thanks. How would I type that in ascii and would it show up escaped in
> \[....]in a text file on the server? Assume that I am editing things
> purely in a text file.
>
> On Thu, Apr 12, 2012 at 9:40 AM, Murray Eisenberg<mur...@math.umass.edu>wrote:
>
>> If you insist on using a character as separator, you could use breve,
>> typed as Esc bv Esc (without the spaces, of course).
>>
>>
>> On 4/12/12 2:35 AM, Jesse Perla wrote:
>>
>>> In other languages like C++, _ is a kosher character to be in variable
>>> names. It is not in mathematica, so things like "my_first_variable" don't
>>> work. I have been using a ` to separate out the words to get things like
>>> my`first`variable, but now realize it is creating other issues due to the
>>> scoping it introduces.
>>>
>>> Are there are other characters I can use to separate out words in
>>> variable names?
>>>
>>> Thanks
>>>
>>>
>> --
>> Murray Eisenberg murraye...@gmail.com
>> 80 Fearing Street phone 413 549-1020 (H)
>> Amherst, MA 01002-1912
>>
>

--
Murray Eisenberg mur...@math.umass.edu
Mathematics & Statistics Dept.
Lederle Graduate Research Tower phone 413 549-1020 (H)
University of Massachusetts 413 545-2859 (W)
710 North Pleasant Street fax 413 545-1801
Amherst, MA 01003-9305

Murray Eisenberg

unread,
Apr 13, 2012, 4:59:53 AM4/13/12
to
\[Breve]

For such things, in the Mathematica front end, just open the Special
Characters palette. When you locate the character you want and mouse
over it, you'll see a display of its short-cut abbreviation (with Esc).
To see the "purely ascii" form (with \[...]), just select the cell in
the notebook where you've typed the character with the Esc sequence --
or just inserted it from the menu -- and change the display of that cell
to reveal the underlying expression (on Mac, this is Cmd Shift E).

(Sorry my original version of this message was truncated when I sent it.)

Jesse Perla

unread,
Apr 13, 2012, 5:00:23 AM4/13/12
to
Thanks. Lets say that I never want to use the FrontEnd and don't want to
use any escaped characters (i.e. \[....]) because they are a pain to write
and format within command line mathematica.

Is there a list of kosher symbols that are allowed? In C, for example, it
would be all the alphanumeric characters, _, and maybe a few more.

On Thu, Apr 12, 2012 at 3:23 PM, Murray Eisenberg <mur...@math.umass.edu>wrote:

> \[Breve]
>
> For such things, in the Mathematica front end, just open the Special
> Characters palette. When you locate the character you want and mouse over
> it, you'll see a display of its short-cut abbreviation (with Esc). To see
> the "purely ascii" form (with \[...]), just select the cell in the notebook
> where you've typed the character with the Esc sequence -- or just inserted
> it from the menu -- and change the display of that cell to reveal the
> underlying expression (on Mac, this is Cmd Shift E).
>
> (Sorry my original version of this message was truncated when I sent it.)
>
>
> On 4/12/12 9:45 AM, Jesse Perla wrote:
>
>> Thanks. How would I type that in ascii and would it show up escaped in
>> \[....]in a text file on the server? Assume that I am editing things
>> purely in a text file.
>>
>> On Thu, Apr 12, 2012 at 9:40 AM, Murray Eisenberg<mur...@math.umass.**edu<mur...@math.umass.edu>

Murray Eisenberg

unread,
Apr 13, 2012, 5:02:25 AM4/13/12
to
Not ever to use the Front End seems like a self-imposed unnecessary
restriction.
>> Mathematics& Statistics Dept.

J. McKenzie Alexander

unread,
Apr 13, 2012, 4:47:07 AM4/13/12
to
I think any letter-like character can be used in variable names. See the following page in the documentation center discussing textual forms and letters and letter-like forms.

guide/TextualForms

tutorial/LettersAndLetterLikeForms

One character which basically does what you want is \[UnderParenthesis]. You can enter it quickly via ESC u( ESC. I just tried it out in Mathematica 8 and it can be used in variable names.

Cheers,

Jason

--
Dr J. McKenzie Alexander
Department of Philosophy, Logic and Scientific Method
London School of Economics and Political Science
Houghton Street, London WC2A 2AE



On 12 Apr 2012, at 07:35, Jesse Perla wrote:

> In other languages like C++, _ is a kosher character to be in variable names. It is not in mathematica, so things like "my_first_variable" don't work. I have been using a ` to separate out the words to get things like my`first`variable, but now realize it is creating other issues due to the scoping it introduces.
>
> Are there are other characters I can use toseparate out words in variable names?
>
> Thanks
>


Please access the attached hyperlink for an important electronic communications disclaimer: http://lse.ac.uk/emailDisclaimer

Jesse Perla

unread,
Apr 13, 2012, 5:02:56 AM4/13/12
to

Sadly, If I am running mathematica on a cluster in batch jobs, connecting
over ssh I don't have much of a choice. Sometimes I play around on the
frontend to get things working, but it doesn't do me much good if the
output of the batch process is not readable and full of \[...]

On Thu, Apr 12, 2012 at 4:33 PM, Murray Eisenberg <mur...@math.umass.edu>wrote:

> Not ever to use the Front End seems like a self-imposed unnecessary
> restriction.
>
>
> On 4/12/12 3:32 PM, Jesse Perla wrote:
>
>> Thanks. Lets say that I never want to use the FrontEnd and don't want to
>> use any escaped characters (i.e. \[....]) because they are a pain to write
>> and format within command line mathematica.
>>
>> Is there a list of kosher symbols that are allowed? In C, for example,
>> it
>> would be all the alphanumeric characters, _, and maybe a few more.
>>
>> On Thu, Apr 12, 2012 at 3:23 PM, Murray Eisenberg<mur...@math.umass.**edu<mur...@math.umass.edu>
>> >wrote:
>>
>> \[Breve]
>>>
>>> For such things, in the Mathematica front end, just open the Special
>>> Characters palette. When you locate the character you want and mouse over
>>> it, you'll see a display of its short-cut abbreviation (with Esc). To see
>>> the "purely ascii" form (with \[...]), just select the cell in the
>>> notebook
>>> where you've typed the character with the Esc sequence -- or just
>>> inserted
>>> it from the menu -- and change the display of that cell to reveal the
>>> underlying expression (on Mac, this is Cmd Shift E).
>>>
>>> (Sorry my original version of this message was truncated when I sent it.)
>>>
>>>
>>> On 4/12/12 9:45 AM, Jesse Perla wrote:
>>>
>>> Thanks. How would I type that in ascii and would it show up escaped in
>>>> \[....]in a text file on the server? Assume that I am editing things
>>>> purely in a text file.
>>>>
>>>> On Thu, Apr 12, 2012 at 9:40 AM, Murray Eisenberg<mur...@math.umass.***
>>>> *edu<mur...@math.umass.edu>
>>>>
>>>> wrote:
>>>>>
>>>>
>>>> If you insist on using a character as separator, you could use breve,
>>>>
>>>>> typed as Esc bv Esc (without the spaces, of course).
>>>>>
>>>>>
>>>>> On 4/12/12 2:35 AM, Jesse Perla wrote:
>>>>>
>>>>> In other languages like C++, _ is a kosher character to be in variable
>>>>>
>>>>>> names. It is not in mathematica, so things like "my_first_variable"
>>>>>> don't
>>>>>> work. I have been using a ` to separate out the words to get things
>>>>>> like
>>>>>> my`first`variable, but now realize it is creating other issues due to
>>>>>> the
>>>>>> scoping it introduces.
>>>>>>
>>>>>> Are there are other characters I can use to separate out words in
>>>>>> variable names?
>>>>>>
>>>>>> Thanks
>>>>>>
>>>>>>

Jesse Perla

unread,
Apr 13, 2012, 5:03:26 AM4/13/12
to

On Thu, Apr 12, 2012 at 8:16 AM, djmpark <djm...@comcast.net> wrote:

> Jesse,
>
> Here are several possibilities, which you can track down on the following
> Help pages.
>
> guide/SpecialCharacters
> guide/NonPrintingCharacters
> ref/character/LetterSpace
> guide/TextualForms
> ref/character/UnderBracket
>


Thanks for the pointers. It seems like Mathematica has a great list of all
of the escaped characters it can put into text (along with the escape
sequence), but doesn't have a list of valid non-escaped characters. Are
only alpha-numeric letters allowed as non-escaped characters in a variable
name?

I was looking for an EBNF definition for mathematica that might let me read
the list, but I can't find that either.


Alexei Boulbitch

unread,
Apr 13, 2012, 5:05:59 AM4/13/12
to
In other languages like C++, _ is a kosher character to be in variable names. It is not in mathematica, so things like "my_first_variable" don't work. I have been using a ` to separate out the words to get things like my`first`variable, but now realize it is creating other issues due to the scoping it introduces.

Are there are other characters I can use to separate out words in variable names?

Thanks


You will be safe capitalizing the first characters of the words in the names of variables. For instance, myFirstVariable. Do not use the sign $ as the initial letter in the variable name, do not use arithmetical and logical signs: *,+,- /, & and && to connect parts of the variable name, or to begin it. Have fun, Alexei

Alexei BOULBITCH, Dr., habil.
IEE S.A.
ZAE Weiergewan,
11, rue Edmond Reuter,
L-5326 Contern, LUXEMBOURG

Office phone : +352-2454-2566
Office fax: +352-2454-3566
mobile phone: +49 151 52 40 66 44

e-mail: alexei.b...@iee.lu




A Retey

unread,
Apr 14, 2012, 3:08:59 AM4/14/12
to
Hi,


> Are there any characters that are simply in the ASCII set and hence won't
> expand to something complicated when I use a .m file or use mathematica on
> the commandline?

no, AFAIK there is only $ that could be used as that. But that has the
disadvantage that it is used by Mathematica itself to create unique
symbol names for lexical scoping with Module (and some other places) and
thus needs to be used with care (or probably not at all): you would have
to make sure that the names you create don't interfere with anything
that Mathematica could create, e.g. x$12 would be a very bad choice
since Module[{x},x] will create exactly such symbols.

hth,

albert

djmpark

unread,
Apr 14, 2012, 3:13:35 AM4/14/12
to
You never explain why you are running Mathematica on a cluster in batch
mode. I could envision someone setting up a highly intensive numerical
calculation, for example, in Mathematica and then feeding it to batch
processing (although I have no experience in that kind of thing). But I
don't see why it would be necessary to work from the command line to do
this.

Mathematica is designed to work with the FrontEnd and notebook interface.
All the nice formatting, display of graphics, finding things, importing and
exporting, and operations such as DynamicModules are integral front end
functionality. If you want FrontEnd functionality you have to use the
FrontEnd, or program it yourself - which means you will probably never get
to the math.

My suggestion is:

1) Get a good personal computer and work on that. They are not that
expensive. Forget the server.

2) If you have to use a server and batch processing because you absolutely
need processing power that is not available on a PC, then divide the work
into the portion that requires input, specification, formatting, display
and presentation from that part that needs the intensive processing. Then
use the notebook interface to ship it off to the batch processor, wait for
the results and when they arrive manipulate them for presentation. I'm
fairly certain someone can show you how to do this.

3) If someone is forcing you to use Mathematica solely via the command line
interface but wants front end features then find some better situation. It
will be too frustrating.

Unless you are in need of special augmented processing power, batch
processing is so 1960's era.

dr DanW

unread,
Apr 15, 2012, 3:20:21 AM4/15/12
to
David,
Don't forget that Workbench is also a perfectly legitimate platform for working in Mathematica, and primarily displays in ASCII. Programming with these Unicode characters with the ungainly \[...] names is a serious pain, and makes for uniquely unreadable code.

Also, camel case is fine when just typing symbol names, but I am currently working on an application where I need to generate symbol names. Without some kind of delimiting character, some of the generated names may conflict or not be readable. I ended up using $ out of sheer necessity, as it is on e of the few ASCII non- alphanumeric characters available for naming.

Daniel

David Bailey

unread,
Apr 16, 2012, 6:10:20 AM4/16/12
to
On 13/04/2012 10:00, Jesse Perla wrote:
> Thanks. Lets say that I never want to use the FrontEnd and don't want to
> use any escaped characters (i.e. \[....]) because they are a pain to write
> and format within command line mathematica.
>

I am probably one of the most command-line oriented programmers you will
find, but absolutely nothing would persuade me to abandon the FrontEnd!
Even is you like your program to be stored in a clear test .m file, do
you realise that you can edit these in Mathematica, much as if they were
a notebook, and the result will save back as a .m file. It is even
possible to add headings, sub headings and text cells to your code.
These are hidden as comments in the .m file! Probably notebooks should
have been organised in this way!

At least try editing .m files in the FrontEnd!

David Bailey
http://www.dbaileyconsultancy.co.uk


James Stein

unread,
Apr 16, 2012, 6:07:45 AM4/16/12
to

Another possibility: characters with various marks, such as cedilla or
umlaut, can also be used in characters. Depending on taste, they may look
better or worse that $, but have the advantage of no conflicting with
Mathematica's use of $.

Murray Eisenberg

unread,
Apr 16, 2012, 6:08:16 AM4/16/12
to
For the auto-generated symbol names, let's say you start with some
alphabetic stub and then append 6-digit tails; one million distinct
names isn't enough?

Or start with a few fixed letters for the stub and, complete the stub
with 5 arbitrary letters (both upper- and lower-case), then append just
4 digits; that's on the order of 10^12 distinct names.


On 4/15/12 3:18 AM, dr DanW wrote:
> David,
> Don't forget that Workbench is also a perfectly legitimate platform for working in Mathematica, and primarily displays in ASCII. Programming with these Unicode characters with the ungainly \[...] names is a serious pain, and makes for uniquely unreadable code.
>
> Also, camel case is fine when just typing symbol names, but I am currently working on an application where I need to generate symbol names. Without some kind of delimiting character, some of the generated names may conflict or not be readable. I ended up using $ out of sheer necessity, as it is on e of the few ASCII non- alphanumeric characters available for naming.

Jesse Perla

unread,
Apr 18, 2012, 3:54:22 AM4/18/12
to
On Monday, April 16, 2012 6:10:20 AM UTC-4, David Bailey wrote:
> On 13/04/2012 10:00, Jesse Perla wrote:
> Even is you like your program to be stored in a clear test .m file, do
> you realise that you can edit these in Mathematica, much as if they were
> a notebook, and the result will save back as a .m file. It is even
> possible to add headings, sub headings and text cells to your code.
> These are hidden as comments in the .m file! Probably notebooks should
> have been organised in this way!

Actually, I like debugging in the FrontEnd or workbench (or at least editing .m files as the binary notebook files do not play nice with SVN, diffs, separating code with output, or having multiple people working on the same code base). As long as there are no escaped characters in variable names, the code is reasonably easy to read in both text and diff form.

As people seem to wonder why me (and likely others) need to run command-line, let me give some context to the issue: I need to run both on Windows FrontEnd and linux command-line as my problem is too time consuming to run on my desktop. The issue is that he job/batch mode of clusters is the way they force everyone to submit jobs so that they can schedule when and where they are run, which makes any hope of using a live session in a notebook impossible. Another issue is that every cluster has elaborate multi-level ssh security methods which make it difficult/impossible to connect directly wit h the FrontEnd. Sorry if I overstated the hypothetical of not using the FrontEnd, I just wanted to filter out comments on how I shouldn't be using command-line or reading clear-text files.

Thanks for all of the responses. It is a matter of taste, but it to summarize people's answers to my questions:

* You can use a $, as in "my$variable". But you need to be extremely careful with Mathematica symbols and name mangling, in particular it is very dangerous to use numbers such as "my$12"

* camelCase is always an option of course, but besides the aesthetics reasons (many find them more difficult to read), you need to be very careful with capitalized letters for the first character as they can clash with mathematica symbols. This is an issue if you are trying to transcribe a lot of math faithfully in the standard notation of your discipline (e.g. "x" for anelement of a set, and "X" for the set itself. Or "x" for the log of a variable, and "X" for the variable in levels).

* I originally had used the "`" symbol, which is also used for scoping. e.g "my`variable". I figured it couldn't really hurt, even if it was also used for scoping/namespaces. Don't do this, as the subtle changes in scoping seemed to math other coding difficult. In particular, I ran into a problem when I tried to store out all globals with DumpSave.

0 new messages