Fanning Software Consulting

Using Greek Symbols

Facebook Twitter RSS Google+

QUESTION: I often want to use Greek symbols in my plot annotations, but I also want to write IDL programs that work in a device-independent way. That is to say, I don't want to write the program one way to work on my display and some other way to work when I want PostScript output. Do you have any suggestions for how I can write my code so that I always get the Greek symbols I want?

ANSWER: Yes, indeed. But, before I get into the details of how this is done, let me just say it is now possible with Coyote Graphics programs to simply embed Greek and other symbols, including superscripts and subscripts, in your plot annotation text. You simply put a "$\" in front of the symbol name, and a "$" after it. The code is exactly the same whether you are sending your output to the graphics display or writing your output to a PostScript file. Here is an example, that uses the Greek character mu on the X axis and an Angstrom squared symbol in the Y axis. You can find more extensive examples in the Coyote Plot Gallery. The symbol and Greek character names that can be used in this way are shown in figures further down in the article.

   cgPlot, cgDemoData(1), XTitle='Length ($\mu$M)', YTitle='Distance ($\Angstrom$$\up2$)'
Greek characters and other symbols can be embedded in text.
Greek characters and other symbols can be embedded in text.

Let me explain in more detail how this works. IDL doesn't make it particularly easy to write device-independent code that uses Greek symbols for graphics annotation. Suppose, for example, you wanted to label the X axis of a plot with the annotation Wavelength (μm), using the Greek letter mu.

First of all, you would have to go find the Greek letter in one of IDL's octal tables where you get the proper octal number for the letter you are interested in. (Octal number!? Now you have to go find your old notes from that CS-101 class you took twenty years ago.) Here is the table you are looking for if you want to display this plot on your graphics display. It is font number 4, the Simplex Greek font.

Green Simplex font table
The Simplex Greek font table.

