5

The following example:

\documentclass{article}
\usepackage{sagetex}
\begin{document}
    \sage{0.1*0.1}
\end{document}

gives

which is totally undesirable.

A brief googling shows that Sage does recognize number formatting such as

How can I utilize such feature in LaTeX? Obviously I cannot write

    \sage{"%.3f"%(.1*.1)}

due to the special meaning of %.

Edit

This question is solved with two independent answers. However, I would like to know if there is any official way to assign formatting once and for all. Maybe something like

\begin{sagesilent}
    please print only two digits thank you
\end{sagesilent}

and

\begin{sagesilent}
    from now on ten digits please
\end{sagesilent}

Just like we use \pgfkeys{/pgf/number format/foo=bar} to configure \pgfmathprintnumber.

  • manual says: use \percent for that purpose. manual found here: ftp.rrzn.uni-hannover.de/pub/mirror/tex-archive/macros/latex/… – naphaneal Jun 21 '16 at 13:12
  • @naphaneal Oops I did not see that coming. Would you like to turn it to an answer? (Copy and paste your comment and I will do the rest.) – Symbol 1 Jun 21 '16 at 13:41
  • You can use .n(digits= ) to set the number of digits you need. See for example my answers here or here or here and elseswhere. – DJP Jun 21 '16 at 13:48
  • @DJP This seems to be more pythonic. Would you mind to make it an answer and/or give me a reference to such feature. – Symbol 1 Jun 21 '16 at 16:40
  • Just a note: naphaneal's link is to a very old version of the manual. It's correct, but since SageTeX isn't distributed as part of CTAN anymore, it's best to get documentation from your Sage install or the Sage website. – Dan Drake Jun 24 '16 at 2:31
4

As the manual says: use \percent for that purpose.

Quote from the manual:

The \percent macro makes everyone happy.

For example

\documentclass{article}
\usepackage{sagetex}
\begin{document}
    \sage{"\percent.3f"\percent(.1*.1)}    
\end{document}

gives

5

One of the advantages of using sagetex is that you get Python plus all the other mathematical ability such as graph theory, built in functions, and more. The documentation here explains how to get decimal digits of precision by using .n(digits=k) to get k decimal digits. Note that digits=1 is an exceptional case.

\documentclass[12point]{article}%
\usepackage{sagetex}%  use Sage for it's math ability
\pagestyle{empty} % remove the page numbers
\begin{document}
The documentation says ``As an exceptional case, digits $=1$ usually    leads 
to $2$ digits (one significant) in the decimal output''\\\\
$\pi \approx \sage{pi.n(digits=2)}$ or \\
$\pi \approx \sage{pi.n(digits=3)}$ or \\
$\pi \approx \sage{pi.n(digits=8)}$ or, if you must, \\
$\pi \approx \sage{pi.n(digits=50)}$\\\\
You change the digits in the calculation so that $(0.1)(0.1)$ is 
$\sage{(.1*.1).n(digits=2)}$..
\end{document}

The output is below: enter image description here

I'm not sure why digits=2 is giving the 0 at the end, though.

  • A experiment shows that (0.00001).n(digits=2) gives 0.000010. It seems the digits-counting is done with respect to scientific notation 1.0E⁻⁵. It makes more sense, as they are floating-point numbers, not fixed-point numbers. – Symbol 1 Jun 22 '16 at 5:54
  • Unfortunately I phrased the question in such a way that I have to accept @naphaneal's answer. But as I said, this answer is more pythonic and I really appreciate it. – Symbol 1 Jun 22 '16 at 6:09
5

Others have mentioned \percent, and the use of .n(), but I wanted to mention a third way which is a little silly but the method is sometimes useful for more complex formatting. Try something like this:

\begin{sagesilent}
def twodigits(x):
    return "%.3f" % x 

# or, more in the Python 3 spirit:
def threedigits(x):
    return "{:.3f}".format(float(x))
\end{sagesilent}

Then, in your text, do

blah $\sage{twodigits(.1 * .1)}$ blah

There's no "official" way to declare that all numbers be displayed to a certain number of decimal places, but in your document you can define such a function and consistently use it to achieve the same result.

  • Very straightforward! Currently I am using \def\mysage#1{\sage{foo #1 bar}}. I guess doing the job in sage makes more sense. – Symbol 1 Jun 24 '16 at 7:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.