# Macros for common abbreviations

Common abbreviations are surprisingly tricky to format correctly. I'd like some simple macros for them, to be used like so:

You should eat more fruit, \eg apples, bananas, oranges, \etc.


There are (at least) three (interrelated) problems to overcome in defining this:

1. If used at the end of a sentence, we end up with doubled-up periods. Simply leaving off the period in the macro seems ugly (especially for things like 'e.g.', 'i.e.', ...). A better solution calls for something like \consumeif{.}, but I can't find a beast.
2. The macro should include the behavior of \xspace.
3. It should also produce correct spacing depending on whether it ends a sentence, like the logic of \@.

How would I (you) write such a macro? Note: I'm aware of this question and others like it, but the answers don't resolve my problems.

-
\etc doesn't even save you any keystrokes!? I think I just wouldn't bother. Do you really need a macro to save you from writing e.g. or more properly e.g.\  –  Seamus Apr 5 '11 at 13:28
@Seamus I like to use \eg to expand to, literally, ‘for example’, but sometimes to use the short-hand abbreviation in a paper or some such. –  Will Robertson Apr 5 '11 at 13:30
@Will I think that sort of thing could more fruitfully be handled by your editor of choice. I know a guy who has emacs automatically change  st  into  spacetime  ... –  Seamus Apr 5 '11 at 13:33
@Seamus True, but I like to italicize most abbreviations. (Yes, many style guides disagree with this.) A macro is definitely better than writing out \textit{i.e.\@\xspace}. –  eegg Apr 7 '11 at 8:11
On another note, I like to add an \, in acronyms like "e.g." or "i.e.". This makes it clear that there are actually two words behind it, but the space doesn't look too big, as a regular space or ~ would. (The German typographical quick reference typokurz recommends this as well.) –  doncherry May 19 '11 at 23:38

I think the \eg and \ie macros should be defined differently to \etc, because they will never be at the end of a sentence. You can simply use \@\xspace at the end to ensure a space with the correct width if one is required. For \etc you can use \@ifnextchar{.}{<yes>}{<no>} to check for a following full stop.

\documentclass{article}

\usepackage{xspace}
\newcommand*{\eg}{e.g.\@\xspace}
\newcommand*{\ie}{i.e.\@\xspace}

\makeatletter
\newcommand*{\etc}{%
\@ifnextchar{.}%
{etc}%
{etc.\@\xspace}%
}
\makeatother

\begin{document}
\noindent
You should eat more fruit, \eg apples, bananas, oranges, \etc. Next sentence.

\noindent
You should eat more fruit, \eg, apples, bananas, oranges, \etc but also tomatoes.

\end{document}

-
This is simple and perfect. Thank you. –  eegg Apr 5 '11 at 13:39
Actually, e.g. & i.e. can occur at the end of a sentence―this very sentence, e.g. –  J. C. Salomon Jul 8 '12 at 12:20
@J.C.Salomon: I'm not an English native speaker myself. However, I never have seen it used at the end of an sentence so far. –  Martin Scharrer Jul 8 '12 at 13:07
@MartinScharrer, it is an obscure usage, which is why I used it in my comment above for illustration. –  J. C. Salomon Jul 8 '12 at 13:40

Phil Ratcliffe has recently posted to CTAN two packages to deal with this question:

• xpunctuate “provides the commands \xperiod, \xcomma and \xperiodcomma, which follow a similar procedure to that of \xspace, and insert punctuation if and only if it is necessary,” and
• foreign which, among other things, uses the commands from xpunctuate to define \eg and \ie as the OP requests.

These commands attempt to correctly handle sentence spacing via a simple convention, better illustrated than described:

• The forest is filled with lions, tigers, bears, \etc.” becomes
The forest is filled with lions, tigers, bears, {\em etc}.
• The forest is filled with lions, tigers, bears, \etc!” becomes
The forest is filled with lions, tigers, bears, {\em etc}.!
• Lions, tigers, bears, \etc fill the forest.” becomes
Lions, tigers, bears, {\em etc}.\@ fill the forest.

(Actually, the font selection is a bit more complicated than I’ve shown.)

There’s even code that inserts a comma after mid-sentence \ie & \eg. (This comma can be disabled via the [UKenglish] option.)

-