ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Fri, 07 Feb 2020 07:27:50 +0100Spanish numbers using LaTeXhttps://ask.sagemath.org/question/47018/spanish-numbers-using-latex/Dear SageMath community,
I am currently writing a LaTeX document in Spanish that relies heavily on sageTeX for computations. The rules for writing numbers in Spanish are a little different from English ones. For example, we are prohibited from using commas to separate every 1000 factor, instead using a small space, only if the number is longer than four digits; otherwise, no sign should be used. On the other hand, the decimal point is actually a decimal comma. For example,
- 1,000,000.25 should be written as 1 000 000,25
- 123,456,789.123456789 should be written as 123 456 789,123 456 789
- 4,000 should be written as 4000
Using sageTeX, SageMath's `latex` command, and LaTeX babel package, it is easy to replace the decimal point with a decimal comma. It is also easy to eliminate the commas for every 1000 factor. However, I haven't been able to put a small space instead.
I could use the `siunitx` LaTeX package, which has a command, `\num`, that does exactly this processing. However, it doesn't work with sageTeX's `\sage` command or any of it's environments. The only solution I can find is to apply the `\num` command directly to every number generated by SageMath via the `latex` command.
My problem is: I don't know how to do this, specially in cases where numbers are entries of a matrix, or the coefficients of a polynomial. So, **how could I apply the `\num` command to every number independently using the `latex` command?**
Thanks in advance for your answers! Any alternative approach is also welcomed.Sun, 30 Jun 2019 06:08:29 +0200https://ask.sagemath.org/question/47018/spanish-numbers-using-latex/Comment by heluani for <p>Dear SageMath community,</p>
<p>I am currently writing a LaTeX document in Spanish that relies heavily on sageTeX for computations. The rules for writing numbers in Spanish are a little different from English ones. For example, we are prohibited from using commas to separate every 1000 factor, instead using a small space, only if the number is longer than four digits; otherwise, no sign should be used. On the other hand, the decimal point is actually a decimal comma. For example,</p>
<ul>
<li>1,000,000.25 should be written as 1 000 000,25</li>
<li>123,456,789.123456789 should be written as 123 456 789,123 456 789</li>
<li>4,000 should be written as 4000</li>
</ul>
<p>Using sageTeX, SageMath's <code>latex</code> command, and LaTeX babel package, it is easy to replace the decimal point with a decimal comma. It is also easy to eliminate the commas for every 1000 factor. However, I haven't been able to put a small space instead.</p>
<p>I could use the <code>siunitx</code> LaTeX package, which has a command, <code>\num</code>, that does exactly this processing. However, it doesn't work with sageTeX's <code>\sage</code> command or any of it's environments. The only solution I can find is to apply the <code>\num</code> command directly to every number generated by SageMath via the <code>latex</code> command.</p>
<p>My problem is: I don't know how to do this, specially in cases where numbers are entries of a matrix, or the coefficients of a polynomial. So, <strong>how could I apply the <code>\num</code> command to every number independently using the <code>latex</code> command?</strong></p>
<p>Thanks in advance for your answers! Any alternative approach is also welcomed.</p>
https://ask.sagemath.org/question/47018/spanish-numbers-using-latex/?comment=49787#post-id-49787Thank you for pointing this Spanish rule. I was taught in school to use dots to separate every three digits. Apparently this has changed since http://lema.rae.es/dpd/srv/search?id=QHaq7I8KrD6FQAyXTSTue, 04 Feb 2020 22:53:35 +0100https://ask.sagemath.org/question/47018/spanish-numbers-using-latex/?comment=49787#post-id-49787Answer by sononicola for <p>Dear SageMath community,</p>
<p>I am currently writing a LaTeX document in Spanish that relies heavily on sageTeX for computations. The rules for writing numbers in Spanish are a little different from English ones. For example, we are prohibited from using commas to separate every 1000 factor, instead using a small space, only if the number is longer than four digits; otherwise, no sign should be used. On the other hand, the decimal point is actually a decimal comma. For example,</p>
<ul>
<li>1,000,000.25 should be written as 1 000 000,25</li>
<li>123,456,789.123456789 should be written as 123 456 789,123 456 789</li>
<li>4,000 should be written as 4000</li>
</ul>
<p>Using sageTeX, SageMath's <code>latex</code> command, and LaTeX babel package, it is easy to replace the decimal point with a decimal comma. It is also easy to eliminate the commas for every 1000 factor. However, I haven't been able to put a small space instead.</p>
<p>I could use the <code>siunitx</code> LaTeX package, which has a command, <code>\num</code>, that does exactly this processing. However, it doesn't work with sageTeX's <code>\sage</code> command or any of it's environments. The only solution I can find is to apply the <code>\num</code> command directly to every number generated by SageMath via the <code>latex</code> command.</p>
<p>My problem is: I don't know how to do this, specially in cases where numbers are entries of a matrix, or the coefficients of a polynomial. So, <strong>how could I apply the <code>\num</code> command to every number independently using the <code>latex</code> command?</strong></p>
<p>Thanks in advance for your answers! Any alternative approach is also welcomed.</p>
https://ask.sagemath.org/question/47018/spanish-numbers-using-latex/?answer=49745#post-id-49745I'm trying to create something more like the command `\SI{number}{units}` of siunitx package but I have some trouble.
Starting with the answer of @Juanjo I'm arrived here:
\begin{sagesilent}
def SIsage(number,units=0):
if units==0:
if number in ZZ or number.parent()==QQ:
return r"\SI{"+ str(number) + "}"
else:
return r"\SI{"+ str(float(number)) + "}"
else:
if number in ZZ or number.parent()==QQ:
return r"\SI{"+ str(number) + "}" +"{"+ units + "}"
else:
return r"\SI{"+ str(float(number)) + "}" +"{"+ units + "}"
\end{sagesilent}
\sagestr{SIsage(2442,"\kilo\metre\squared")}
\sagestr{SIsage(2442)}
and it works except when using `\newton` witch it become "ewton" problably cause to `\n`
After this I want to create a new custom LaTeX command that replace `\SI{}{}` but I have some trouble with non-default arguments. I didn't understand how them work. I want be able to use both of `\SI{number}{units}` that `\SI{number}` without units. Probably the best choise is to create two different commands(?)
\newcommand{\SIsage_units}[1]{,"#1"}
\newcommand{\SIsage_number}[1]{\sagestr{SIsage(#1}}
\newcommand{\SISAGE}[2]{\SIsage_number{#2}\SIsage_units{#1})} #doesn't work
The command I want:
\SISAGE{21}{\kilo\metre\squared}
\SISAGE{26}{}
So my problems are:
1. how to avoid `\n`
2. how to set an unic custom command `\SISAGE`
Thanks everybodySun, 02 Feb 2020 09:14:40 +0100https://ask.sagemath.org/question/47018/spanish-numbers-using-latex/?answer=49745#post-id-49745Comment by sononicola for <p>I'm trying to create something more like the command <code>\SI{number}{units}</code> of siunitx package but I have some trouble.</p>
<p>Starting with the answer of <a href="/users/25859/juanjo/">@Juanjo</a> I'm arrived here:</p>
<pre><code>\begin{sagesilent}
def SIsage(number,units=0):
if units==0:
if number in ZZ or number.parent()==QQ:
return r"\SI{"+ str(number) + "}"
else:
return r"\SI{"+ str(float(number)) + "}"
else:
if number in ZZ or number.parent()==QQ:
return r"\SI{"+ str(number) + "}" +"{"+ units + "}"
else:
return r"\SI{"+ str(float(number)) + "}" +"{"+ units + "}"
\end{sagesilent}
\sagestr{SIsage(2442,"\kilo\metre\squared")}
\sagestr{SIsage(2442)}
</code></pre>
<p>and it works except when using <code>\newton</code> witch it become "ewton" problably cause to <code>\n</code></p>
<p>After this I want to create a new custom LaTeX command that replace <code>\SI{}{}</code> but I have some trouble with non-default arguments. I didn't understand how them work. I want be able to use both of <code>\SI{number}{units}</code> that <code>\SI{number}</code> without units. Probably the best choise is to create two different commands(?)</p>
<pre><code>\newcommand{\SIsage_units}[1]{,"#1"}
\newcommand{\SIsage_number}[1]{\sagestr{SIsage(#1}}
\newcommand{\SISAGE}[2]{\SIsage_number{#2}\SIsage_units{#1})} #doesn't work
</code></pre>
<p>The command I want:</p>
<pre><code>\SISAGE{21}{\kilo\metre\squared}
\SISAGE{26}{}
</code></pre>
<p>So my problems are:</p>
<ol>
<li>how to avoid <code>\n</code></li>
<li>how to set an unic custom command <code>\SISAGE</code></li>
</ol>
<p>Thanks everybody</p>
https://ask.sagemath.org/question/47018/spanish-numbers-using-latex/?comment=49837#post-id-49837Sorry I read this only now. I didn't receveid any notification.
Thanks for your help. I have post a question right now @dsejasFri, 07 Feb 2020 07:27:50 +0100https://ask.sagemath.org/question/47018/spanish-numbers-using-latex/?comment=49837#post-id-49837Comment by dsejas for <p>I'm trying to create something more like the command <code>\SI{number}{units}</code> of siunitx package but I have some trouble.</p>
<p>Starting with the answer of <a href="/users/25859/juanjo/">@Juanjo</a> I'm arrived here:</p>
<pre><code>\begin{sagesilent}
def SIsage(number,units=0):
if units==0:
if number in ZZ or number.parent()==QQ:
return r"\SI{"+ str(number) + "}"
else:
return r"\SI{"+ str(float(number)) + "}"
else:
if number in ZZ or number.parent()==QQ:
return r"\SI{"+ str(number) + "}" +"{"+ units + "}"
else:
return r"\SI{"+ str(float(number)) + "}" +"{"+ units + "}"
\end{sagesilent}
\sagestr{SIsage(2442,"\kilo\metre\squared")}
\sagestr{SIsage(2442)}
</code></pre>
<p>and it works except when using <code>\newton</code> witch it become "ewton" problably cause to <code>\n</code></p>
<p>After this I want to create a new custom LaTeX command that replace <code>\SI{}{}</code> but I have some trouble with non-default arguments. I didn't understand how them work. I want be able to use both of <code>\SI{number}{units}</code> that <code>\SI{number}</code> without units. Probably the best choise is to create two different commands(?)</p>
<pre><code>\newcommand{\SIsage_units}[1]{,"#1"}
\newcommand{\SIsage_number}[1]{\sagestr{SIsage(#1}}
\newcommand{\SISAGE}[2]{\SIsage_number{#2}\SIsage_units{#1})} #doesn't work
</code></pre>
<p>The command I want:</p>
<pre><code>\SISAGE{21}{\kilo\metre\squared}
\SISAGE{26}{}
</code></pre>
<p>So my problems are:</p>
<ol>
<li>how to avoid <code>\n</code></li>
<li>how to set an unic custom command <code>\SISAGE</code></li>
</ol>
<p>Thanks everybody</p>
https://ask.sagemath.org/question/47018/spanish-numbers-using-latex/?comment=49770#post-id-49770Hello, @sononicola! Since this is a different question from my original, you shouldn't post it as an answer to my original question. This will cause confusion among user. Please, re-ask your doubt as a separate one. I have an answer for you, but I can't post it here.Mon, 03 Feb 2020 16:56:28 +0100https://ask.sagemath.org/question/47018/spanish-numbers-using-latex/?comment=49770#post-id-49770Answer by Juanjo for <p>Dear SageMath community,</p>
<p>I am currently writing a LaTeX document in Spanish that relies heavily on sageTeX for computations. The rules for writing numbers in Spanish are a little different from English ones. For example, we are prohibited from using commas to separate every 1000 factor, instead using a small space, only if the number is longer than four digits; otherwise, no sign should be used. On the other hand, the decimal point is actually a decimal comma. For example,</p>
<ul>
<li>1,000,000.25 should be written as 1 000 000,25</li>
<li>123,456,789.123456789 should be written as 123 456 789,123 456 789</li>
<li>4,000 should be written as 4000</li>
</ul>
<p>Using sageTeX, SageMath's <code>latex</code> command, and LaTeX babel package, it is easy to replace the decimal point with a decimal comma. It is also easy to eliminate the commas for every 1000 factor. However, I haven't been able to put a small space instead.</p>
<p>I could use the <code>siunitx</code> LaTeX package, which has a command, <code>\num</code>, that does exactly this processing. However, it doesn't work with sageTeX's <code>\sage</code> command or any of it's environments. The only solution I can find is to apply the <code>\num</code> command directly to every number generated by SageMath via the <code>latex</code> command.</p>
<p>My problem is: I don't know how to do this, specially in cases where numbers are entries of a matrix, or the coefficients of a polynomial. So, <strong>how could I apply the <code>\num</code> command to every number independently using the <code>latex</code> command?</strong></p>
<p>Thanks in advance for your answers! Any alternative approach is also welcomed.</p>
https://ask.sagemath.org/question/47018/spanish-numbers-using-latex/?answer=47025#post-id-47025I will provide a parcial answer to the question. Let us consider the following LaTeX code:
\documentclass[12pt]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[spanish,es-noshorthands]{babel}
\usepackage{amsmath}
\usepackage{siunitx}
\usepackage{sagetex}
\begin{document}
\begin{sagesilent}
def NumWrap(number):
if number in ZZ or number.parent()==QQ:
return r"\num{"+ str(number) + "}"
else:
return r"\num{"+ str(float(number)) + "}"
def MatWrap(m):
s = r"\begin{pmatrix} "
for nrow in range(m.nrows()):
for ncol in range(m.ncols()-1):
s += NumWrap(m[nrow,ncol]) + " & "
s += NumWrap(m[nrow,-1])
if nrow<m.nrows()-1: s += r" \\ "
s += r" \end{pmatrix}"
return s
\end{sagesilent}
Let us do a few operations with integers:
\begin{equation*}
\num{125}\times\num{8750}+\num{93251} = \sagestr{NumWrap(125*8750+93251)}.
\end{equation*}
Now, we operate with fractions, first in display math mode:
\begin{equation*}
\sisetup{quotient-mode = fraction}
\num{12985/3425} + \num{100000/625}
= \sagestr{NumWrap(12985/3425+100000/625)},
\end{equation*}
and now in inline math mode:
$\num{12985/3425} + \num{100000/625}=\sagestr{NumWrap(12985/3425+100000/625)}$.
It is the turn for floating point numbers and matrices.
\begin{sagesilent}
m = matrix([[12.3456, 2.3453456],[-987654.32456, 23145.5342]])
\end{sagesilent}
Let
\begin{equation*}
M = \sagestr{MatWrap(m)}.
\end{equation*}
Then,
\begin{equation*}
M^{-1} = \sagestr{MatWrap(m.inverse())}.
\end{equation*}
\end{document}
Suppose that `x` is a number coming from a Sage computation that we want to include in the LaTeX document. The Sage function `NumWrap` takes a string representation of this number and returns the string `\num{str(x)}`. This string is passed from Sage to LaTeX by the `\sagestr` macro, so it can be typeset. For a matrix, `MatWrap` applies `NumWrap` to each element and adds LaTeX code to get a `pmatrix` environment (defined in the amsmath package). I adjoin a screen capture of the document obtained after typesetting the above LaTeX code.
![image description](/upfiles/15619959924053571.png)Mon, 01 Jul 2019 17:46:59 +0200https://ask.sagemath.org/question/47018/spanish-numbers-using-latex/?answer=47025#post-id-47025Comment by dsejas for <p>I will provide a parcial answer to the question. Let us consider the following LaTeX code:</p>
<pre><code>\documentclass[12pt]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[spanish,es-noshorthands]{babel}
\usepackage{amsmath}
\usepackage{siunitx}
\usepackage{sagetex}
\begin{document}
\begin{sagesilent}
def NumWrap(number):
if number in ZZ or number.parent()==QQ:
return r"\num{"+ str(number) + "}"
else:
return r"\num{"+ str(float(number)) + "}"
def MatWrap(m):
s = r"\begin{pmatrix} "
for nrow in range(m.nrows()):
for ncol in range(m.ncols()-1):
s += NumWrap(m[nrow,ncol]) + " & "
s += NumWrap(m[nrow,-1])
if nrow<m.nrows()-1: s += r" \\ "
s += r" \end{pmatrix}"
return s
\end{sagesilent}
Let us do a few operations with integers:
\begin{equation*}
\num{125}\times\num{8750}+\num{93251} = \sagestr{NumWrap(125*8750+93251)}.
\end{equation*}
Now, we operate with fractions, first in display math mode:
\begin{equation*}
\sisetup{quotient-mode = fraction}
\num{12985/3425} + \num{100000/625}
= \sagestr{NumWrap(12985/3425+100000/625)},
\end{equation*}
and now in inline math mode:
$\num{12985/3425} + \num{100000/625}=\sagestr{NumWrap(12985/3425+100000/625)}$.
It is the turn for floating point numbers and matrices.
\begin{sagesilent}
m = matrix([[12.3456, 2.3453456],[-987654.32456, 23145.5342]])
\end{sagesilent}
Let
\begin{equation*}
M = \sagestr{MatWrap(m)}.
\end{equation*}
Then,
\begin{equation*}
M^{-1} = \sagestr{MatWrap(m.inverse())}.
\end{equation*}
\end{document}
</code></pre>
<p>Suppose that <code>x</code> is a number coming from a Sage computation that we want to include in the LaTeX document. The Sage function <code>NumWrap</code> takes a string representation of this number and returns the string <code>\num{str(x)}</code>. This string is passed from Sage to LaTeX by the <code>\sagestr</code> macro, so it can be typeset. For a matrix, <code>MatWrap</code> applies <code>NumWrap</code> to each element and adds LaTeX code to get a <code>pmatrix</code> environment (defined in the amsmath package). I adjoin a screen capture of the document obtained after typesetting the above LaTeX code.
<img alt="image description" src="/upfiles/15619959924053571.png"></p>
https://ask.sagemath.org/question/47018/spanish-numbers-using-latex/?comment=47067#post-id-47067Hello, @Juanjo. Thank you very much for your answer!!! I had an awful time trying to use the `\num` LaTeX command through sageTeX. I could find a general solution (see my answer), but I used yours as a starting point. Thanks again!Fri, 05 Jul 2019 03:42:57 +0200https://ask.sagemath.org/question/47018/spanish-numbers-using-latex/?comment=47067#post-id-47067Answer by dsejas for <p>Dear SageMath community,</p>
<p>I am currently writing a LaTeX document in Spanish that relies heavily on sageTeX for computations. The rules for writing numbers in Spanish are a little different from English ones. For example, we are prohibited from using commas to separate every 1000 factor, instead using a small space, only if the number is longer than four digits; otherwise, no sign should be used. On the other hand, the decimal point is actually a decimal comma. For example,</p>
<ul>
<li>1,000,000.25 should be written as 1 000 000,25</li>
<li>123,456,789.123456789 should be written as 123 456 789,123 456 789</li>
<li>4,000 should be written as 4000</li>
</ul>
<p>Using sageTeX, SageMath's <code>latex</code> command, and LaTeX babel package, it is easy to replace the decimal point with a decimal comma. It is also easy to eliminate the commas for every 1000 factor. However, I haven't been able to put a small space instead.</p>
<p>I could use the <code>siunitx</code> LaTeX package, which has a command, <code>\num</code>, that does exactly this processing. However, it doesn't work with sageTeX's <code>\sage</code> command or any of it's environments. The only solution I can find is to apply the <code>\num</code> command directly to every number generated by SageMath via the <code>latex</code> command.</p>
<p>My problem is: I don't know how to do this, specially in cases where numbers are entries of a matrix, or the coefficients of a polynomial. So, <strong>how could I apply the <code>\num</code> command to every number independently using the <code>latex</code> command?</strong></p>
<p>Thanks in advance for your answers! Any alternative approach is also welcomed.</p>
https://ask.sagemath.org/question/47018/spanish-numbers-using-latex/?answer=47066#post-id-47066Hello, Sage community. I was able to adapt @Juanjo's answer, but then I did some incantation (which I don't completely understand) that generalized his method. The only problem is that I had to modify the code in the file `sage.misc.latex.latex`. Here I describe what I did.
First of all, I made a copy of the file I just mentioned, and added a small modification of part of your code. Here is the patch:
@@ -36,6 +36,7 @@
from sage.misc.sage_ostools import have_program
from sage.misc.temporary_file import tmp_dir
@@ -36,6 +36,7 @@
from sage.misc.sage_ostools import have_program
from sage.misc.temporary_file import tmp_dir
+from sage.all import RR, CC
EMBEDDED_MODE = False
COMMON_HEADER = \
@@ -921,6 +922,10 @@
sage: latex((x,2), combine_all=True)
x 2
"""
+ if x in RR:
+ return LatexExpr(r'\num{' + str(x) + '}')
+ elif x in CC:
+ return LatexExpr(r'\num{' + str(x.real_part()) + '+' + str(x.imag_part()) + 'i}')
if has_latex_attr(x):
return LatexExpr(x._latex_())
try:
I saved this as LaTeX.py., then I made an import:
from LaTeX import latex
And here is the part I don't understand:
sage.misc.latex.latex = latex
This last line makes my new `latex()` function work on matrices, tuples, list, etc. automatically. (Why?)
Fri, 05 Jul 2019 03:40:34 +0200https://ask.sagemath.org/question/47018/spanish-numbers-using-latex/?answer=47066#post-id-47066