Using a function name instead of its definition in AxesLabel

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.

-

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.

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:

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.

-
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
@Jens, I was trying to reproduce what you did and I realized that it does not work in Mathematica 10 but it does in Mathematica 9. Similarly, Plot[x, {x, -3, 3}, PlotLegends -> Text[TraditionalForm[x]], Epilog -> Text[x, {3, 1}]] puts both x's in TraditionalForm in version 9 but not in version 10. Is this supposed to be the case? Is there another work around? –  Ben Allgeier Jul 21 '14 at 20:54
@BenAllgeier I also see this problem, and I think it's a bug in version 10. I'm currently mostly using version 8 because I want to wait until the bugs are fixed. In the meantime, maybe you can instead use my answer here, until we figure out what's wrong with HoldForm. I'll update this when I know more... –  Jens Jul 21 '14 at 21:39
@Jens, I could not get it to work in version 10. I am not sure I mastered your technique from your other posts. How did you get \!$$\*UnderoverscriptBox[\(\[Sum]$$, $$i = 1$$, $$n$$] \*SubscriptBox[$$x$$, $$i$$]\)/\!$$\*UnderoverscriptBox[\(\[Sum]$$, $$i = 1$$, $$n$$] \*SubscriptBox[$$y$$, $$i$$]\) == \!$$\*UnderoverscriptBox[\(\[Sum]$$, $$i = 1$$, $$n$$]$$(\(( \*FractionBox[ SubscriptBox[\(x$$, $$i$$], SubscriptBox[$$y$$, $$i$$]])\) p)\)\)/n Magnify[%] // TraditionalForm from here –  Ben Allgeier Jul 21 '14 at 23:47

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.

-
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
@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 :) –  R. M. 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. –  R. M. Mar 21 '12 at 18:46