1

For the sake of illustration, I want to plot three functions:

  • linear with direct \sageplot invocation,
  • quadratic with \sagestr
  • and sinusoidal with \sageplot

However, it seems that the \sagestr does not produce any output. Morever, it sabotages the output of the code that follows. What is wrong?

\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}

enter image description here

Edit

Actually I want to compile the following but I fails. The previous code above is the minimal of the following.

\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}
Display Name
  • 46,933
  • 2
    Did you just find that sage is a shorthand for sabotage? ;-) –  Jun 10 '20 at 06:46
  • @Schrödinger'scat: Yes. Exactly! – Display Name Jun 10 '20 at 06:48
  • I can't test it but I think you are using \sagestr wrong. The manual states that this function can be used for functions that return LaTeX code in Sage. The example file contains \sagestr{pascals_triangle(n)}, where pascals_triangle is a function that contains a lot of statements similar to s.append(r"\end{tabular}"). Your o variable on the other hand does not call a Sage function that produces LaTeX code, but the variable contains LaTeX code itself - and worse still, LaTeX code that actually calls Sage again. So it's all very confusing. – Marijn Jun 10 '20 at 15:34
  • Maybe you can make your use case a bit more clear? – Marijn Jun 10 '20 at 15:34
  • @Marijn: See my last edit. – Display Name Jun 10 '20 at 15:57
  • @MoneyOrientedProgrammer that looks a bit more like the way the function is used in the manual. However, still there is a \sageplot command in the output, which is not visible for the first pass of sagetex. So it would probably be more straightforward to write the for loop in LaTeX, and within that loop first call \sagestr for the four first lines of the loop, then call (back in the LaTeX command) the \sageplot part and then resume the LaTeX loop for the next plot. – Marijn Jun 10 '20 at 18:14

1 Answers1

1

The code for the second plot is incorrect. We can see that from running portions of the code. When the first and third plots are run there is no problem. enter image description here

However, running the first and second creates a problem.

enter image description here

This shows up in the .sout file where we can see the first plot was processed without issue.

enter image description here

So what went wrong? EDIT: I had mistakenly posted that the string command was the problem in the second diagram because of how the command occurs after Sage has run. I realized that was wrong, went back and found the real issue: you used a string, and not a raw string. A raw string is required because you have a backslash in the string. When I make the string a raw string, the code compiles.

enter image description here

Finally, the code you give in your EDIT, which you say doesn't works, actually works; well, sort of. There's nothing wrong with the code but when I tried running it using Cocalc I got ?? and I don't know why. I ended up changing the line for i in range(10,7,-1): to for i in range(10,6,-1): and the everything worked. Then I changed the 6 back to 7 and everything was fine. Weird.

DJP
  • 12,451
  • Thank you. My edit shows ?? even though I changed it to for i in range(10,6,-1):. That is why I said it does not work. – Display Name Jun 12 '20 at 02:10
  • 1
    I don't know why but I've gotten similar behavior recently. I have to delete all files except for the .tex file and/or change some of the lines of the code and recompile. I am able to get that code to run though it didn't initially. – DJP Jun 12 '20 at 02:16
  • I made a mistake yesterday. The issue is you used a string rather than a raw string. A raw string is required so that a backslash will be processed properly. – DJP Jun 12 '20 at 11:38
  • Even though I use raw string, I still get the same issue. – Display Name Jun 12 '20 at 13:18
  • If you aren't already, use Cocalc (so our two versions are identical) to process. Copy/paste your original code, add the r to make the string raw, save and compile the document. It compiles with ?? for pictures. Change, the domain of one of x^2 to go from -2 to 3. Save and compile. The document now shows the all pictures with x^2 actually going from -2 to 2. Like it's looking at the previous compilation. Do you get similar behavior? – DJP Jun 12 '20 at 21:00
  • Confirmed. The same behavior. The output uses the previous plot. – Display Name Jun 12 '20 at 22:27
  • 1
    I contacted Cocalc explaining the problem. Hopefully they will look into it and fix. – DJP Jun 12 '20 at 22:28
  • Thank you very much! – Display Name Jun 12 '20 at 22:29