sagetex - result with comma as decimal separator

307 views
Skip to first unread message

iDan

unread,
Mar 23, 2010, 10:25:29 AM3/23/10
to sage-support
Hi,
In french, we use comma as decimal separator. How can I convert the
result of the instruction \sage{} (from sagetex package) ?

%!TEX TS-program = sage
\documentclass{article}
\usepackage[latin1]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[francais]{babel}
\usepackage{numprint}
\usepackage{sagetex}
\begin{document}
$\sage{sqrt(3.)}$ % Output is 1.73205080756888, but I would like
1,7320...
$\mathrm{\sage{sqrt(3.)}}$ % Output is 1.73205080756888
$\numprint{12.34567}$ % Ok, outpout is 12,345 67 (with comma)
%$\numprint{\sage{sqrt(3.)}}$ % don't work (bad argument for
numprint)
%\numprint{$\sage{sqrt(3.)}$} % don't work (bad argument for
numprint)
%\numprint{$\mathrm{\sage{sqrt(3.)}}$} % don't work (Generic Error)
\end{document}

Thanks
iDan

Dan Drake

unread,
Mar 23, 2010, 8:34:52 PM3/23/10
to sage-s...@googlegroups.com
On Tue, 23 Mar 2010 at 07:25AM -0700, iDan wrote:
> Hi,
> In french, we use comma as decimal separator. How can I convert the
> result of the instruction \sage{} (from sagetex package) ?

Hrm, unfortunately, I don't think this is possible right now.

You can have Sage insert a comma in this way:

sage: x = 1.234
sage: x.str().replace('.', ',')
'1,23400000000000'

so in your TeX file, you could do

$\sage{(sqrt(3)).str().replace('.', ',')}$

which is not very pleasant.

You could define a small function which makes this nicer:

\begin{sagesilent}
def numprint(x):
return x.str().replace('.', ',')
\end{sagesilent}

and then use \sage{numprint(x)}, which is a bit nicer.

Python has a bit of support for locale-dependent printing, which I
should go investigate.

I think the real long-term solution is to change the str() method to
allow commas as the decimal separator and create a configuration option
so that a user can say "use commas always".

Dan

--
--- Dan Drake
----- http://mathsci.kaist.ac.kr/~drake
-------

signature.asc

Dan Drake

unread,
Mar 23, 2010, 10:07:12 PM3/23/10
to sage-s...@googlegroups.com
On Tue, 23 Mar 2010 at 07:25AM -0700, iDan wrote:
> In french, we use comma as decimal separator. How can I convert the
> result of the instruction \sage{} (from sagetex package) ?

I've created http://trac.sagemath.org/sage_trac/ticket/8594. Hopefully
we'll include support for this in Sage sometime soon.

signature.asc

ma...@mendelu.cz

unread,
Mar 24, 2010, 7:00:35 AM3/24/10
to sage-support

On 24 bře, 01:34, Dan Drake <dr...@kaist.edu> wrote:
> On Tue, 23 Mar 2010 at 07:25AM -0700, iDan wrote:
> > Hi,
> > In french, we use comma as decimal separator. How can I convert the
> > result of the instruction \sage{} (from sagetex package) ?
>
> Hrm, unfortunately, I don't think this is possible right now.
>
> You can have Sage insert a comma in this way:
>
> sage: x = 1.234
> sage: x.str().replace('.', ',')
> '1,23400000000000'
>
> so in your TeX file, you could do
>
> $\sage{(sqrt(3)).str().replace('.', ',')}$
>
> which is not very pleasant.

And note the difference between $12,13212$ and $12{,}13212$ - the
first one has incorrect spacing in TeX!

Robert

btw: Anyway, we use also comma as separator for decimal places and it
is very uncomfortable in many other situations. My suggestion is to
switch the separator used in France and Czech from comma to dot :)

iDan

unread,
Mar 24, 2010, 2:14:46 PM3/24/10
to sage-support
On 23 mar, 20:34, Dan Drake <dr...@kaist.edu> wrote:

>
> \begin{sagesilent}
>   def numprint(x):
>     return x.str().replace('.', ',')
> \end{sagesilent}
>
> and then use \sage{numprint(x)}, which is a bit nicer.

