TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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}

enter image description here

share|improve this question
6  
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 9 hours ago
    
Okay, so eliminating the space cures this, but makes the code less readable. Is there a solution to that? – A Feldman 8 hours ago
    
@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 7 hours ago
up vote 11 down vote accepted

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.