Find your letter in the table. Take the number in the first column of that row with your letter (14x) in this case and multiply 14 times 10. (That's what the "x" means.) Take this new number (140) and add to it the number at the top of the column containing your letter. In this case, the number 14. So, 140 plus 14 equals 154. But this is an octal number and a byte value. (It is extremely important to remember it is a byte value.) In IDL we write an octal number with a double quote in front of it like this. The "B" at the end of the number makes sure it is a byte value.

   IDL> thisLetter = "154B

We need this letter as a string, so we will have to cast it. While we are doing that, we will add a "!4" to the front of the number, which will select the Simplex Greek font, and a "!X" to the end of the number to revert to whatever font was in effect before we switched to the Simplex Green font.

   IDL> greekLetter = '!4' + String(thisLetter) + '!X'

Now we are ready to use this letter in a plot.

   IDL> Plot, Loaddata(1), XTitle='Wavelength (' + greekLetter + 'm)', FONT=-1

You can see the results below.

Using the Greek letter.
The Greek letter μ used in a plot annotation on the display.

We would do exactly the same thing if we wanted to display this plot in a PostScript file, except we would have to use a different font table with a different font number! At least we would if we want to use either PostScript hardware fonts or True-Type fonts in our PostScript output, and what would be the point of using PostScript if you didn't want to use these better fonts? In this case, we would have to use the Symbol font table. Here is the Symbol font table for True-Type fonts.

The Symbol font table.
The Symbol font table.

As you can see, in this case, the octal number we are looking for is 155, not 154 as we found before. Plus, we need to create the Greek letter using the Symbol font, which is !9, not !4. The code will look like this.

   thisLetter = "155B
   greekLetter = '!9' + String(thisLetter) + '!X'

To use this kind of Greek letter, we have to be sure the PostScript device is set up to use ISOLATIN1 encoding, and that we are using either PostScript fonts or True-Type fonts. Since all of this (and a whole lot more) is set up with the Coyote Library PostScript set-up routines cgPS_Open and cgPS_Close, we will use those. The code will look like this.

   IDL> cgPS_Open, Filename=''
   IDL> Plot, Loaddata(1), XTitle='Wavelength (' + greekLetter + 'm)'
   IDL> cgPS_Close

You can see the result of these commands in the figure below.

The PostScript results.
The Greek letter μ used in a plot annotation in PostScript.

Naturally, to write device-independent code, we can do something like this, which will work if the current graphics device is the display or if it is the PostScript device.

   IF !D.Name EQ 'PS' THEN
      thisLetter = "155B
      greekLetter = '!9' + String(thisLetter) + '!X'
      thisLetter = "154B
      greekLetter = '!4' + String(thisLetter) + '!X'
   Plot, Loaddata(1), XTitle='Wavelength (' + greekLetter + 'm)'

A Program for Greek Letters

Of course, if you wanted to use some other Greek letter in your annotation, you will have to go find the two font tables you need (and IDL makes this particularly difficult, for some reason), get the proper values from the table, and so on. It can be a challenge.

It is for this reason I have written the IDL function, cgGreek. The Greek function takes as input the name of one of the 24 letters in the Greek alphabet. If your current graphics device is PostScript at the time the function is called, the PostScript version of the Greek letter will be returned. If the current graphics device is not PostScript, the Hershey font version of the Greek letter will be returned.

This means that I can now write device-independent code to use a Greek letter, like this.

   cgPlot, cgDemoData(1), XTitle='Wavelength (' + cgGreek('mu') + 'm)'

Be default, the Greek program will return lowercase Greek letters. If you wish to return uppercase Greek letters, set the CAPITAL keyword, like this.

   cgPlot, cgDemoData(1), XTitle='Wavelength (' + cgGreek('mu', /CAPITAL) + 'M)'

Or, you can simply make the first letter of the Greek letter name uppercase, like this.

   cgPlot, cgDemoData(1), XTitle='Wavelength (' + cgGreek('Mu') + 'M)'

If you wish to see the names and symbols of all the supported Greek letters, call the function with the EXAMPLE keyword set.

   void = cgGreek(/Example) 

You see below the result of calling the command above when in the PostScript device.

The Greek alphabet.
The Greek alphabet and symbols.

QUESTION: OK, I followed your advice, but when I look at my PostScript output I see a check mark instead of a Greek symbol. What's up with that?

ANSWER: You need to check for two things: (1) Are you using ISOLATIN1 encoding in your PostScript device (i.e., did you set this keyword when you configured the PostScript device), and (2) Are you using true-type fonts when you issue your graphics commands (!P.Font=1 or FONT=1 on your graphics command). cgPS_Open will make sure of both of these things, but if you are configuring your PostScript device in some other way, these are often overlooked.

QUESTION: OK, I did exactly what you said, and the results look great on my display, but the PostScript results are still screwy. I am producing the PostScript files from my Macbook. If I use the same program from my wife's Windoze machine, everything looks good. This is Bad News, because of what I had to go through to convince her a Mac was insanely great!

ANSWER: Humm. Yes, you have a problem. It appears that Macs, in their wisdom, have decided to use a Symbol font which is different from the Symbol font used on every other computer in the world. One could argue, since it uses Unicode, that it is more “correct” than other implementations of the Symbol font but that doesn't alleviate the pain very much.

I have added the ability to return the Greek Unicode character by setting the UNICODE keyword. I'd be curious to know if that helps or not.

QUESTION: What about other symbols, such as a greater-than-or-equal-to sign or an Angstrom symbol?

ANSWER: A more general symbol-producing program than the Greek program is to use the Coyote Graphics System routine cgSymbol. This program can produce the Greek symbols (by calling the cgGreek program), but it can also produce other symbols both for the display and PostScript devices, as shown in the figure below.

Other cgSymbol symbols.
In additon to Greek symbols, the
cgSymbol program can produce these
symbols, too.

Version of IDL used to prepare this article: IDL 7.1.2

Written: 8 January 2010
Updated: 23 December 2012