Hi,
Thanks for your suggestion. But,
- this function change the number of significant figures
- \sage{numprint(1.234)} is printed like \texttt{1,234}

Daniel
P.S. : Thanks for the ticket (http://trac.sagemath.org/sage_trac/
ticket/8594)


iDan

unread,
Mar 24, 2010, 2:31:22 PM3/24/10
to sage-support
On 24 mar, 07:00, "ma...@mendelu.cz" <ma...@mendelu.cz> wrote:
> And note the difference between $12,13212$ and $12{,}13212$  - the
> first one has incorrect spacing in TeX!

Hi Robert,
You are right, but there is no difference between $12{,}345$, $
\numprint{12,345}$ and $\DecimalMathComma 12,345$
If you don't have vectors in your document, you can use
\everymath{\DecimalMathComma} and then, $12{,}345$ is the same as
$12,345$.

> <...> My suggestion is to


> switch the separator used in France and Czech from comma to dot :)

Haha, hum... heu... maybe :-) Also
it would be preferable that we all speak chinese,
that we have all blue skin,
that we have all the same religion,
and, finally, that we all use MacOSX :-)

Daniel

Mike Hansen

unread,
Mar 24, 2010, 2:38:25 PM3/24/10
to sage-s...@googlegroups.com
If the locale is properly set, we can get this information from

sage: locale.localeconv()['decimal_point']
'.'

--Mike

Oscar

unread,
Mar 24, 2010, 2:55:04 PM3/24/10
to sage-support
I agree. Ideally, sagetex would work with the numprint package and the
different language packages for babel,so that users of sagetex and
those packages wouldn't need to do anything different. Or, as I think
you imply, an option within sagetex.

Oscar

YvesD

unread,
Jul 23, 2011, 5:22:24 AM7/23/11
to sage-s...@googlegroups.com
Hello,
As a belgian french speaking user of sagetex and a mathematics & physics teacher I needed a "french formated" output.
Indeed,with the above method, the output is then a string. This anoying if you have more complex output (scientific notation, ...)

If you are interested, here is my personnal solution:
It goes through "sagestr":

# Pour adapter la sortie LaTeX de sage aux conventions francaises.
def numprint(x):
x = latex(x)
x = str(x)
x = x.replace('.', ',\!').replace('\\times','\, \ldotp')
x = sage.misc.latex.LatexExpr(x)
return x

#and use \sagestr{numprint(x))}


I join a demo:
demoGenInt000.pdf and demoGenInt000.tex

If you find this useful, thank you to let me know.
(I am new to the group and made a mistake yesterday, sorry for those of you who received this message more than once.)

Yours
--------------------------------------
Yves Delhaye
http://YvesDelhaye.be/

Le binaire divise l'humanité en 10:
Ceux qui le comprennent et les autres.
--------------------------------------

demoGenInt000.pdf
demoGenInt000.tex

Felipo Bacani

unread,
May 3, 2012, 8:50:39 PM5/3/12
to sage-s...@googlegroups.com
Is there a way that *every* printed number (and not only the ones that one uses "replace" ou "locale.format" ) have a comma as a separator for decimals?
I ask this because I can't make the ticks of my graphics to print in this format.


Thanks.
BTW, there's a ticket on this:
http://trac.sagemath.org/sage_trac/ticket/8594
There's no milestone for it =/

YvesD

unread,
Jan 26, 2014, 12:14:42 PM1/26/14
to sage-s...@googlegroups.com
I just updated my way of replacing the dot with a comma.
Just after the "\ begin{document}", I insert this:

\begin{sagesilent}
 #   Pour adapter la sortie LaTeX de sage aux conventions francaises.
  def numprint(x):
     x = latex(x)
     x = str(x)
     x = x.replace('.', ',\!').replace('\\times','\, \ldotp')
     x = sage.misc.latex.LatexExpr(x)
     return x
       
  #and use \sagestr{numprint(x)}
\end{sagesilent}
\newcommand{\SageVirgule}[1]{\sagestr{numprint(RDF(#1))}}


"Virgule" is comma in french.
The RDF is not mandatory: it is just convenient for me!
Sorry, I don't output enough graphics for me to search a solution to your problem.
Reply all
Reply to author
Forward
0 new messages