0

Sometimes I create ad-hoc macros in the middle of my document for various reasons. However, the definitions sometimes cause extra space to be inserted. My questions are:

  • Why does this happen?
  • How can I prevent this from happening?

Here is an example:

\documentclass[varwidth]{standalone}

\begin{document} Lorem ipsum sit amet. Lorem ipsum dolor sit amet.

Lorem ipsum \newcommand\dolor{dolor} sit amet. Lorem ipsum \dolor{} sit amet. \end{document}

pdflatex output

  • 1
    well you are inserting two spaces: one before the definition and one after it. Use % to suppress one of them. E.g. \newcommand\dolor{sit}%. But generally it is not a good idea to do definitions like this, your document can get very fast quite messy. Keep them in one place, – Ulrike Fischer Apr 22 '21 at 08:31
  • Thank you for the quick answer. Adding a comment at the end of the line works. I'm still confused why \newcommand would insert an extra space. I've learnt that TeX ignores redundant whitespace around words, including a single newline. – MyComputer Apr 22 '21 at 08:45
  • It doesn't insert a space. It only prevents tex to merge the two space into one. You now have , and not simply . – Ulrike Fischer Apr 22 '21 at 08:47
  • That makes a lot of sense, thank you. How can I mark the question as solved? – MyComputer Apr 22 '21 at 08:49

1 Answers1

1

You are inserting two spaces: one before the definition and one after it (the new lines at the end).

 Lorem ipsum   %<--- end of line =  space
 \newcommand\dolor{dolor}  %<--- end of line =  space
 sit amet. Lorem ipsum \dolor{} sit amet.

Normally tex merges two spaces into one, but the \newcommand prevents this as you now have <space><something><space>, and not simply <space><space>, even if the <something> doesn't led to some visual output.

Suppress one of the spaces with %:

 Lorem ipsum
 \newcommand\dolor{dolor}%
 sit amet. Lorem ipsum \dolor{} sit amet.

Or better move the `\newcommand to a better place. Using that in the middle of a paragraph leads to messy code.

Ulrike Fischer
  • 327,261