6

I'd like to be able to make a macro \foo whose definition would look something like

\newcommand{\foo}{
  \ifintextmode{abc}
  \ifinmathmode{x\cdot y\otimes z}
}

so that

\foo $\foo$

produces

enter image description here

This TeX.SE question seems somewhat related, but I didn't quite understand the answer and it does not directly solve my issue. I'm sorry I don't have anything more to offer, in terms of making any progress myself.

Zev Chonoles
  • 5,466
  • 5
  • 31
  • 52
  • 1
    Can you make a significant example for this? – egreg Apr 02 '13 at 22:05
  • The question you linked at is trying to do the opposite : detect if there is math inside the arguments of the macro. Its solution could be adapted if there was no predefined macro for this, though. – T. Verron Apr 02 '13 at 22:05
  • 1
    @egreg: I'm not sure what you mean - are you asking how I'm actually planning to use this? If so, the use I had in mind was making the macro \adeles produce ad\'eles in text mode and \mathbb{A} in math mode. I generally try to abstract such specifics away in my questions, if they don't seem like they'd be important. – Zev Chonoles Apr 02 '13 at 22:09
  • @ZevChonoles That's what I feared. You gain nothing by having a macro that does very different things according to the context. You're bound to lose track of where you are. But of course it's only my opinion. After 25+ years of TeX. ;-) – egreg Apr 02 '13 at 22:39
  • @egreg: But wouldn't the use I proposed, at least, be a very semantic definition? In text mode, it produces the word (with the benefit that I don't have to remember what command makes the necessary accent), and in math mode, it makes the symbol that represents the word. I wouldn't really like having separate commands like \adeletext and \adelesymbol; though maybe TeX would like it, I think it would be harder for me to parse when reading the code directly. – Zev Chonoles Apr 02 '13 at 22:43
  • @ZevChonoles Are all of your adèles called \mathbb{A}? Semantically, the name of a class of objects is different from the name of an object in the class. By the way, "adèle" is spelled with a grave accent: ad\`ele. – egreg Apr 02 '13 at 22:47
  • @egreg: Well, it's a good thing I've been using the command \adele to represent the word this whole time, my mistake will be easy to clean up! :) – Zev Chonoles Apr 02 '13 at 22:49
  • @egreg: Well, I defer to your opinion. Do you consider it acceptable to use a macro to avoid typing accents, in general? It seems like a good idea to me, especially given the mistake you've pointed out to me. Would \newcommand{\ringofadeles}[1][]{\mathbb{A}_{#1}} and \newcommand{\adeles}{ad\\eles}` be okay? – Zev Chonoles Apr 02 '13 at 22:57
  • @ZevChonoles This is more consistent, in my opinion. – egreg Apr 02 '13 at 23:28

1 Answers1

12
\DeclareRobustCommand{\foo}{%
  \ifmmode
   x\cdot y\otimes z%
  \else
  abc%
   \fi
}

I should say though it's generally a bad idea to do this (or use the similar \ensuremath command) as TeX has a built in distinction between text and math and really the author needs to know at all times whether the document is in text or math mode, and use suitable commands in each case.

Consider the accent commands; \hat could have been defined as above to work in text or math mode, but instead plain TeX (and LaTeX) define \hat for math mode and \^ for text mode. The implementation and semantics of the two commands are very different and superficially using the same syntax for text and math would not have been helpful.

David Carlisle
  • 757,742
  • In addition, I strongly suggest to implement the macro in a single line as so: \DeclareRobustCommand{\forcemmode}[1]{\ifmmode#1\else$#1$\fi} to avoid any troubles with extra inserted spaces or tab. – Welgriv May 24 '22 at 09:34
  • @Welgriv I wouldn't, it makes the code very hard to maintain, and the version as I posted here will not add any spaces. – David Carlisle May 24 '22 at 09:38