macros – Best Macros for Common Abbreviations in LaTeX

acronymsmacrosspacingxspace

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.

Best Answer

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}