0

I try to define a macro taking as an argument either a number or a colon-delimited range of numbers, e.g. \macro{3.14} or \macro{2:5}. I can parse the colon easily using a simple trick:

\def\macro#1{ \def\m@gic##1:##2:##3\end{\ifx ##3:\relax Two arguments (##1,##2)\else One argument (##1)\fi}\m@gic #1::\end} 

However, calling such a macro fails when French babel is loaded, because it makes the colon an active character. Undaunted, I write a second macro for that case:

\begingroup\catcode`\:=\active \def\macro@active#1{ % same contents... } \endgroup 

and can now decide at call-time, depending on the value of \the\catcode\`:, which macro to call.

However, the situation becomes even more complex! It so occurs that \macro may take a default value, which might be defined before the babel package is loaded, and thus might have a normal colon character: something like this

\def\macrodefault{\macro{1:2}} % with normal category colon... \usepackage[french]{babel} % ... but now it is active. 

(This situation is out of my control and I cannot just swap the two lines). So now I want the \macro expansion to be able to parse both types of colon, either active or normal. Is there a simple way to do this? Maybe re-parse the #1 in a \toks register? or use some \lccode trick? (A simple \afterassignment will not do it, since I cannot expect the numbers to be integers).

1
  • 2
    It is a lot easier to use expl3 and its seq data type. I've used that for a (private) package that parses labels of the form thm:foo or item:thm:foo:a. And it does work with french babel. Commented Aug 23, 2024 at 10:35

1 Answer 1

4

Since you only care about digits (and decimal points and commas) and colons you can normalize the catcodes before splitting.

enter image description here

\documentclass{article} \def\macro#1{\expandafter\xmacro\detokenize{#1:}\relax:\xmacro} \def\xmacro#1:#2:#3\xmacro{\ifx\relax#2One (#1)\else Two (#1,#2)\fi} \begin{document} \macro{1.234} \macro{2:33} \catcode`\:=\active \def:{French spaced colon code here} \macro{1.234} \macro{2:33} \end{document} 

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.