# 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.

-
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 1, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "math_score": 0.9674559831619263, "perplexity": 1910.4084253529352}, "config": {"markdown_headings": true, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 10, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "plain"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "warc_path": "s3://commoncrawl/crawl-data/CC-MAIN-2015-22/segments/1432207929176.79/warc/CC-MAIN-20150521113209-00055-ip-10-180-206-219.ec2.internal.warc.gz"}