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.

N3buchadnezzar
  • 11,348
  • 7
  • 55
  • 114
  • 1
    See http://tex.stackexchange.com/questions/31091/space-after-latex-commands. If this answers your question, please leave a comment so that we can close this question and link it to the previous one. – yannisl Oct 24 '11 at 06:41

1 Answers1

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}
Werner
  • 603,163