编辑

编辑

为了便于说明,我想绘制三个函数:

  • 直接\sageplot调用线性,
  • 二次函数\sagestr
  • 正弦波\sageplot

但是,似乎没有\sagestr产生任何输出。此外,它破坏了后续代码的输出。哪里出了问题?

\documentclass{article}
\usepackage{sagetex}
\usepackage[a6paper,margin=2mm]{geometry}

\begin{document}
\section*{Linear}
\sageplot[width=4cm]{plot(x,-2,2)}

\section*{Quadratic}
\begin{sagesilent}
o = "\sageplot[width=4cm]{plot(x^2,-2,2)}"
\end{sagesilent}
% It does not produce plot but it sabotages one produced by the following.
\sagestr{o}

% the following output is sabotaged by the \sagestr above.
\section*{Sinusoidal}
\sageplot[width=4cm]{plot(sin(x),-2,2)}
\end{document}

在此处输入图片描述

编辑

实际上我想编译下面的代码,但是失败了。上面的代码是下面的代码中的最小代码。

\documentclass{article}
\usepackage{sagetex}
\begin{document}
%==================== BEGIN ===========================
\begin{sagesilent}
output = r""
for i in range(10,7,-1):
    f(x) = exp(x)*sin(i*x)
    output += r"The function is $f(x)= %s$."%(latex(f(x)))
    output += r" The second derivative of $f(x)$ is \[ \frac{\mathrm{d}^{2}}{\mathrm{d}x^{2}} %s = %s.\]"%(latex(f(x)),latex(diff(f, x, 2)(x)))
    output += r"Here's a plot of $f$ from $-1$ to $1$:\\"
    output += r"\begin{center}"
    output += r"\sageplot[width=8cm]{plot(%s,-1,1)}"%(f(x))
    output += r"\end{center}"
    output += r"\vspace{.15in}"
\end{sagesilent}
%=================== END ==============================
\sagestr{output}
\end{document}

答案1

第二个图的代码不正确。我们可以通过运行部分代码来发现这一点。运行第一个和第三个图时没有问题。 在此处输入图片描述

然而,运行第一和第二个会产生一个问题。

在此处输入图片描述

这显示在.sout文件中,我们可以看到第一个图的处理没有问题。

在此处输入图片描述

那么到底哪里出了问题?编辑:我错误地将字符串命令发布为第二张图中的问题,因为该命令在 Sage 运行后出现。我意识到那是错的,回过头来发现真正的问题:你使用了字符串,而不是原始字符串。需要原始字符串,因为字符串中有一个反斜杠。当我将字符串设为原始字符串时,代码就可以编译。

在此处输入图片描述

最后,您在 EDIT 中给出的代码(您说它不起作用)实际上起作用了;嗯,有点。代码没有问题,但是当我尝试使用 Cocalc 运行它时,我得到了错误,??我不知道为什么。我最终将行更改for i in range(10,7,-1):for i in range(10,6,-1):,然后一切正常。然后我将 6 改回 7,一切都正常了。很奇怪。

相关内容