How to check if attempts at totally expanding the tokens of an arbitrary sequence of tokens would yield an error?

How to check if attempts at totally expanding the tokens of an arbitrary sequence of tokens would yield something where catcode-1/2-braces are not balanced?

(For me these questions are part of the problem of checking whether an arbitrary sequence of tokens is suitable for forming a number or forming a length.

E.g., all the number-checks I found so far bail out with error-messages when expanding the sequence of tokens in question yields error-messages or something with unbalanced braces.

E.g., the tricky thing


   \Switch{ }{\expandafter\expandafter\expandafter}%


\CheckWhetherInteger{4}{Integer}{No Integer}

\CheckWhetherInteger{Text}{Integer}{No Integer}


does not work with things like

\CheckWhetherInteger{\csname}{Integer}{No Integer}


\CheckWhetherInteger{\@gobble}{Integer}{No Integer}


\CheckWhetherInteger{\iffalse{\fi}}{Integer}{No Integer}


\CheckWhetherInteger{\gobbletoclosebrace}{Integer}{No Integer}


  • 1
    What are you actually trying to do? My assumption would be that "completely expand this and figure out if there's errors" would be equivalent to the halting problem, and therefore impossible to do. Are you simply trying to determine if an argument is an integer, and running into problems when the potential integer is code? – Teepeemm May 16 at 13:21
  • @Teepeemm I try to achieve something that is a naturalness in many programming-languages: Checking whether an argument is of the desired type. (You can, e.g., write macros where the user within arguments has to specify numbers or lengths and where specifying non-numbers/non-lengths leads to problems and in (La)TeX to inscrutable error-messages from which the all-days user cannot deduce what's wrong about her/his code.) – user189052 May 16 at 13:50
  • 1
    How to handle, e.g., \def\recursion#1{\recursion{#1}} \CheckWhetherInteger{\recursion{1}}{Integer}{No Integer} ? This won't work out and it won't yield error-messages all too soon... ;-) – Ulrich Diez May 16 at 14:05
  • 1
    You could, e.g., implement a loop which token-wise checks whether the leading token of the sequence is expandable and if so trigger expansion. But you would, e.g., also need testing whether each macro will always find the arguments it needs... A reliable test for that is unknown to me. Besides this: Numbers and lengths do not necessarily come from expandable tokens. Referring to the value of a \count-register via \count or via a countdef-token can, e.g., with things like \ifnum or \romannumeral be done without the tokens that form the number containing anything expandable at all. – Ulrich Diez May 16 at 14:12
  • Besides this you'd, e.g., need checking for matching \else and \fi. The possibility of never-ending recursion might need to be taken into account also. – Ulrich Diez May 16 at 14:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.