I wrote a command for accessing a hardcoded string ("For Reasons™", because xstring StrMid
is incredibly slow on large strings) and wonder now
- Why can the results be printed but not reused?
- How would I write this command so that it can be further used?
This snippet runs as expected and generates the following pdf.
debug for part two
\newcounter{debugctr}
\setcounter{debugctr}{-1}
{\loop
\stepcounter{debugctr}
\edef\tmpinput{\the\numexpr \value{debugctr} - 0}
\def\tmpchar{\expandafter\lucidcharat\tmpinput}
(\tmpinput)[\tmpchar],
%\edef\tmpcharr{\tmpchar}
\show\tmpchar
%\typeout{tmpchar is \tmpchar}
\ifnum \value{debugctr}<3
\repeat }
The command lucidcharat
is defined as follows:
\newcommand{\lucidcharat}[1]{
\edef\tmpp{#1}
\IfEq{\tmpp}{0}{1}{}%
\IfEq{\tmpp}{1}{2}{}%
\IfEq{\tmpp}{2}{2}{}%
}
and is input to the other file with \input{sneakycode}
.
I assume this all has to do with how fickle xstring
is with regard to evaluation. To demonstrate what I mean with "results can not be further used" uncomment one of the commented lines above and you'll see that neither typeout
nor edef
work. They'll error out with
! Undefined control sequence.
\reserved@a ->\def \xs@dessep
{,}\@xs@readdecimalpart
l.617 \repeat
Please help me understand how a command using IfEq
internally would be written so that it can be reused e.g. for comparison of the output and why it is not working currently.
Here's the complete MCVE for copy-pasting again (It's the same as above, just put together into one file):
\documentclass{article} \usepackage[utf8]{inputenc}
\usepackage{xstring}
\newcommand{\lucidcharat}[1]{
\edef\tmpp{#1}
\IfEq{\tmpp}{0}{1}{}%
\IfEq{\tmpp}{1}{2}{}%
\IfEq{\tmpp}{2}{2}{}%
}
\begin{document}
debug for part two
\newcounter{debugctr}
\setcounter{debugctr}{-1}
{\loop
\stepcounter{debugctr}
\edef\tmpinput{\the\numexpr \value{debugctr} - 0}
\def\tmpchar{\expandafter\lucidcharat\tmpinput}
(\tmpinput)[\tmpchar],
%\edef\tmpcharr{\tmpchar}
\show\tmpchar
%\typeout{tmpchar is \tmpchar}
\ifnum \value{debugctr}<3
\repeat }
\end{document}
edef
is my problem? I mean, the command evaluates correctly when writing the result to the PDF\lucidacharat
is a fragile command (because of the def that you do and xstring does) so can not be used in a\typeout
unless prefixed by\protect
when it will just print as itself. You can probably do expandable tests instead of using xstring but you have not said what test you are intending.edef
is not my problem. Usingifnum
instead ofIfEq
does not help.) I guess it boils down to me not understanding fragility. Why is the def a problem?\def\foo{}
then\def
stays as the token\def
and\foo
gives an undefined command error as you are expanding it when it is not defined.