15

Superscripts can be included in axis labels by the use of the caret symbol (e.g., Plot[x^2, {x, -1, 1}, AxesLabel -> {x, x^2}]).

How does one include subscripts in an axis label? The use of the underscore character does not seem to work (e.g., Plot[f, {x, -1, 1}, AxesLabel -> {x, f_i(x)}]).

user001
  • 1,397
  • 3
  • 14
  • 19

4 Answers4

15

There is a subtle problem if you use strings as axis labels. Look closely at a plot like this:

Plot[f, {x, -1, 1}, AxesLabel -> {x, "\!\(\*SubscriptBox[\(f\), \(i\)]\)(x)"}]

You will see that the argument x in the function appears in a different font style than the argument on the horizontal axis.

To make sure that you get a consistent font style on both axes using the default styling for graphics, you should use the following:

Plot[f, {x, -1, 1}, AxesLabel -> {x, HoldForm[Subscript[f, i][x]]}]

Of course, to enter the Subscript expression in your label, you can still use the keyboard shortcuts that are mentioned by @Szabolcs.

The argument in HoldForm is your vertical label, typed in actual Mathematica syntax with square brackets for the function argument. I didn't surround the horizontal label in HoldForm, but you could do that for safety, in particular if your variable x has been assigned a value somewhere else in the Notebook.

Jens
  • 97,245
  • 7
  • 213
  • 499
6

Enclose the label in quotation marks for safety (just in case any symbols you use in them are defined), and enter two-dimensional input. Check the documentation on how to do it. You can enter a subscript using Ctrl-_ or Ctrl--. The Basic Math Assistant palette can be useful as well---it has a Typesetting section.

Szabolcs
  • 234,956
  • 30
  • 623
  • 1,263
  • oooh, how do you do the nice CTRL commands? – tkott Mar 15 '12 at 18:44
  • @tkott Click the edit link to see ;-) It was really useful here because CTRL-- isn't very clear... Maybe CTRL-[-] – Szabolcs Mar 15 '12 at 18:44
  • smacks head thanks :) – tkott Mar 15 '12 at 18:45
  • Despite the fact than I ran out of votes hours ago I keep trying to vote. It's become a habit. – Mr.Wizard Mar 15 '12 at 18:46
  • Thanks. When I enclose the label in quotation marks, it romanizes the text. Can I force it to remain italic? If I try LabelStyle->{Italic}, the tick labels are italicized in addition to the axis labels. I prefer for the axis labels to be italic and the tick labels to be roman. – user001 Mar 15 '12 at 18:55
  • @user001 I usually just select it and press Ctrl-I. If you have a subscript, there's no need for that. I'm open to hearing better suggestions, as I use this often too. – Szabolcs Mar 15 '12 at 18:56
  • Hmm, my plot shows the f and i as italic, and only the (x) part as not-italic – tkott Mar 15 '12 at 18:58
3

That's because _ is only a subscript in LaTeX world. In MMA, you can simply use CTRL+-. The following should show you what I mean (once you copy it into MMA):

Plot[Sin[x], {x, -1, 1}, 
 AxesLabel -> {x, "\!\(\*SubscriptBox[\(f\), \(i\)]\)[x]"}]

Note that the reason that x^2 shows up like it does is because ^2 is automatically changed to MMA's boxes / display format. If you were to write:

Plot[Sin[x], {x, -1, 1}, AxesLabel -> {x, "x^2"}]

You'll notice the caret stick around.

tkott
  • 4,939
  • 25
  • 44
1

Another option is to create the label with strings:

Plot[f, {x, -1, 1},
 AxesLabel -> {x, 
   Style[Row[{Subscript["f", "i"], "(x)"}], FontFamily -> "Times", 12,
     Italic]}]
Verbeia
  • 34,233
  • 9
  • 109
  • 224
Mike Honeychurch
  • 37,541
  • 3
  • 85
  • 158
  • This works fine in most simply labels, but it quickly gets ugly when you have actual Mathematica expressions as labels. Just replace the vertical label by Sin[x] and ask how you'd format that in a string so that it doesn't come out as sin(x) but sin(x). This is just a simple example - the point is that it's much better to let the default TraditionalForm in plot labels do the work for you. – Jens Mar 15 '12 at 21:02
  • I agree @Jens but the question related to a simple subscript. – Mike Honeychurch Mar 15 '12 at 21:04