TransWikia.com

macro definition in document body with `newcommand` inserts spaces

TeX - LaTeX Asked by user100361 on June 9, 2021

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
  newcommanddolor{dolor}
  sit amet. Lorem ipsum dolor{} sit amet.
end{document}

pdflatex output

One Answer

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
 newcommanddolor{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
 newcommanddolor{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.

Correct answer by Ulrike Fischer on June 9, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP