# Why does declaring a command in-document produce horizontal space?

I'm really confused about why there is some extra horizontal space that appears before the second "Test" on my second line:

\documentclass[12pt]{article}
\usepackage[english]{babel}

\begin{document}

Test Test

Test \newcommand{\foo}{} Test

\end{document}


• You have a space before the \newcommand and before the final Test, both of which are actualized. If you had two consecutive spaces, it would be treated as a single space, but that is not the case here. – Steven B. Segletes Apr 5 '16 at 1:30
• Okay, so eliminating the space cures this, but makes the code less readable. Is there a solution to that? – A Feldman Apr 5 '16 at 2:25
• @AFeldman After playing around, it seems like one possibility is to keep the space before \newcommand{\foo}{}, but write a % immediately afterwards and then put the second Test on the next line of code. It seems more readable to me, at least. – justin Apr 5 '16 at 2:40

as pointed out by Steven Segletes, only consecutive spaces are compressed into a single space, so a \newcommand in the middle of text, with a space on either side, will result in a wider space in the output.

if you are looking for readability in the source file, you can avoid the spacy output at the cost of more lines in the source:

Test
\newcommand{\foo}{}%
Test


the same spacy result is often caused by inserting multiple \index entries for the same text in a source file, and can be solved in the same way:

some text
\index{index item}%
\index{item to be indexed}%
item to be indexed


i know you probably haven't reached that problem yet, but it's a good idea to learn good habits early.