Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I seek to use a token as an axis label, but this token, having other meanings, is expanded by Mathematica. If I enclose the token in quotes, it is not expanded, but it is also no longer italicized. Using LabelStyle->{Italics} results in italicization of all fonts in the figure (axis labels and axis tick labels). How do I circumvent token expansion (or equivalently, in effect, italicize only the axis labels and not the axis tick labels)?

Two examples:

(1) I may wish to plot a function f (say f=x^2) and want the axis label to be simply f (in italics), but it is expanded to x^2 by the command Plot[f, {x, -2, 2}, AxesLabel -> {x, f}]. Changing to Plot[f, {x, -2, 2}, AxesLabel -> {x, "f"}] results in italic x and roman f.

(2) I may wish to label an axis with E, but it is changed by Mathematica to e (the irrational constant). Again, I can use "E", but this has the undesired effect of altering the font face.

share|improve this question
add comment

2 Answers

up vote 12 down vote accepted

What you are looking for can be achieved by wrapping the labels in HoldForm, or if you prefer, HoldForm@InputForm. For example, here is a plot that combines both labeling issues you mentioned:

f = x^2;
Plot[f, {x, -2, 2}, AxesLabel -> {x, HoldForm[InputForm[E = f]]}]

The two issues you mention are indeed separate:

  1. To get f instead of $x^2$ you should use HoldForm, but that still allows the display of TraditionalForm shorthand forms for built-in symbols such as E (which is Euler's constant but is pretty-printed as $\mathbb e$).
  2. To prevent the replacement of E by $\mathbb e$, InputForm can be used.

As you noticed, using strings in labels (though sometimes perfectly fine) has undesirable effects on the font and requires more "finger-painting" with styles. The HoldForm approach is easier to use and the code is easier to read when labels get complicated.

See also this related question.

To expand on this topic:

Sometimes you need more complicated labels that require "two-dimensional" typesetting, as in $\psi = \frac{1}{2}\int f(x) \mathbb{d}x$, see this image:

formulas in labels

Edit: how to get formatting into labels in general

For output like the above, the essential ingredient is that the expression should be wrapped in a FormBox. Strings aren't made for two-dimensional display, but Mathematica has a way of sneaking FormBoxes into strings: see the documentation.

Therefore, you can get a two-dimensional formula into a plot label either using HoldForm or a string. Using HoldForm, I got the formatting in the image by doing the following:

  1. Create the formula in a TraditionalForm environment. This could e.g. be in a Text cell by starting an equation with Ctrl-( and ending with Ctrl-).
  2. Copy this formula from within that math inset.
  3. Lay out your plot by typing something like Plot[f, {x, -2, 2}, AxesLabel -> {x, HoldForm[ ]}]
  4. In the blank space that I have left inside the HoldForm[ ], paste the copied equation.
  5. You will be asked if you want to wrap the TraditionalForm expression in a FormBox, and the answer is yes.
  6. Provided that the pasted expression obeys Mathematica syntax, you should now be able to evaluate the plot cell and get the output shown above.

Now in some cases you want to label a graph with a two-dimensional formula that doesn't obey Mathematica syntax, and in that case you would replace step 4. above by this:

Plot[f, {x, -2, 2}, AxesLabel -> {x,""}

Instead of HoldForm, I now left an empty string "" in the label. Now proceed as above with step 4, for example using an equation like

-(\[HBar]^2/(2m))\[PartialD]^2 \[Psi]

entered in a math inset (in a text cell as in step 1). If you tried this with HoldForm, it would give a syntax error because the \[PartialD] is being used in a mathematically acceptable but syntactically incorrect way.

Edit 2: the fastest way

The way I described copying and pasting of TraditionalForm into strings was based on my habits, but it's actually not the fastest. I should adjust my habits to the following: In the code for your plot, type a single string placeholder letter for your label, such as "y". Using the mouse, highlight the y in your string and go to the menu item Cell > Convert To > TraditionalForm (or use the keyboard shortcut). This creates the all-important FormBox. Since this box is invisible, you now have to use the arrow keys or mouse to get inside this FormBox, right next to to the placeholder y. From here, you can start typing any arbitrary formula which will then be typeset as TraditionalForm in the plot label.

So in conclusion, HoldForm is a very direct way of getting valid Mathematica expressions into labels without expanding them, and strings should be used in combination with the FormBox wrapper method above to typeset arbitrarily complicated labels.

share|improve this answer
    
Thanks Jens. Is HoldForm@InputForm the same as HoldForm[InputForm[...]]? –  user001 Mar 21 '12 at 6:32
    
Yes, it is indeed. –  Jens Mar 21 '12 at 6:46
add comment

You can use strings for the labels and Style them as you like — change colour/font/fontsize, etc. For example:

Plot[Sinc[x], {x, 0, 10}, PlotStyle -> Darker@Green, 
  AxesLabel -> (Style[#, Italic, Red, FontFamily -> "Verdana"] & /@ {"x","sinc(x)"})]

This will style only the labels and leave the tick marks intact.

enter image description here

share|improve this answer
    
Thanks R.M. A question about the syntax. The #, & seems to be a cooptation of the pure function syntax, whereby the {"x","sinc(x)"} are substituted into the #. How about the /@? This suggests to me an effect similar to /. # -> {"x","sinc(x)"}, but I'm not familiar with /@ per se. –  user001 Mar 21 '12 at 5:56
2  
@user001 /@ is shorthand for Map, which applies a function to each element in the list (loosely speaking). Strictly speaking, it applies to every element in the first level of the expression (doesn't necessarily have to be a list). See the linked documentation for more :) –  rm -rf Mar 21 '12 at 6:22
    
@R.M I noticed you left out the quotation marks around the Verdana font name. It needs to be "Verdana", otherwise it's not recognized and you get Helvetica. –  Jens Mar 21 '12 at 18:41
    
@Jens Oops! Thanks for catching that. –  rm -rf Mar 21 '12 at 18:46
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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