[Tex/LaTex] Switching decimal mark between comma and dot

typography

You might know that in large part of continental Europe, decimal mark is comma instead of dot. I am writing a really huge document and I would like to switch between them easily from one point, e.g.

\newif\comma
\commatrue

\ifcomma{
???
}

\pi = 3???14

I am aware that all decimal mark dots should be replaced by something else, but what? I am opening contest for a most elegant solution for the problem.

Best Answer

The following example sets the numbers in math mode. That allows an easy "redefinition" of the comma or dot to produce a dot or comma.

Syntax:

  • \num{...} takes a number with dot or comma and outputs the number according to switch \ifnumcomma.
  • If \numcommatrue if active, then \num outputs a comma, with \numcommafalse, the output is a dot.

The definition can be used for both plain TeX and LaTeX. The example contains a testing part for plain TeX:

\mathchardef\NumMathCodeDot=\mathcode`\. %
\mathchardef\NumMathCodeComma=315 % \mathord, not \mathpunct

\newif\ifnumcomma

\def\num#1{%
  \leavevmode
  \begingroup
    \ifnumcomma
      \mathcode`\.=\NumMathCodeComma
      \mathcode`\,=\NumMathCodeComma
    \else
      \mathcode`\.=\NumMathCodeDot
      \mathcode`\,=\NumMathCodeDot
    \fi
    \ifmmode
      #1%
    \else
      \mathsurround=0pt %
      $#1$%
    \fi
  \endgroup
}

% Testing (plain TeX)

\numcommatrue
\num{3.14} + \num{1,23}

\numcommafalse
\num{3.14} + \num{1,23}

\bye

Result

Remarks:

  • The font for the number is taken from math mode.
  • If siunitx is available, use it, because it is much more powerful and configurable. Then \num can be left in the code, but the definition then comes from siunitx. And \num is configured there.