59

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.

7
  • 1
    \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, 2011 at 13:28
  • 1
    @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. Apr 5, 2011 at 13:30
  • 1
    @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, 2011 at 13:33
  • 4
    @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}. Apr 7, 2011 at 8:11
  • 5
    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, 2011 at 23:38

3 Answers 3

51

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}
3
  • 16
    Actually, e.g. & i.e. can occur at the end of a sentence―this very sentence, e.g. Jul 8, 2012 at 12:20
  • 2
    @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. Jul 8, 2012 at 13:07
  • 4
    @MartinScharrer, it is an obscure usage, which is why I used it in my comment above for illustration. Jul 8, 2012 at 13:40
17

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.)

1
  • This looks like the better option to me. Would you mind to add an example of how to use xpunctuate? Jan 31, 2019 at 13:15
1

After reading @J. C. Salomons answer I think xpunctuate is suited best for this task. Since I do not need the extra functionality from the foreign package, I decided against using that one.

The implementation of the macro provided by the package was not trivial for me, so I expect others might struggle with this as well. Here is my definition of an \eg macro, using the xpunctuate package (make sure to load it in the preamble):

\providecommand{\eg}[0]{e.g\xperiod}

Explanation:

  • \providecommand defines a new macro if the macro name is not already used. Otherwise it will issue an error. I choose this to not unknowingly overwrite an important macro of a different package which might break its functuality.

  • This is followed by the macro name: {\eg}

  • Then the number of arguments is defined, in this case [0]. This is optional, however I like to make it explicit.

  • Last, the macro is defined. It consists of e.g, followed by xpunctuate's macro \xperiod. This will insert a period and an interword space if the macro is followed by another word, a period and no space if it is followed by punctuation and nothing if it is followed by a period, i.e. at the end of a sentence.

This should handle all different usecases for abbreviations correctly.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .