This question is related to (but distinct from) this question: Parsing a \$ as part of an improved \getargs command, which I asked earlier.
I am experiencing a residual space in a string result from which I am trying to remove all spaces, both regular spaces and hardspaces. The routine works great when the argument contains just regular spaces, it works when the argument contains hardspaces not in the leading position. But it fails to remove a leading hardspace. If I lead the argument with multiple hardspaces, it removes all but one.
I would like, if possible, to retain this logical approach (recursive) to the problem, since it is fast and it fits into a larger algorithm being developed. I'm not sure what makes a leading hardspace different from other hardspaces, or if my coding merely is not structured to catch a leading space (I thought it was).
Here is input (note: re-edited to minimize content for MWE):
\documentclass{article}
\usepackage{ifnextok}
\usepackage{ifthen}
\makeatletter
\def\stringend{$}
\def\add@space{\protected@edef\thestring{\thestring\@sptoken}}
\newcommand\noblanksF[2][v]{%
\def\thestring{}\expandafter\eat@Block#2\stringend%
\if v#1\thestring\fi}
\def\eat@Block{\IfNextToken\stringend%
{\@gobble}%
{\add@tostring{\eat@Block}}%
}
\def\add@tostring#1#2{%
\ifthenelse{\equal{#2}{~}}%
{}%
{\if\@sptoken#2\else\protected@edef\thestring{\thestring#2}\fi}%
#1}
\makeatother
\parindent 0in\def\bl{\rule{1ex}{1ex}}
\begin{document}
Testing noblanks: \\
\bl\noblanksF{This is a test with 0 leading spaces}\bl\\
\bl\noblanksF{ This is a test with 1 leading space}\bl\\
\bl\noblanksF{ This is a test with 2 leading spaces}\bl\\
\bl\noblanksF{ This is a test with 2 spaces (lead+everywhere)}\bl\\
FROM HERE OUT, RESULTS LEAVE ONE LEADING SPACE:\\
\bl\noblanksF{~This is a test with 1 leading hardspace}\bl\\
\bl\noblanksF{~~This is a test with 2 leading hardspaces}\bl\\
\bl\noblanksF{~~~This is a test with 3 leading hardspaces}\bl\\
\bl\noblanksF{~~~~This is a test with 4 leading hardspaces}\bl\\
\bl\noblanksF{~~This~~is~~a~~test~~with~~2~~hardspaces~~(lead+everywhere)}\bl
\end{document}
~through\protected@edefit becomes\protect \nobreakspace {}. – egreg Mar 07 '13 at 13:22mwe.styand put it between\makeatletter...\makeatotherinmwe.tex. (Also BTW: there is already amwepackage on CTAN). And the comments about things you deleted are distracting from the problem. See this answer to a question on meta about MWEs. – Matthew Leingang Mar 07 '13 at 14:35