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.... >.>
    – Canageek
    Apr 2, 2012 at 18:22
  • I'm surprised: there's no lua solution. (Yet?)
    – mbork
    Apr 2, 2012 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, 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{}.
    – Nagel
    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

2 Answers 2


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









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


siunitx update:

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




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:)
    – yannisl
    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
  • 19
    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?).


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.
    – Canageek
    Apr 2, 2012 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.
    – yannisl
    Apr 2, 2012 at 18:25
  • 4
    @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
  • 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:)
    – yannisl
    May 30, 2012 at 12:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .