2

Possible Duplicate:
Space after LaTeX commands

I`ve 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 I`ve read something about it, but my searches ended in vain.

3

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:

enter image description here

\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}

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