I have a command that occasionally produces whitespace that I don't want.
\def\testa{a}
\def\testb{a}
\newcommand{\mycommand}[1]{\ifx\testa\testb{#1}\fi}
When \testa and \testb are different, I wind up getting unwanted whitespace in the following example:
Testing \mycommand{This} \mycommand{is} \mycommand{my} \mycommand{trial} \mycommand{run.} Testing
I thought I could do something clever like
\def\sweetnothing{}
\def\testa{a}
\def\testb{b}
\newcommand{\mycommand}[1]{\ifx\testa\testb{#1}\else\sweetnothing\fi}
I thought this would make the test line above equivalent to the following
Testing \sweetnothing \sweetnothing \sweetnothing \sweetnothing Testing
But it's not.
I tried some \expandafter magic
\newcommand{\mycommand}[1]{\ifx\testa\testb{#1}\else\expandafter\sweetnothing\fi}
Since I thought that would essentially unwind to \sweetnothing followed by a blank space.
No such luck.
Here's a M(non)WE:
\documentclass{article}
\makeatletter
%%
\def\testa{a}
\def\testb{b}
\newcommand{\mycommand}[1]{\ifx\testa\testb{#1}\fi}
%%
\def\sweetnothing{}
\newcommand{\mycommandvar}[1]{\ifx\testa\testb{#1}\else\sweetnothing\fi}
%%
\makeatother
\pagestyle{empty}
\begin{document}
\textbf{Line 1:} Testing \mycommand{This} \mycommand{is} \mycommand{my} \mycommand{trial} \mycommand{run.} Testing
\textbf{Line 2:} Testing \mycommandvar{This} \mycommandvar{is} \mycommandvar{my} \mycommandvar{trial} \mycommandvar{run.} Testing
\textbf{Line 3:} Testing {} {} {} {} {} Testing
\textbf{Line 4:} Testing \sweetnothing \sweetnothing \sweetnothing \sweetnothing Testing
\end{document}
I really wanted all three lines to look like line 4 when \testa and \testb are different.
Then I went off the deep end and tried something along the lines of
\documentclass{article}
\makeatletter
\def\testa{a}
\def\testb{b}
\def\eat@white@space#1{\ifcat #1\relax\else#1\fi}
\newcommand{\mycommand}[2]{\ifx\testa\testb{#1#2}\else\expandafter\eat@white@space\fi}
\makeatother
\pagestyle{empty}
\begin{document}
Testing \mycommand{This} \mycommand{is} \mycommand{my} \mycommand{trial} \mycommand{run.} Testing
\end{document}
But this had completely unexpected results. I didn't really expect it to work, but I didn't expect to quite fail like this either.

What I thought I was doing was comparing the catcode of a space to the catcode passed of the first argument. If they were the same, then I wanted to discard the second argument (at that point there should be a recursive call---which I don't do). If they are not the same, then I want to keep the first argument.
Any ideas?



\newcommand{\mycommand}[1]{\ifx\testa\testb{#1}\else\unskip\fi}, and\newcommand{\mycommandvar}[1]{\ifx\testa\testb{#1}\else\sweetnothing\unskip\fi}do what you are looking for? – Peter Grill Feb 22 at 2:36\unskiponly applies to trailing glue on the line. Since I've got more text to come, what is it doing? – A.Ellett Feb 22 at 2:44