Here's the question: how do I get the following to work in SageTeX? The quotes always lead to an error.
\sageplot{plot(cos(x),(x,0,pi),label='$\cos(x)$')}
Double quotes, escaping them, ... nothing seems to work.
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It only takes a minute to sign up.
Sign up to join this communityHere's the question: how do I get the following to work in SageTeX? The quotes always lead to an error.
\sageplot{plot(cos(x),(x,0,pi),label='$\cos(x)$')}
Double quotes, escaping them, ... nothing seems to work.
I don't know enough to tell you what's going on, but this works:
\documentclass{article}
\usepackage{sagetex}
\begin{document}
\pagestyle{empty}
\begin{sagesilent}
G=Graphics()
var('x')
G+=plot(cos(x),(x,0,pi),label='$\cos(x)$')
\end{sagesilent}
\[ \sageplot{G}\]
\end{document}
Note, to get even more control over the placement of your text label try this:
\documentclass{article}
\usepackage{sagetex}
\begin{document}
\pagestyle{empty}
\begin{sagesilent}
G=Graphics()
H=Graphics()
var('x')
G+=plot(cos(x),(x,0,pi))
H+= text("$\cos(x)$", (3, 1), fontsize=16, color='black')
\end{sagesilent}
\[ \sageplot{G+H}\]
\end{document}
The people at AskSage will know what's best. Here's output from the first set of code:
Graphics()
and var
incantations and just directly do G = plot...
, since x
is predefined and all plots begin with empty graphics objects.
%!TEX TS-program = sage
and it was during the LaTeX stage that I had problems; it never actually got to Sage. I assumed this was a SageTeX problem but apparently that was mistaken.
It's not the quotes that are the problem here. If you used 'cos(x)'
, it will work. The real problem is the dollar signs and the \cos
: TeX is interpreting stuff before \sageplot
. With the dollar signs, here's what ends up in your .sagetex.sage
file:
_st_.plot(0, format='notprovided', _p_=plot(cos(x),(x,0,pi),label='$\mathop {\mathgroup \symoperators cos}\nolimits (x)$'))
In this case, SageTeX does exactly what you (unintentionally) asked: it includes the above text as a label. And since TeX does this before \sageplot
sees anything, there's nothing SageTex can do.
The above solution with sagesilent
is more complicated than necessary, but works because the dollar signs and backslashes inside sagesilent
are passed directly to the .sagetex.sage
file. Here's a simpler way:
\begin{sagesilent}
P = plot(cos(x),(x,0,pi),label='$\cos(x)$')
\end{sagesilent}
\sageplot[width=.5\textwidth]{P}