I apologize that this question may seem very much like several other recent ones I have asked (Parsing leading hardspaces, Parsing a \$ as part of an improved \getargs command). They are all related to using a recursive TeXnique to parse strings. David Carlisle has already helped a great deal in the prior queries in pointing me to \protected@edef
to handle special (e.g., \$
) characters and in using \let
to redefine hardspaces to something innocuous (either nulls or soft spaces).
But now I find the algorithm chokes on diacritical marks like umlauts (\"u
). I guessing that these are not "characters" but expandable "macros" which might explain why an algorithm that searches character by character may choke. So I'm looking for a clever workaround, something like a temporary \let
or \catcode
redefinition that would allow the algorithm to digest these types of "characters".
So the code is below. I commented out the one line (2nd from end) which will cause the algorithm to choke. What the algorithm does is looks at the argument and parses it into single words. It places the first word in \argi
, the second in \argii
, etc. and the total number of words in \narg
. And its much faster than its predecessor in the stringstrings
package. It works great with alphanumeric inputs, special characters, hard and soft spaces, but not (yet) with diacritical marks.
\documentclass{article}
\makeatletter
\usepackage{ifnextok}
\usepackage{ifthen}
\def\string@end{$}
\def\converttilde{T}
\newcounter{arg@index}
\let\SaveHardspace~
\def\getargsF#1{%
\if T\converttilde\def~{ }\fi%
\protected@edef\the@string{#1}%
\def\argi{}\setcounter{arg@index}{1}%
\expandafter\parse@Block\the@string\string@end%
\let~\SaveHardspace%
}
%ifthenelse needed when leading spaces end up as \argi
\def\parse@Block{\IfNextToken\string@end%
{\edef\narg{\arabic{arg@index}}\@gobble}%
{\IfNextToken\@sptoken{\addtocounter{arg@index}{1}%
\ifthenelse{\equal{\argi}{}}{\addtocounter{arg@index}{-1}}{}%
\expandafter\def\csname arg\roman{arg@index}\endcsname{}%
\add@to{\parse@Block}}%
{\add@to{\parse@Block}}}}
\def\add@to#1#2{\if\@sptoken#2\else\expandafter\protected@edef%
\csname arg\roman{arg@index}\endcsname%
{\csname arg\roman{arg@index}\endcsname#2}\fi#1}
\makeatother
\begin{document}
\getargsF{mein hut} \argi~\argii $<$NO PROBLEM\\
BUT PUT UMLAUT ON THE u AND IT BREAKS
%\getargsF{mein h\"ut} \argi~\argii
\end{document}