1

Suppose you want to print pi with 2 decimal digits. The following code does not work

$\sage{"%0.2f"%pi}$

What should be used instead?

  • 1
    in which way it doesn't work in terms of making the question clearer? – percusse Jul 11 '15 at 19:08
  • I get error when I latex the source. Probably the character % is interpreted as a comment by the latex compiler. – Primo Petri Jul 11 '15 at 20:46
  • Does this $\sage{"\%0.2f"\%pi}$ work maybe by escaping the percent chars? If this is Python then maybe you can also use the new string formatting $\sage{'{0:.2f}'.format(pi)}$ – percusse Jul 11 '15 at 20:52
  • @PrimoPetri You are getting answers which use non-sagetex methods e.g. using Lua or PGF. I think if you could make it more obvious that you want a sagetex solution, that would be helpful. The 'What should be used instead?' is being understood as 'I'm open to non-sagetex solutions'. Alternatively, if you are open to such solutions, of course make that clear instead. – cfr Jul 13 '15 at 03:00
  • This is presumably calling SAGE from pdflatex (or whatever you use to process your file), and that isn't allowed by default (for security reasons). – vonbrand Aug 07 '15 at 14:55

3 Answers3

2

Try this:

\documentclass{article}
\usepackage{sagetex}
\begin{document}
\noindent Print $\pi$ as a number: $\sage{pi.n(digits=3)}$\\
Print $\pi$ as a string: \sagestr{str(pi.n(digits=3))}
\end{document}

Here is the output in Sagemath Cloud: enter image description here

As I mention here there are issues with digits and using sage that I don't understand. Using sagestr has been better for me.

DJP
  • 12,451
2

DJP's answer is good; I will add that "pi" in Sage is not a float; it is a (Sage/Python) object that represents the real number pi.

You did not describe the error, but I believe what is going wrong is that LaTeX is interpreting the % as a comment character. There is a way to use your string formatting method, but the idiomatic way to get a decimal approximation in Sage is to use the .n() method. Here are some examples:

$\sage{pi.n()}$

$\sage{pi.n(10)}$ which returns an approximation with 10 bits of accuracy

$\sage{pi.n(digits=10)}$ which returns an approximation accurate to 10 significant figures

$\sage{pi.n(digits=10000)}$ if you want to fill several screens full of digits

If you use Sage, you really should get used to using .n() on various things. Open a worksheet on SageMathCloud and try it on things like pi, sqrt(5), e, log(123), and so on. Also feel free to ask questions on the psage-support Google Group](https://groups.google.com/forum/#!forum/sage-support).

Dan Drake
  • 1,364
1

Can this help?

\documentclass[tikz,border=10pt]{standalone}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}

\pgfmathsetmacro \pi {pi}

\node at (0,0) {\pgfmathprintnumber[fixed,precision=2]{\pi}};
\node at (0,-1) {\pgfmathprintnumber[fixed,precision=4]{\pi}};
\node at (0,-2) {\pgfmathprintnumber[fixed,precision=6]{\pi}};

\end{tikzpicture}
\end{document} 

enter image description here

fluxmodel
  • 695
  • 4
  • 15
  • I believe that the OP wants to know what to use in sagetex rather than asking about an entirely different method. You are the second person to offer an answer of this kind so I think maybe the question should be clarified. – cfr Jul 13 '15 at 02:58
  • Sorry about that. Didnt notice. Perhaps someone else searching for a similar answer might find my contribution useful. – fluxmodel Jul 13 '15 at 08:08