# Error in spacing when using custom command [duplicate]

Possible Duplicate:
Space after LaTeX commands

Ive made a few very short commands, and they work, alas the spacing after them is wrong.

\newcommand{\yaksen}{$y$-aksen}
\newcommand{\xaksen}{$x$-aksen}
\newcommand{\zaksen}{$z$-aksen}


This produces no space after the commands, so if i type

this is an example \yaksen, is the norwegian term for the y-axis.
and the \zaksen is perpendicular to both the \xaksen and the \yaksen.


The output is

this is an example y-aksen, is the norwegian term for the y-axis.
and the z-aksenis perpendicular to both the x-aksenand the y-aksen.


However, putting blank spaces after the commands. Like this

\newcommand{\yaksen}{$y$-aksen }
\newcommand{\xaksen}{$x$-aksen }
\newcommand{\zaksen}{$z$-aksen }


Produces this

this is an example y-aksen , is the norwegian term for the y-axis.
and the z-aksen is perpendicular to both the x-aksen and the y-aksen .


Is there any easy way to obtain correct spacing? I think Ive read something about it, but my searches ended in vain.

-

## marked as duplicate by Werner, Yiannis Lazarides, Jake, lockstep, Alan MunnOct 25 '11 at 13:42

You need to use \xspace provided by the xspace package. It checks to see whether the character following a command sequence is a space or not, and inserts a space accordingly:

\documentclass{article}
\usepackage{xspace}% http://ctan.org/pkg/xspace
\newcommand{\yaksen}{$y$-aksen\xspace}
\newcommand{\xaksen}{$x$-aksen\xspace}
\newcommand{\zaksen}{$z$-aksen\xspace}
\begin{document}
this is an example \yaksen, is the norwegian term for the y-axis.
and the \zaksen is perpendicular to both the \xaksen and the \yaksen.
\end{document}

-
thanks! It worked =D –  N3buchadnezzar Oct 24 '11 at 6:53