Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I'd like a macro that is the opposite of \ensuremath: something like \ensurenotmath, which makes sure that its argument is not typeset in math mode. Is there such a macro, or a way to implement it?

For my purposes, it would be sufficient to just detect math mode, so I could do something like:

\if\inmathmode
    % give sensible error message
\else
    % do the usual thing
\fi

Any ideas?

(Alternatively: I tried \show\ensuremath to see how it works, but it's protected. How can I use \show on such a macro?)

share|improve this question
3  
morbusg answered your real question. For the second question, protected macros come are defined by expanding to \protect\foo where there is actually a space in the name \foo . You can use \expandafter\show\csname foo \endcsname--note the space after foo--to see the definition. –  TH. Apr 7 '11 at 6:38
5  
Also, you can use \mbox{...} and the ... will be typeset in restricted horizontal mode, no matter what mode you're in outside the \mbox. –  TH. Apr 7 '11 at 6:39
    
@TH: thanks for the tip about \expandafter and \csname! –  Dan Drake Apr 7 '11 at 7:01
1  
You could also use the texdef script: texdef -t latex ensuremath (or latexdef ensuremath if the latedef symlink to texdef was created). It detects the protected status automatically and also show the macro with the space. You can also use it explicitly: texdef -t latex 'ensuremath ' –  Martin Scharrer Apr 7 '11 at 7:39
add comment

2 Answers 2

up vote 20 down vote accepted

\ifmmode <do_something>\else <do_something_else>\fi

share|improve this answer
    
Heh, you beat me by 2 seconds. –  TH. Apr 7 '11 at 6:36
    
@TH.: Haha, I know the feeling –  morbusg Apr 7 '11 at 6:41
    
Ack! I found \ifmmode right after posting this question. Thanks for the super-quick answer. –  Dan Drake Apr 7 '11 at 6:57
8  
Note, however, that if you reliably want to escape from math mode to text then you need to use an \mbox. This is nicely wrapped up by the amstext package as the \text macro. –  Joseph Wright Apr 7 '11 at 7:42
add comment

Simply using \text{} should ensure that you are not in math mode?

Joseph makes a very good point about text{} not being unrestricted horizontal mode, so the above will work only for short text snippets that don't need to cross line boundaries. I think a better solution would be:

\parbox{\linewidth}{}

share|improve this answer
1  
Note that this will be a box: you can't use this to get into unrestricted horizontal mode. –  Joseph Wright Aug 3 '11 at 18:37
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.