0

My situation is similar to, but hopefully different from How do I ‘expand’ a control sequence \let to a character?, as that question does not admit a positive answer. I want to parse, not expand, but not character by character, if I can avoid that (as in Parsing a macro argument character-by-character for conditional execution). Existing parsers like Can one define an expandable command that removes control sequences from its argument? seem not not work for me, unfortunately (well, I can't get them to work, that is).

Consider the following: There is this \let definition

\newif\ifintheway
\inthewayfalse
\show\inthewayfalse % ->\let \ifintheway \iffalse .

that sits in a macro \parseme of which I want to extract the text 17

\def\parseme{\inthewayfalse 17}

How can I do that?

Obviously, expanding the macro with \edef\parsedmacro{\parseme} does not work and I get the complaint

! Incomplete \iffalse; all text was ignored after line 5.

However, adding a \fi in the \edef does not help, either. Is there something else I can do?

In my situation I even know what the obstacle is. It is always \intheway. Then there is my text (of which I know it is a number), and then the macro ends. Finally, as an additional obstacle, \parseme is really a `\csname b\@citeb\endcsname.

(A positive answer would also resolve Custom \thebibliography with alphabetic numbering for scrltt2 not working in presence of babel, at least in parts.)

bjoseru
  • 133
  • Why can't you use \def rather than \edef? – cfr Nov 22 '14 at 22:02
  • @cfr I want to use the content of the macro and the idea with \edef was that it expands away the stuff I don't need. In contrast, \def does not expand anything, so the obstructing \let/\inthewayfalse stays where it is and I just have a new macro with a different name but the same problem as before. – bjoseru Nov 24 '14 at 10:32

1 Answers1

4

An explicit number can be expanded without changing it and the unwanted command sequence can be redefined to vanish itself, when expanded:

% Setup
\makeatletter
\newif\ifintheway
\def\@citeb{foobar}
\def\bfoobar{\inthewayfalse 17}

% Extract the number from \csname b\@citeb\endcsname
% and store it in the macro \mynumber    
\begingroup
  \let\inthewayfalse\@empty
  \edef\x{\endgroup
    \def\noexpand\mynumber{%
      \csname b\@citeb\endcsname
    }%
  }%
\x

% Show the result in the console:
\typeout{My number is "\mynumber".}
\stop

The essential part is:

\let\inthewayfalse\@empty
% same as:
\def\inthewayfalse{}
% or
\newcommand*{\inthewayfalse}{}

And the expansion gets rid of \intheway:

\edef\mynumber{\csname b\@citeb\endcsname}

The example puts this in a group to keep the redefinition of \inthewayfalse local.

Heiko Oberdiek
  • 271,626