[Tex/LaTex] Abbreviations in math mode

math-modespacingxspace

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

Best Answer

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}
Related Question