50

In German and some other European languages, the comma rather than the period is used as a decimal separator and the dot is used as a thousands separator: 12.345,67. The comma creates some unwanted space when used as in $12.345,67$. Is there a light-weight package or some macro to support this? (I'm not looking for automatic formatting like insertion of a thousands separator.)

\documentclass{minimal}
\usepackage[utf8]{inputenc}

\begin{document}
Der Preis beträgt $12.345,67$ Euro.    
\end{document}

Der Preis beträgt $12.345,67$ Euro.

An easy fix is typing $12.345,{}67$ but I think this is too cumbersome.

1
  • 3
    This goes for Danish as well (which I was looking for). Dec 15, 2014 at 13:21

5 Answers 5

41

Use the icomma package.

The effect can also be achieved with the siunitx package and its output-decimal-marker setting, although you'll have to pass numbers to the \num macro.

4
  • 1
    The implementation of icomma is really small and worth a look. May 20, 2011 at 12:12
  • 5
    with siunitx you can even omit the thousand-seperator and use the group-separator option to get it automatically.
    – Tobi
    May 20, 2011 at 13:40
  • 2
    Just for future reference, icomma doesn't seem to work with xelatex.
    – sfranky
    Nov 9, 2014 at 11:22
  • I disagree with @sfranky's claim. For sure, \documentclass{article} \usepackage{icomma} \begin{document} $123,45$ \end{document} works as expected under XeLaTeX.
    – Mico
    Sep 8, 2023 at 12:02
20

There are various situations one can find in.

(1) There are just a few decimal numbers in the document: use $123{,}45$

(2) There are several numbers and a few formulas involving the comma: use Herbert's trick and define also

\DeclareMathSymbol{\comma}{\mathpunct}{letters}{"3B}

using \comma when it must be a punctuation symbol in formulas

(3) Several numbers and several formulas: use icomma; one must remember to put a space when needed in cases such as the interval~$[0, 1]$. Actually icomma works as outlined in case (2), but using the , as a "math active" character that acts depending on the next token.

(4) Many numbers: use siunitx and \num, which ensures uniform treatment of the numbers. For example, one can change the format from 123.456,78 to a "more correct" $123\,456{,}78$ by changing one line in the document, i.e., in the argument of \sisetup.

7
\documentclass{minimal}
\usepackage[utf8]{inputenc}
\DeclareMathSymbol{,}{\mathord}{letters}{"3B}

\begin{document}
Der Preis beträgt $12.345,67$ Euro.    
\end{document}

you can also redefine the dot to a \mathpunct, if you like

1
  • 6
    this is good assuming that the OP doesn't need the comma in math formulas such as $(0,1)$.
    – egreg
    May 20, 2011 at 11:32
6

Here's a LuaLaTeX-based solution. It wraps all commas that are immediately preceded and followed by numeric digits in pairs of curly braces. The method works in both text mode and math mode.

If the material is in math mode and you do not want Lua to convert , to {,} "on the fly", simply put a space either before or after the comma. E.g., write the closed 0-1 interval as $[0, 1]$.

enter image description here

% !TeX program = lualatex
\documentclass{article}
\usepackage{fontspec}
\usepackage{luacode}
\begin{luacode}
function wrap_comma ( buff )
  return ( string.gsub ( buff , "(%d),(%d)", "%1{,}%2" ) )
end
\end{luacode}
\AtBeginDocument{\directlua{luatexbase.add_to_callback 
    ("process_input_buffer", wrap_comma, "wrap_comma")}}
\begin{document}
Der Preis beträgt $12.345,67$ Euro. Das Intervall $[0, 1]$. 
\end{document}
1

Just for completeness' sake:

There's also the ziffer package.

Note that its standard setting actually has a \phantom{.} as thousands separator, likely intended to emulate the widespread (ISO, BIPM, IUPAC, AMA) recommendation of using (thin) spaces as unambiguous digit group separator.

If a . (or an actual \thinspace) is needed for this purpose, the \ZifferLeer needs to be redefined.

You must log in to answer this question.

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