views:

1538

answers:

3

Hi,

When I assign a new command and call it at the beginning of a \par the space between the variable text and next word is missing.

\newcommand{\testcmd}{This is a test}

\par \testcmd foobar.

Will be rendered as:

This is a testfoobar.

\par foo \testcmd bar.

Renders fine as: foo This is a test bar.

Anyone come across this before and have a solution?

Thanks

+2  A: 

I do not know the exact thing which is going on here but there are several ways to get that space back:

  1. \newcommand{\testcmd}{This is a test } % <- space before closing brace
  2. par \testcmd{} foobar % <- note {}
  3. The most verbose but the most robust way too:

    \usepackage{xspace}

    \newcommand{\testcmd}{This is a test\xspace}

Laurynas Biveinis
What's going on is that the TeX lexer considers any amount of white space to terminate the command and gobbles all of it. If it didn't, you would have to be careful about spaces after every command invocation.
Jouni K. Seppänen
+1  A: 

Actually a much simpler answer would be to:

\newcommand{\testcmd}{This is a test}

\par \testcmd \ foobar.

Notice the extra "\ " before foobar (slash and space). No extra package needed. It is the same as the most common method for things like:

Mr.\ Smith
etc.\ and
Proc.\ Amer.\ Math.\ Soc.
bastijn
I believe it is customary in (La)TeX to write "Mr. Smith" as "Mr.~Smith" to avoid placing a line break between the title and the surname.
las3rjock
>Normally latex allows you to indicate that you don't want theinter-sentence spacing in these circumstances by either escaping the space with a \ "Mr.\ Smith" or by inserting a non-breaking space with ~ "Mr.~Smith." The ~ is frequently the preferred method because it prevents line breaks between a title and name, or in the middle of an abbreviation, like a citation.Hm never knew that option, thanks for the info.
bastijn
A: 

\usepackage{xspace}

\newcommand{\testcmd}{This is a test\xspace}

I had the same problem. That worked great, thanks!