16

I prefer a large number be shown in three digit groups. For example 1234567 be shown like 1 234 567.

Is there a way to make this automatic in LaTeX such that wherever I insert 1234567, it is automatically shown as 1 234 567 in the output?

8
  • 3
    Related: How to typeset large numbers
    – Werner
    Oct 18, 2015 at 11:56
  • They are good ansers but I'm looking for a more automatic way. For example, with a single command applied to all of the document. Oct 24, 2015 at 7:47
  • Look again you'll find the automatically word there. I just tried to improve it grammatically. The answers are good, btw they don't solve my problem. So I haven't accepted them yet. Oct 24, 2015 at 8:15
  • Are the large numbers in your document integers, or can they contain decimal parts, as in 123456.789012? Please advise.
    – Mico
    Oct 25, 2015 at 5:04
  • 1
    See also this posting, from April 2012: Automatic thousands separators?
    – Mico
    Oct 29, 2015 at 20:35

3 Answers 3

17

Here's a LuaLaTeX-based solution, which adds thinspaces automatically to the integer parts of numbers that contain between 5 and 12 digits.

Some comments:

  • Numbers with exactly four digits do not get a thinspace inserted after the first (left-most) digit, in keeping with the comment @egreg posted. If you do want a thinspace separator for numbers with exactly four digits, change the final string.gsub instruction line from

                text = string.gsub ( text , "(%d?%d%d)(%d%d%d)", "%1\\,%2" )
    

    to

                text = string.gsub ( text , "(%d?%d?%d)(%d%d%d)", "%1\\,%2" )
    
  • No thinspaces are inserted in the decimal portions of any long numbers; only the integer portions are processed.

  • The code keeps track of whether or not we are inside a verbatim or Verbatim environment. If we're inside such an environment, processing is suspended.

enter image description here

% !TEX TS-program = lualatex
\documentclass{article}

\usepackage{luacode}
\begin{luacode}
ok_to_process = true
function ez_read ( line )
  if string.find ( line , "\\begin{[vV]erbatim}" ) then
     ok_to_process = false
     return line
  elseif string.find ( line , "\\end{[vV]erbatim}" ) then
     ok_to_process = true
     return line
  else
    if ok_to_process then
      x = line:gsub ( "([%.]-)(%d+)", function ( back, text )
        if back ~= "" then
          return back..text
        end
        text = string.gsub ( text , "(%d?%d?%d)(%d%d%d)(%d%d%d)(%d%d%d)",
                                    "%1\\,%2\\,%3\\,%4" )
        text = string.gsub ( text , "(%d?%d?%d)(%d%d%d)(%d%d%d)",
                                    "%1\\,%2\\,%3" )
        text = string.gsub ( text , "(%d?%d%d)(%d%d%d)", "%1\\,%2" )
        return text
      end)
      return x
    end
  end
end
luatexbase.add_to_callback( "process_input_buffer", ez_read, "ez_read")
\end{luacode}

\begin{document}    
123456789012, 123456789, 123456, 12345

But: 1234 --- no thinspace after ``1''

123456.123456789, 0.12345, 123456
\end{document}
1
  • Usually one doesn't want the separation in groups of four digits (except in tables where separation involves larger groups in the same column).
    – egreg
    Oct 24, 2015 at 10:01
35

The packages numprint and siunitx can do this (here I show numprint)

\documentclass{article}

\usepackage{numprint}

\begin{document}
\numprint{1234567}

\numprint{123456789}
\end{document}

enter image description here

2
  • 1
    Ah ha. Improving on your typing speed? ;-)
    – user11232
    Oct 18, 2015 at 9:22
  • @HarishKumar: Stock answer:-P
    – user31729
    Oct 18, 2015 at 9:22
23

siunitx provides \num macro for such jobs.

\documentclass{article}
\usepackage{siunitx}
\begin{document}
  \num{1234567}
\end{document}

enter image description here

You must log in to answer this question.

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