How to convert dots to commas in mathmode?

For my study of physics I have to do various lab reports. Normally I just process all my data using python, gnuplot or Mathematica and copy it from there to my TeX document. Unfortunately all these applications use the . (dot) as a decimal seperator. Because I study in Germany the german standard says that I have to use , (comma) as the decimal seperator. In most cases the supervisors of the lab are forgiving, but one of them told me to redo my whole lab report because of this. Now I don't want to search all the 15 pages to replace all dots with commas. I could have used a regular expression but this still implied editing the whole file.

-
Although it is very interesting (and quite useful), it does not pose a question. This is a Q&A site. So a better way would be to ask "How to change . to , in mathmode". Then you can yourself answer that question. Please see the discussion: meta.tex.stackexchange.com/questions/4/… –  zeroth Feb 10 '13 at 15:11
No problem. I just didn't find that question here so I thought it might be helpful for others if I share my solution. –  Henri Menke Feb 10 '13 at 15:17
Great! This is much better! :) Also there is no need to sign of with your name. I have deleted that. –  zeroth Feb 10 '13 at 15:18

It is perhaps less intrusive just to change the mathcode of . rather than globally define an active . to be a comma.

\mathcode\.=\mathcode\,


Would define . to be like , (as in the self-answer) If you want the . to be mathord so it os not followed by a thin space then you could use

\DeclareMathSymbol{.}{\mathord}{letters}{"3B}

-
\mathcode\.=\numexpr\mathcode\,-"6000\relax ;-) –  egreg Feb 10 '13 at 20:42

Whether in math mode or not, I will

• always use \num from siunitx for any numerical literals,
• always use . for the decimal marker in the decimal literals,
• but later set the global setting in the preamble to specify the decimal marker for the rendered output.
\documentclass{article}

\usepackage{siunitx}
\sisetup{
%output-decimal-marker={,}% just uncomment if you want to use comma as the decimal marker!
}

\begin{document}
$\num{3.14}$ is not the value of $\pi$, \num{2.718281828} is not the value of $e$.
\end{document}


This mechanism makes your document portable!

-
I hate using , as the decimal marker because comma separated list becomes ambiguous. For example, what does (1,2,3,4) mean in the context of 3-dimensional point? In scientific purposes, we have to use . for the decimal marker! –  Please don't touch Feb 10 '13 at 15:53
I totally agree with you on that. The problem is that I needed the supervisors signature for this lab report. And in the lab not the truth is correct but what is signed by the supervisors :( –  Henri Menke Feb 10 '13 at 16:19
@GarbageCollector This is not about hating, but about national typography rules. In Czech language, the rules are similar, and you get easily used to ; as a list seperator. With the proper spacing (tight around , and loose after ;) the result is just ok. –  tohecz Feb 10 '13 at 16:30
The countries using non-metric measurng systems should change it ;-) –  Tobi Feb 10 '13 at 17:06
@GarbageCollector No, but the imperial units are confusing (and ambiguous). In my opinion more than using ,; instead of .,. Since this discussion starts to be off-topic, this is my last comment here. –  tohecz Feb 10 '13 at 17:24

The Trick

\mathcode\.="8000
{\catcode\.=\active
\gdef.{,}}


Explanation

But what does this do? I'll try to explain what the above code is doing. If I'm wrong please correct me.

\mathcode\.="8000 makes the comma "active" in mathmode, i.e. everytime the compiler runs over a comma code is executed instead of a comma being typeset. {\catcode\.=\active \gdef.{,}} is the code that gets executed in the beginning, where \catcode\.=\active makes . active and then defines \gdef.{,} which substitutes the . with a ,. That's it!

Keep in mind that now EVERY . in mathmode is replaced with a ,. The code I presented doesn't care if the . you wrote is a decimal seperator of a full stop. If you want to type a full stop consider using \text{.}, or define yourself a macro.

-
I don't think you need \makeat... here ;) Your explanation of the meaning of that command is incorrect, see here: tex.stackexchange.com/questions/8351/… –  tohecz Feb 10 '13 at 15:30
You're right, thanks. –  Henri Menke Feb 10 '13 at 15:35
Yes thanks I deleted my comment (I prefer my answer though:-) –  David Carlisle Feb 10 '13 at 16:17
Btw, shouldn't it be \gdef.{{,}}` so that it is an ordinary atom therefore tightly spaced? –  tohecz Feb 10 '13 at 16:28
I accomplished this by loading icomma, but your solution also sounds good, thank you. –  Henri Menke Feb 10 '13 at 19:25