# Macro with environmental dependent output

I'm trying to develop a macro that gives two different outputs depending on whether it is in math mode (i.e.  or $$...$$) 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.

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.

• 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 7 '13 at 23:27
• @Ascurion I don't think it's necessary. Just use \twCO{} in text mode. – egreg Jul 7 '13 at 23:32
• Regarding xspace see Drawbacks of xspace – A.Ellett Jul 7 '13 at 23:36
• Wouldn't it be better to use mhchem in this case? At least it is quite good at typesetting isotopes etc. – daleif Jul 8 '13 at 8:22
• @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 9 '13 at 11:07

@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

• 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 7 '13 at 23:40
• @egreg Ick! What's going on? – A.Ellett Jul 7 '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 7 '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 8 '13 at 8:45
• what's wrong with \ensuremath? (not an answer to the question, but neither are many of the options offered so far.) – wasteofspace Jul 8 '13 at 10:37