1

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

\documentclass{article}

\makeatletter
\newcommand\Switch[2]{#2#1}%
\newcommand\InsertBraceAndCheckForEmptiness{%
   \romannumeral0%
   \Switch{ }{\expandafter\expandafter\expandafter}%
   \expandafter\expandafter\expandafter\CheckIfEmpty
   \expandafter\expandafter\expandafter{%
   \expandafter\@gobble\string}%
}%
\newcount\tempcount
\newcommand\CheckWhetherInteger[1]{%
  \afterassignment\InsertBraceAndCheckForEmptiness
  \expandafter\@gobble\string{%
  \tempcount=0#1}%
}%
\newcommand\CheckIfEmpty[1]{%
  \if\relax\detokenize{#1}\relax
    \expandafter\@firstoftwo\else\expandafter\@secondoftwo\fi
}%
\makeatother

\begin{document}

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

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

\end{document}

does not work with things like

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

or

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

or

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

or

\def\gobbletoclosebrace{%
   \expandafter\expandafter\expandafter\@gobble
   \expandafter\expandafter\expandafter{\expandafter\@gobble\string}%
}%
\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.