I have implemented couple of macros that should remove commas from numbers. It's nasty dirty stuff, but it works when input is number:

\removecommas{12,34,56.7} % => 123456.7

But it stop working when the input is given by some command, such as:


Why is this happening? How can I force TeX to expand \mynumber before commas? I've tried:


but that didn't seem to help.


3 Answers 3


You are nearly there with


What is needed is a group:


To understand why, you have to remember that TeX grabs arguments as tokens. Taking the example input, in the first case, you expand \mynumber to 12,34,56.7. TeX then grabs 1 as the argument to \removecommas, and unsurprisingly nothing actually happens. On the other hand, with the group in place all of 12,34,56.7 is grabbed by \removecommas, and the processing takes place as you want.

(Notice that we need two \expandafter primitives. The second one is needed to expand past {.)


If you need only up to one level expansion, this works:

\def\removecommas{\begingroup\catcode`,=9 \doremove}

The other answers explain the macro expansion. However, if all you want to do is to remove the commas in numbers you could use the numprint package with the following setup:


\renewcommand*\nprt@dotlist{.}% only . for decimal separator in input
\renewcommand*\nprt@ignorelist{,}% ignore , in input
\npdecimalsign{\ensuremath{.}}% use . as decimal output


You must log in to answer this question.