# Is there a more Mathematica-y way to label these plots?

I am studying an infinite square well in the context of quantum mechanics.

ClearAll["Global*"];

(* Length of the well *)
L = 1;

(* The eigenfunctions, n=1,2,3,... *)
u[n_, x_] := Sqrt[2/L] Sin[n π x / L]

(* Probability density function, ρ[x] = |u[n,x]|^2 *)
ρ[n_, x_] := Simplify[u[n, x]\[Conjugate] u[n, x], {n ∈ Integers, x ∈ Reals}]

(* Plots of ρ[n,x] for various values of n *)
Table[ Plot[ρ[n, x], {x, 0, L}, PlotLabel -> "ρ[" <> ToString@n <> ",x]"], {n, 1, 4}]


And here is the output:

My questions is:

Can I use Mathematica pattern magic to rewrite PlotLabel -> "ρ[" <> ToString@n <> ",x]" ?

For instance, I tried (although I knew it wouldn't work): PlotLabel -> HoldForm@ρ[n,x], but that of course leaves n unevaluated as well, although I would like n to be evaluated and then the result to be held.

-

I think your original method is fine, but perhaps this will be more to your liking:

Table[With[{n = ni},
Plot[ρ[n, x], {x, 0, L}, PlotLabel -> Defer@ρ[n, x]]
], {ni, 1, 4}]


Another, perhaps less fundamental way is

Table[
Plot[ρ[n, x], {x, 0, L}, PlotLegends -> Placed["Expressions", Top]]
,{n, 1, 4}]


-
Would be ok to use HoldForm[] instead of Defer[] here ? I tried it and it works, but are there any caveats? –  Zet Nov 6 '13 at 23:29
@Zet Yes, of course, HoldForm is probably even safer as Defer gets evaluated when given explicitly as input. –  Ajasja Nov 6 '13 at 23:36

This is probably more Mathematica-ish. But sincerely, I would go with your solution :D

Table[Plot[ReleaseHold@# /. n -> r, {x, 0, L},
PlotLabel -> (# /. n -> r)] &@HoldForm[ρ[n, x]], {r, 1, 4}]


-
Next time I will be very careful on what I'm wishing :P Thanks for the link and the solution ! –  Zet Nov 6 '13 at 23:13
I don't understand the Belisarius Superfluous Comma trick. It produces an error on my machine... –  cormullion Nov 8 '13 at 11:44
@cormullion Thanks, I did several mistakes in the previous version. It worked here, but probably only because some dirty previous definitions or a simple copy-paste error. –  belisarius Nov 8 '13 at 12:18
@belisarius Damn, I thought I had spotted another devious trick... –  cormullion Nov 8 '13 at 12:20
@cormullion What you've spotted is called stupidity by the locals here –  belisarius Nov 8 '13 at 12:22

I'm always a fan of making a new function for plotting any time I'm going to do the same type of plot multiple times.

\[Rho]plot[n_, plotopts : OptionsPattern[]] :=
Plot[\[Rho][n, x], {x, 0, L},
Evaluate[FilterRules[{plotopts}, Options[Plot]]],
PlotLabel -> "\[Rho][" <> IntegerString[n] <> ",x]"]


Now you can do a simple plot like

\[Rho]plot[1]


and get

or you can add any options to it to fancy it up:

Grid[Partition[
Table[\[Rho]plot[n, ImageSize -> 400, BaseStyle -> 20,
Frame -> True], {n, 4}]
, 2]]


-

To my simple mind all the approaches suggested so far are overkill. I would do it the simple way -- I would use Row.

Table[Plot[\[Rho][n, x], {x, 0, L},
PlotLabel -> Row[{"ρ[", n, ",\[ThinSpace]x]"}]], {n, 1, 4}]
`

Please take note of the use of a thin-space glyph. It is needed to make the spacing look right.

-