7

I'm trying to develop a macro that gives two different outputs depending on whether it is in math mode (i.e. $ $ or \begin{equation} ... \end{equation}) or anywhere else. The reason is that I use a lot of physical variables and molecule names such as e.g. $^{12}$CO(1-0) and use macros to be able to have them typeset uniformly troughout the text. I use them in normal text as well as math environment. Right now I have two macros one for text:

\newcommand{\twCO}{%
$^{12}$CO(1-0)\xspace%
}%

and one for math with out the math signs:

\newcommand{\twCO}{%
^{12}CO(1-0)%
}% .

I would like to come up with something more generic:

\newcommand{\twCO}{%
\ifinmathmode%
 ^{12}CO(1-0)%
\else%
 $^{12}$CO(1-0)\xspace%
}

Searching quite some time on TeX and a prominent search provider, I could not find a satisfying answer to this, only more complex constructions to do the check. But I'd like to ask if someone is aware of a straightforward check to see which environment one is in?

EDIT:

After trying out the options given in the answer and comments below, I found that the easiest and best setup for my situation in which I also want the new commands to respect boldface and italic (which I figured out after I asked...) from the environment is to use text from amsmath which was pointed out by @egreg in the comments to his answer. Thus the best solution to my question is to use something like:

\usepackage{amsmath}
\newcommand{\twCO}{\text{\textsuperscript{12}CO}}

To set Isotopes however mhchem, which was pointed out in the comments, is really usefull.

However for someone that want to test for math mode, the second answer is working well. Or one could use \ensuremath, also mentioned in the comments. This however does not respect boldface or italics from the environment.

Ascurion
  • 429

2 Answers2

5

If you use $^12$CO(1-0) in text mode and ^12CO(1-0) in math mode you surely get *different results. The letters CO change shape and the hyphen becomes a minus sign in math mode.

If you really want uniform results in text and math mode, do

\newcommand{\twCO}{\textnormal{\textsuperscript{12}CO(1-0)}}

This actually means using text mode also in math.

Add \xspace if you really want it; I never recommend it.

egreg
  • 1,121,712
  • 1
    Great thanks that was quick and easy, I was not aware of the \textnormal command and was searching something way to complicated. Just out of curiosity, why would you not use \xspace? – Ascurion Jul 07 '13 at 23:27
  • @Ascurion I don't think it's necessary. Just use \twCO{} in text mode. – egreg Jul 07 '13 at 23:32
  • Thanks for the info! I am really amazed how quick, helpfull and friendly everybody is on TeX. – Ascurion Jul 07 '13 at 23:40
  • 1
    Wouldn't it be better to use mhchem in this case? At least it is quite good at typesetting isotopes etc. – daleif Jul 08 '13 at 08:22
  • @daleif Possibly, or maybe certainly. – egreg Jul 08 '13 at 08:40
  • @dalei thanks for the hint! I actually switched to mhchem to do the isotopes and molecule typesetting part and some chemical formulas I wrote by hand. However, my question also included physical units such as, e.g. L$_{\odot}$ where mhchem cant help. – Ascurion Jul 08 '13 at 19:39
  • @egreg I switched the accepted answer since, although your solution is more generic and clean I found in the end that it ignores the surrounding format and thus does not come out correctly in e.g. section headers, as I explained in my Question. Is there a way to circumvent this? – Ascurion Jul 09 '13 at 10:51
  • 1
    @Ascurion I don't think this should respect the format (boldface, in particular) since it's a symbol; however, just use \textup instead of \textnormal or, if you want also to respect italic, just \text (requires amsmath). – egreg Jul 09 '13 at 11:07
  • @egreg I guess you are right! But in my field, Astronomy, its quite common to use molecules in the section headers and they are normally set in boldface as well. I just checked some books to be sure. :) But using \text this is working as I intend it too. Thanks again! – Ascurion Jul 09 '13 at 17:55
1

@egreg showed you how to achieve uniformity. But you also asked about testing whether you were in math mode:

Then you'll want to do something like:

\newcommand{\test}{%
  Hello:
  \ifmmode%
    this is math
  \else
    this is not math
  \fi
}

Your example command could then be rewritten as

% requires `amsmath` for `\text{...}`
\newcommand{\twCO}{%
  \relax
  \ifmmode
    ^{12}\text{CO(1-0)}%
  \else
    $^{12}$CO(1-0)%
  \fi
}

Or, as also suggested by egreg,

\DeclareRobustCommand{\twCO}{%
  \ifmmode
    ^{12}\text{CO(1-0)}%
  \else
    $^{12}$CO(1-0)%
  \fi
}

Though before defining macros which test for math mode, you might want to be aware of various, potential complications

A.Ellett
  • 50,533
  • Put \relax before \ifmmode; you'll see why if you use \twCO as the first element in an array cell. Or use \DeclareRobustCommand. – egreg Jul 07 '13 at 23:40
  • @egreg Ick! What's going on? – A.Ellett Jul 07 '13 at 23:44
  • @A.Ellet Oh yes thanks, this is what I was frustratingly trying to find. Good to know how to check for math mode. I think I stick to egregs solution for my current application, though, since it is more straight forward in terms of implementation and uniformity for my application in the end. – Ascurion Jul 07 '13 at 23:58
  • @A.Ellett TeX tries to expand the first token in a cell, in order to see if it has to use \omit (usually buried in a \multicolumn and it is in horizontal mode at the time; so the expansion of \ifmmode follows the "false" branch and TeX decides there's no \omit. Now it inserts the $ commanded by array and so the input is equivalent to $$^12... which gives an error because $$ in horizontal mode just makes an empty formula and TeX finds ^ outside math mode. The \relax is not expandable, so the conditional is not untimely expanded. – egreg Jul 08 '13 at 08:45
  • what's wrong with \ensuremath? (not an answer to the question, but neither are many of the options offered so far.) – wasteofspace Jul 08 '13 at 10:37
  • @wasteofspace I tried \ensuremath but in my experience this ignores the surrounding environment. Thus, e.g. using an Isotope in a section header renders it not bold. – Ascurion Jul 09 '13 at 10:40