ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Thu, 13 Jan 2022 00:11:36 +0100SageMath plot: Axis label only partly in LaTeX style. Why?https://ask.sagemath.org/question/60622/sagemath-plot-axis-label-only-partly-in-latex-style-why/For bigger scripts it makes sense to define functions outside of the interact def function. Here it is the function g(), which is defined before the def function. I'd like to use this function name as argument for both plot() and as argument for setting the label of the y-axis.
- This works fine as plot argument: `plot( g(a,x), ... )`
- For the y-axis I can use g() and get the correct expression, but its shown in bold.
How can I manage to get the italic latex style for this y-axis label?
Remark: The usual way to set the label of the y-axis ( '$a*x^3$' )
works well, but this is by far not as flexible as using ( eval('g()') ), because g can be changed.
Do you have any hint, how `eval('g()')` in `p.axes_labels` can be handled in a way, that the y-axis also has a latex style font?
Here is a working minimal code exmple:
var('a, x')
g(a,x) = a*x^3
@interact
def _(a=slider([-5..5], default=2, label='Param a: ')):
p = plot(g(a,x),(x,-3,3), color='purple')
# p.axes_labels([ '$x$', '$a*x^3$' ])
p.axes_labels([ '$x$', eval('g()') ])Tue, 11 Jan 2022 12:21:15 +0100https://ask.sagemath.org/question/60622/sagemath-plot-axis-label-only-partly-in-latex-style-why/Answer by rburing for <p>For bigger scripts it makes sense to define functions outside of the interact def function. Here it is the function g(), which is defined before the def function. I'd like to use this function name as argument for both plot() and as argument for setting the label of the y-axis.</p>
<ul>
<li>This works fine as plot argument: <code>plot( g(a,x), ... )</code></li>
<li>For the y-axis I can use g() and get the correct expression, but its shown in bold.
How can I manage to get the italic latex style for this y-axis label?
Remark: The usual way to set the label of the y-axis ( '$a*x^3$' )
works well, but this is by far not as flexible as using ( eval('g()') ), because g can be changed.</li>
</ul>
<p>Do you have any hint, how <code>eval('g()')</code> in <code>p.axes_labels</code> can be handled in a way, that the y-axis also has a latex style font?</p>
<p>Here is a working minimal code exmple: </p>
<pre><code>var('a, x')
g(a,x) = a*x^3
@interact
def _(a=slider([-5..5], default=2, label='Param a: ')):
p = plot(g(a,x),(x,-3,3), color='purple')
# p.axes_labels([ '$x$', '$a*x^3$' ])
p.axes_labels([ '$x$', eval('g()') ])
</code></pre>
https://ask.sagemath.org/question/60622/sagemath-plot-axis-label-only-partly-in-latex-style-why/?answer=60624#post-id-60624You can do this to achieve what you want:
p.axes_labels([ '$x$', '$'+latex(g())+'$' ])Tue, 11 Jan 2022 14:04:47 +0100https://ask.sagemath.org/question/60622/sagemath-plot-axis-label-only-partly-in-latex-style-why/?answer=60624#post-id-60624Comment by be for <p>You can do this to achieve what you want:</p>
<pre><code>p.axes_labels([ '$x$', '$'+latex(g())+'$' ])
</code></pre>
https://ask.sagemath.org/question/60622/sagemath-plot-axis-label-only-partly-in-latex-style-why/?comment=60655#post-id-60655Thanks a lot -- this works like a charm.
I was not aware of this trick: to concatenate the string for the label from several partly strings. But afterwards it's clear and pure Python syntax.
Thanks again, this helped to make the real sample for my students look much better.Thu, 13 Jan 2022 00:11:36 +0100https://ask.sagemath.org/question/60622/sagemath-plot-axis-label-only-partly-in-latex-style-why/?comment=60655#post-id-60655