3
$\begingroup$

In short, the following Mathematica code

Manipulate[
  Plot[a Sin[x], {x, -Pi, Pi}], 
  {a, -1, 1}, 
  FrameLabel -> TraditionalForm[y == a Sin[x]]]

produces a plot with the (obviously wrong) label

$\textrm{y}=\textrm{a}\$\$\sin(\textrm{x})$.

I am trying to use this in a function which plots and automatically labels other functions, so separating the manipulated variable somehow in the label is not an option.

$\endgroup$
  • $\begingroup$ Try using TraditionalForm[y == Dynamic[a] Sin[x]] instead. $\endgroup$ – Karsten 7. Aug 23 '16 at 0:52
  • $\begingroup$ I am trying to use this in a function which plots and automatically labels other functions, so separating the manipulated variable somehow in the label is not an option. $\endgroup$ – Fibonacci Aug 23 '16 at 0:55
  • $\begingroup$ What do you want the label to look like? What about TraditionalForm[y == "a" Sin[x]]? $\endgroup$ – Karsten 7. Aug 23 '16 at 0:58
  • 1
    $\begingroup$ I think it's unclear what would be obviously right.... $\endgroup$ – Michael E2 Aug 23 '16 at 1:17
  • 1
    $\begingroup$ FrameLabel -> (TraditionalForm[y == a Sin[x]] /. a -> ToString[a]) works. $\endgroup$ – Fibonacci Aug 23 '16 at 4:25
4
$\begingroup$

If using an other, modified input expression for TraditionalForm is not an option and you want the label to look like

TraditionalForm[y == a Sin[x]]

png1

then using a replacement rule, that will replace a$$ with a is an alternative

Manipulate[Plot[a Sin[x], {x, -Pi, Pi}], {a, -1, 1}, 
 FrameLabel -> (TraditionalForm[y == a Sin[x]] /. a -> ToString[a, TraditionalForm])]

png2

This approach also works in usage scenarios like

Table[
 With[{expr = expr},
  Manipulate[Plot[expr, {x, -Pi, Pi}], {a, -1, 1}, 
   FrameLabel -> (TraditionalForm[expr] /. 
      a -> ToString[a, TraditionalForm])]], {expr, {a Sin[x], a Cos[a x]}}] // Column

png3

or

TabView[Table[
  With[{expr = expr}, 
   Manipulate[Plot[expr, {x, -Pi, Pi}], {a, -1, 1}, 
    FrameLabel -> (TraditionalForm[expr] /. 
       a -> ToString[a, TraditionalForm])]], {expr, {a Sin[x], a Cos[a x]}}]]

png4

$\endgroup$
4
$\begingroup$

Manipulate automatically wraps 1st argument in DynamicModule with the variables defined in its control specifications as the localized dynamic variables. But expressions in its options list are not so treated. So you have to manually make them dynamic.

Manipulate[
  Plot[a Sin[x], {x, -Pi, Pi}],
  {a, -1, 1},
  FrameLabel -> Dynamic @ TraditionalForm @ Style[y == a Sin[x], 14]] 

plot

Your question isn't clear about how you actually expected the label to look so perhaps what you really want is this;

Manipulate[
  Plot[a Sin[x], {x, -Pi, Pi}],
  {a, -1, 1},
  FrameLabel -> TraditionalForm @ Style[y == "a" Sin[x], 14]]

plot

If you require the internal localized variable name to be delocalized programatically, then it can be done by

SetAttributes[delocalize, HoldFirst]
delocalize[symbl_Symbol] := 
  First @ StringSplit[SymbolName[Unevaluated @ symbl], "$"]

Manipulate[
  Plot[a Sin[x], {x, -Pi, Pi}],
  {a, -1, 1},
  FrameLabel -> TraditionalForm @ Style[y == delocalize[a] Sin[x], 14]]

plot

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.