Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I want to typeset big numbers with a separator (e.g a space or a ",") between hundreds and thousands, etc... For example 1 Million = 1 000 000.

I know the package siunitx which does it very well, but one must write \num{the big number}. Would it be possible, that this typeset automatically occurs for any number in a maths environment $ ...$, without being obliged to write explicitly \num ?

share|improve this question
    
Well you could write \SI and include units.... >.> –  Canageek Apr 2 '12 at 18:22
    
I'm surprised: there's no lua solution. (Yet?) –  mbork Apr 2 '12 at 22:55
1  
@mbrok: ConTeXt does parse digits when converting to MathML. For example $1234 x$ will be converted to <mnum> 123 </mnum> <...>x</...> (sorry don't remember MathML tags on top of my head. Of course the parser is written in Lua and, in principle, adding a output formatter for PDF output is easy. –  Aditya May 30 '12 at 15:31
    
Thanks for this question, I was just wondering the same thing. Since I think @David Carlisle's advice is as wise as his code is impressive, and am not tempted to use that code, my two cents is that inputting \, separators manually is often easier than writing \num{}. –  Nagel Apr 3 at 10:31

2 Answers 2

up vote 24 down vote accepted

You really really really don't want to do this.

But if you did want to do it, then you could do this, but you just know it's bound to break something.

enter image description here

\documentclass{article}

\usepackage{comma}
\def\commaformtoken{\,}
\edef\mca{\the\mathcode`0\space}
\edef\mcb{\the\mathcode`1\space}
\edef\mcc{\the\mathcode`2\space}
\edef\mcd{\the\mathcode`3\space}
\edef\mce{\the\mathcode`4\space}
\edef\mcf{\the\mathcode`5\space}
\edef\mcg{\the\mathcode`6\space}
\edef\mch{\the\mathcode`7\space}
\edef\mci{\the\mathcode`8\space}
\edef\mcj{\the\mathcode`9\space}

\def\normaldigits{%
\mathcode`\0=\mca
\mathcode`\1=\mcb
\mathcode`\2=\mcc
\mathcode`\3=\mcd
\mathcode`\4=\mce
\mathcode`\5=\mcf
\mathcode`\6=\mcg
\mathcode`\7=\mch
\mathcode`\8=\mci
\mathcode`\9=\mcj
}

\newcount\hmmcnt
\makeatletter
\def\hmmdef#1{%
\bgroup\lccode`\~`#1\lowercase{\egroup
\count@\mathcode`~
\mathcode`~="8000
\edef~{%
\bgroup
\noexpand\normaldigits
\afterassignment\noexpand\hummcomma\hmmcnt#1}}}

\def\hummcomma{\@commaform\hmmcnt\egroup}

\def\activedigits{
\hmmdef0
\hmmdef1
\hmmdef2
\hmmdef3
\hmmdef4
\hmmdef5
\hmmdef6
\hmmdef7
\hmmdef8
\hmmdef9
}


\makeatother

\begin{document}

\activedigits
\[123456 = \frac{1234560}{10} \]
\normaldigits
\[123456 = \frac{1234560}{10} \]
\activedigits
\[123456 = \frac{1234560}{10} \]

\end{document}

siunitx update:

If you'd rather use siunitx rather than comma package to do the spacing then change the package loading, and change

\def\hummcomma{\@commaform\hmmcnt\egroup}

to

\def\hummcomma{\num{\the\hmmcnt}\egroup}

basic idea of code

to turn 123 into \num{123} the idea is fairly simple.

  1. give each digit an active definition so that, say, 1 is equivalent to \aftarassignment\helper\count@1

  2. TeX then starts to assign a number to \count@ so it gobbles up all following digits until it gets to a non-digit leaving the value in \count@ (and failing if that number is too big).

  3. The \afterassignment primitive then re-inserts the \helper token to expand, so this can now access the number from the count register as \the\count@ so \expandafter\num\expandafter{\the\count@} is the same as \num{123}

There is a slight problem in that the above description doesn't work, as you don't know which digit will be first, so you have to make all digits have mathcode "8000 and all have active definitions. But they would still have those definitions when the digits were re-inserted by executing \the\count@ which would put you in an infinite loop. So the definition has to start a local group, within that group re-define each digit to typeset its normal \mathcode specified character, and then finally after applying the spacing command, end the group. The \mc? commands are the saved mathcodes for each of the digits, and \hmmdef sets up the digit specified in its argument to have the right mathcode and active definition. \hmmcomma is the helper token inserted by \aftergroup that actually does the spacing of every third digit, using siunitx or comma packages.

update changed the grouping to use \bgroup rather than \begingroup as the latter does not work with x^2 you have to use the official LaTeX syntax x^{2}.

share|improve this answer
2  
+1 and wow awesome –  Marco Daniel Apr 2 '12 at 19:59
    
Wow! You need to explain this:) –  Yiannis Lazarides Apr 2 '12 at 20:03
    
and by the way ... why should i really really really do NOT want to do this ? –  nicolas roy Apr 2 '12 at 20:43
9  
reasons for not doing this: either (a) something will go wrong or, worse, (b) it will work and you'll like using it then 20 years later something will go wrong and you'll expect me to remember how to fix it. –  David Carlisle Apr 2 '12 at 20:53
    
Just to throw a wrench in the works: Is it possible to modify the code to accept a minimum 5-digit integer before grouping occurs (similar to siunitx's group-minimum-digits)? –  Werner Apr 3 '12 at 1:18

No it is not possible to automate this in LaTeX. Many people including Knuth will disagree with you using thousands separators in math environments. (See Should one use thousands separators in equations?).

Edit

As David just showed it is possible to automate it.

share|improve this answer
    
In most cases it is probably better to use scientific notation or a larger unit (km instead of m for example), unless you need all the digits. –  Canageek Apr 2 '12 at 18:23
1  
@Canageek Sure it is better if is applicable but sometimes, especially in some expansions you might want to show discrete number coefficients. –  Yiannis Lazarides Apr 2 '12 at 18:25
4  
@YiannisLazarides, not sensible, but never say not possible, (see other answer:-) –  David Carlisle Apr 2 '12 at 19:56
    
If you have numbers big enough to need thousands separators, it isn't maths, it's arithmetic. :) –  Brent.Longborough May 30 '12 at 12:48
1  
@Brent.Longborough Possibly, but they keep on popping up in papers using polynomials. When I was in high school maths was divided into three subjects for some years arithmetic, algebra and geometry:) –  Yiannis Lazarides May 30 '12 at 12:52

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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