15

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?

5
  • This question pretty seems to be the same as tex.stackexchange.com/questions/2229/… . If you don't want the bigger space after the period, enter a \ after e.g., like e.g.\ rugby
    – Matten
    Commented Dec 10, 2010 at 11:01
  • 2
    @Matten: I disagree. The point of this question is how to have a macro put in the space. The point of that question is how much space to put in (and how to control it). Commented Dec 10, 2010 at 11:12
  • ok, you're right
    – Matten
    Commented Dec 10, 2010 at 11:19
  • 2
    I know this is orthogonal to your question, but IMO if you care enough about your document that you want to control the spacing after periods, then you should care enough about the text to avoid abbreviations like "e.g." Commented Dec 10, 2010 at 14:42
  • @Thomas: You might also be interested in this answer to a related question. Commented Dec 10, 2010 at 14:55

1 Answer 1

10

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}
6
  • 10
    Actually, the spacing here is incorrect. Thanks to Will's comment on an answer to tex.stackexchange.com/questions/2229/…, the correct definition is \newcommand*{\eg}{e.g.\@\xspace} Commented Dec 10, 2010 at 11:13
  • 2
    @Andrew: This still doesn't handle punctuation that needs double spacing at the end of a sentence, e.g., in "There were all sorts of farmyard animals there: rabbits, pigs, \etc". Some code of mine from SO does handle this, although it doesn't treat "!" and "?", and its use of expl3 makes it a bit heavyweight. Commented Dec 10, 2010 at 20:15
  • @Charles: ouch! Mind you, the example given was e.g. and I can't think of that going at the end of a sentence. Commented Dec 10, 2010 at 20:23
  • 3
    @Andrew: "Don't use the three most common Latin abbreviations, i.e., etc., i.e., and e.g." Commented Aug 17, 2011 at 11:58
  • @Bruno: Very witty. Commented Aug 17, 2011 at 18:03

You must log in to answer this question.