8

I am trying to adapt Martin Scharrer's solution from Macros for common abbreviations to be able to use them in math mode. Yes I realize I am about to incur some criticism that I should just use \text{}, but I'd prefer the macros to do the thinking for me.

  1. I have three different versions of the \ie macro below and not sure which one is the correct one to use. The third one (\ieC, which is what I have been using) seems to yields slightly different spacing.

    enter image description here

  2. I am not able to figure out how to adapt the \etc macro for math mode to properly handle the case where there is a subsequent period as in \etc.. Furthermore, it appears as if the addition of \ifmmode screws up the \etc macro such that it no longer works properly even in text mode if there is a trailing period.

    enter image description here

If there was a version of the \xspace macro that worked in math mode that would be great.

References:

Code:

\documentclass{article}

\usepackage{amsmath}
\usepackage{xspace}

\newcommand*{\ieA}{%
\ifmmode%
    \text{i.e.}\ %
\else%
    i.e.\@\xspace%
\fi%
}%

\newcommand*{\ieB}{%
\ifmmode%
    \text{i.e.~}%
\else%
    i.e.\@\xspace%
\fi%
}%

\newcommand*{\ieC}{%
\ifmmode%
    \text{i.e. }%
\else%
    i.e.\@\xspace%
\fi%
}%


\makeatletter
\newcommand*{\etc}{%
    \ifmmode%
        \@ifnextchar{.}{\text{etc}}{\text{etc.}}%
    \else%
        \@ifnextchar{.}%
            {etc}%
            {etc.\@\xspace}%
    \fi%
}%
\makeatother

\begin{document}\noindent
    $\ieA x < 2$ \quad \verb|\text{i.e.}\ |\par\noindent
    $\ieB x < 2$ \quad \verb|\text{i.e.~}|\par \noindent
    $\ieC x < 2$ \quad \verb|\text{i.e. }|\par\noindent
    
    \medskip\noindent
    $a \ne 0, b \ne 0, \etc$\par\noindent
    $a \ne 0, b \ne 0, \etc.$\par\noindent

    \medskip\noindent
    $a \ne 0, b \ne 0, \text{\etc}$ \quad \verb|\text{\etc}|\par\noindent
    $a \ne 0, b \ne 0, \text{\etc.}$\quad \verb|\text{\etc.}|\par\noindent

    \bigskip
    \noindent
    Outside of math mode \verb|\etc| no longer works at the end of sentence
    
    \noindent
    You should eat more fruit, \ieA apples, bananas, \etc. Next sentence.
    
    \noindent
    You should eat more fruit, \ieA, apples, bananas, \etc but also tomatoes.
\end{document}

The above MWE was for testing purposes, so I agree that the abbreviations there are not necessary to be within the math environment in those cases. But for my actual use case is in display mode where it is clearer to put that text as part of the formula as opposed to on the next line. For example:

enter image description here

3
  • 1
    Why should "etc." and "i.e." go in math mode? Your last formula is actually two formulas followed by text.
    – egreg
    Jun 15, 2012 at 18:56
  • I hope it's OK if I'm curious: does my solution for \etc work for you, or is there any way I could improve my answer? Jun 23, 2012 at 11:13
  • @HendrikVogt: Your solution works great. I had attempted to adapt it to also work for \ie an \eg (without the check for the . since these are not intended to be used at the end of sentence). Initially this appeared to not be working, so had intended to come back to find out why. Turns out I had an extra brace group that was causing problem, so your solution now works for me in all the cases I can think of. Thanks. Jun 24, 2012 at 2:58

2 Answers 2

5

Your third version \ieC produces larger spacing since you have in text mode, i.e., the period ends a sentence. Usually one would probably write i.e.\ $x<2$, which corresponds to your first version \ieA. But it's up to you what you choose. Maybe in display math you'll prefer the larger spacing.


As for your second question, your \etc doesn't work because it lacks \expandafter. You test the next character with \@ifnextchar, but have a look at your code: When in math mode, you test the \else, and else you test the \fi! I know, this is the point where programming TeX can drive you crazy. What you have to do: use \expandafter to get rid of \else and \fi so that the \@ifnextchar can actually see the next character.

Just as a side note: you should put some space between a \ne 0, and b \ne 0. Moreover, your code really contains a lot of %s that could be omitted. Here's my solution for \etc:

\documentclass{article}

\usepackage{amsmath}
\usepackage{xspace}

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

\newcommand*{\etc}{%
    \ifmmode
        \expandafter\mathetc
    \else
        \expandafter\textetc
    \fi
}

\begin{document}\noindent
    $a \ne 0,\ b \ne 0,\ \etc$\par\noindent
    $a \ne 0,\ b \ne 0,\ \etc.$\par\noindent

    \medskip\noindent
    $a \ne 0,\ b \ne 0,\ \text{\etc}$ \quad \verb|\text{\etc}|\par\noindent
    $a \ne 0,\ b \ne 0,\ \text{\etc.}$ \quad \verb|\text{\etc.}|\par\noindent

    \bigskip
    \noindent
    Outside of math mode \verb|\etc| works now at the end of sentences!

    \noindent
    You should eat more fruit, i.e.\ apples, bananas, \etc. Next sentence.

    \noindent
    You should eat more fruit, i.e., apples, bananas, \etc but also tomatoes.

\end{document}
4

Your premises are fallacious. A TeX input such as

$a\ne0, b\ne0, \etc$

is simply wrong. Those are two formulas followed by text:

$a\ne0$, $b\ne0$, \etc

(one should ask whether the abbreviation "etc." is really good, but it's another problem; I never use it).

To answer the problem at hand, I'd say that

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

is what you're looking for. The space factor setting and the space possibly inserted by \xspace are ignored anyway in math mode. I assume that \etc will never appear in subscripts or superscripts, that would be too much. :)

Similarly, i.e.,~$x<2$ would be the correct input. Or, without the comma, i.e.~$x<2$. Putting the "i.e." inside the formula is wrong: it's no math. And in this way you have no problem with spaces. The definition for "i.e." can be

\newcommand{\ie}{i.e.,\xspace}

or

\newcommand{\ie}{i.e.\@\xspace}

depending on whether you want the comma or not.

8
  • Agreed! Putting this in two formulas is a lot better. However, it's interesting to note that $a\ne0$, $b\ne0$ and $a\ne0,\ b\ne0$ (which I proposed) produce different spacing. Personally, I prefer the larger spacing of the second variant. Since I also prefer two separate formulas, I write $a\ne0$,\, $b\ne0$. Jun 15, 2012 at 19:26
  • @HendrikVogt the first has the normal space after a comma, the second has the thin space after a punctuation atom and a "spacefactor immune" space. You probably are using \frenchspacing. But the first is right, the second isn't. :)
    – egreg
    Jun 15, 2012 at 19:31
  • Yeah, I know that it's from the thin space after the comma - that's why I used the thin space \, :-) I'm not sure why \frenchspacing would play a role here. As for right or wrong, I just think that the second version is more reader friendly, in particular if the formulas are more complicated like Let $n\in\N$,\, $f\colon[a,b]\to\R$ and ... (call it a manual \mathsurround). Jun 15, 2012 at 19:45
  • @HendrikVogt With \nonfrenchspacing the space after a comma is slighly larger than the interword spaces in the line, since \sfcode`,=1250 (with LaTeX default setting).
    – egreg
    Jun 15, 2012 at 19:51
  • Oops? I get exactly the same spacing when I test \nonfrenchspacing a, b \par \frenchspacing a, b! Jun 15, 2012 at 19:56

You must log in to answer this question.

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