Message Boards Message Boards

0
|
6939 Views
|
2 Replies
|
1 Total Likes
View groups...
Share
Share this post:

How can I join two strings in a PlotLabel without formatting errors?

Posted 10 years ago

I am reviewing my old calculus book from college, Calculus and Analytic Geometry, 4th Ed. (by Sherman K. Stein). I started a notebook in MMA 10.0.2 to do the problems at the end of each section, if I can. See attached. (Not much there yet because I'm just getting started. My goal is to learn and have fun with Mathematica while reviewing calculus.)

To keep track of the problems, I am using PlotLabel to label the graph with the problem number and the equation. Rather than type out the text of the equation each time in the PlotLabel option (for example, PlotLabel->"f(x)=2/(x^2+1)", I discovered to my delight that I could simply type use PlotLabel->f[x]. However, now I'm missing the problem number and the "f(x)=".

No problem, I thought. I'll just join two text strings such as: PlotLabel->"9. f(x)="<>f[x]. But, since f[x] is not a string, I'm given a StringJoin error.

But, converting f[x] to a string (ToString[f[x]]) only results in misaligned plot label due to everything being represented by Courier font characters.

Finally, using PlotLabel->"9. f(x)="&&f[x] almost gives me what I want, but there is a wedge-like symbol inserted after the equal sign. I don't what the double ampersand means, and I don't know why it almost works, but I had tried that first as a hunch from my Visual BASIC days. (And, yes, you're right. I think that a single ampersand is how you join strings in VB. I accidentally put in two here.)

Can anyone please help me? Maybe there is a completely different and better way to do what I'm trying to achieve. See attachment. And thanks!

--Glen

Attachments:
POSTED BY: Glen Deering
2 Replies
Posted 10 years ago

I think you were almost there. Here's one way to do it:

f[x_] := 2/(1 + x^2); 
Plot[f[x], {x, -2, 2}, AspectRatio -> Automatic, 
 PlotLabel -> "9.  " <> ToString[TraditionalForm[f[x]]]]
POSTED BY: Jim Baldwin
Posted 10 years ago

That was too easy! Thanks so much!

--Glen

POSTED BY: Glen Deering
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract