Automatic thousands separators?

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 ?

• Well you could write \SI and include units.... >.> Apr 2, 2012 at 18:22
• I'm surprised: there's no lua solution. (Yet?) Apr 2, 2012 at 22:55
• @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. May 30, 2012 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{}. Apr 3, 2014 at 10:31
• See the posting Making large numbers readable by inserting thinspaces for visual grouping for a LuaLaTeX-based solution that (a) works in both text and math mode and (b) doesn't operates only on the integer portions and not on the decimal portions of numbers
– Mico
Oct 29, 2015 at 21:25

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.

\documentclass{article}

\usepackage{comma}
\def\commaformtoken{\,}
\edef\mca{\the\mathcode0\space}
\edef\mcb{\the\mathcode1\space}
\edef\mcc{\the\mathcode2\space}
\edef\mcd{\the\mathcode3\space}
\edef\mce{\the\mathcode4\space}
\edef\mcf{\the\mathcode5\space}
\edef\mcg{\the\mathcode6\space}
\edef\mch{\the\mathcode7\space}
\edef\mci{\the\mathcode8\space}
\edef\mcj{\the\mathcode9\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}.

• Wow! You need to explain this:) Apr 2, 2012 at 20:03
• and by the way ... why should i really really really do NOT want to do this ? Apr 2, 2012 at 20:43
• 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. Apr 2, 2012 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, 2012 at 1:18
• @DavidCarlisle : too late, i implemented your code and sold the software with your name for online-support ;) Apr 3, 2012 at 6:26

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.

• 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. Apr 2, 2012 at 18:23
• @Canageek Sure it is better if is applicable but sometimes, especially in some expansions you might want to show discrete number coefficients. Apr 2, 2012 at 18:25
• @YiannisLazarides, not sensible, but never say not possible, (see other answer:-) Apr 2, 2012 at 19:56
• If you have numbers big enough to need thousands separators, it isn't maths, it's arithmetic. :) May 30, 2012 at 12:48
• @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:) May 30, 2012 at 12:52