Ask Your Question

SageMath plot: Axis label only partly in LaTeX style. Why?

asked 2022-01-11 12:21:15 +0100

be gravatar image

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
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()') ])
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2022-01-11 14:04:47 +0100

rburing gravatar image

You can do this to achieve what you want:

p.axes_labels([ '$x$', '$'+latex(g())+'$' ])
edit flag offensive delete link more



Thanks 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.

be gravatar imagebe ( 2022-01-13 00:11:36 +0100 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2022-01-11 12:21:15 +0100

Seen: 29 times

Last updated: Jan 11