Using a function name instead of its definition in AxesLabel
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:
- To get
f
instead of $x^2$ you should useHoldForm
, but that still allows the display ofTraditionalForm
shorthand forms for built-in symbols such asE
(which is Euler's constant but is pretty-printed as $\mathbb e$). - 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:
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 FormBox
es 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:
- Create the formula in a
TraditionalForm
environment. This could e.g. be in a Text cell by starting an equation withCtrl-(
and ending withCtrl-)
. - Copy this formula from within that math inset.
- Lay out your plot by typing something like
Plot[f, {x, -2, 2}, AxesLabel -> {x, HoldForm[ ]}]
- In the blank space that I have left inside the
HoldForm[ ]
, paste the copied equation. - You will be asked if you want to wrap the
TraditionalForm
expression in aFormBox
, and the answer is yes. - 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.
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.