[Tex/LaTex] Abbreviations and spacing

macrosspacingxspace

I'm sure this is a pretty standard questions that's been asked (and answered!) a hundered times before, so apologies for boring people.

In LaTeX, I'd like to write a macro for abbreviations, such as, "e.g." or "i.e." but I'd like LaTeX to get the spacing after the trailing dot right: sometimes I enclose the "e.g." in commas and sometimes I don't, depending on the document I'm writing:

A lot of fruits contain vitamin C, e.g., lemons and oranges.

To practice a sport like e.g. rugby, a good level of fitness is adviced.

(Please let's not discuss whether this use of commas is correct or not.)

How can I write a macro \eg that gets the spacing right automatically for both cases?

Best Answer

You can use the package xspace for this purpose:

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[latin1]{inputenc}
\usepackage{xspace}
\newcommand*{\eg}{e.g.\xspace}

\begin{document}
A lot of fruits contain vitamin C, \eg, lemons and oranges.

To practice a sport like \eg rugby, a good level of fitness is adviced.
\end{document}
Related Question