# latex command has extra space

I'm writing a command to deal with bacteria names. They're written in full the first time, then with just the first name and a period following it. I've managed what I have by cobbling together others' examples, though I seem to have an extra space. I'm ignoring for now that sometimes the second name is shortened as well.

MWE

\documentclass[letterpaper,10pt]{scrartcl}
\usepackage{etoolbox}

\def\splitfirstchar#1#2\sentinel{#1.}
\newcommand{\first}[1]{\splitfirstchar#1\sentinel}

\def\testwords#1{%
}
\ifcsundef{bactused}{
\def\bactused{true}%want full name
\textit{#1 #2}
}{
\textit{\first{#1} #2}
}
%\relax
}
\def\ecoli{\testwords{Escherichia coli}}
\begin{document}
\ecoli
\ecoli\ecoli. Blah
\end{document}


Produces:

So where is the extra space coming from?

-

You need % at the ends of your lines that containt \textit{...} and the line that ends with {

-
Huh. Good to know. I'm still only just beginning to start out with programming in LaTeX. I never understood why everyone stuck %s at the end of so many lines which didn't look like they needed it. –  EricR Feb 13 '12 at 16:46
@EricR: A end-of-line character (i.e. a line break) is normally taken as a space. If you add % at the end it is commented out. Note that any spaces after a macro is removed, so you don't need to add % in such lines. An exception are the single character macros like \_ etc. which do not delete following spaces. –  Martin Scharrer Feb 13 '12 at 16:53
I realised the line break was a space I guess I just didn't think about that when writing a command. Cheers mate. –  EricR Feb 13 '12 at 17:04

TeX considers a <return> as whitespace just like it does a number of consecutive spaces. So, in your instance, there's actually a return after every use of \ecoli. The % "removes" this <return>.

From the TeX Book regarding the SHORT STORY by A. U. Thor (Chapter 6 Running TeX, p 26):

The % sign here is a feature of plain TeX that we haven't discussed before: It effectively terminates a line of your input file, without introducing the blank space that TeX ordinarily inserts when moving to the next line of input. Furthermore, TeX ignores everything that you type following a %, up to the end of that line in the file; you can therefore put comments into your manuscript, knowing that the comments are for your eyes only.

-