I have a macro which appears throughout my document in various places:


Sometimes it is placed with regular text. Sometimes it is placed within other math modes. However, the document won't compile if it is placed inside another math mode.

None of my math ever appears in displayed formulas, it is always part of the paragraph.

  • How can I have a macro which sometimes appears inside math mode, sometimes outside, but itself contains math mode?
  • If memory serves me correctly, you want to look into using \ensuremath in your definition. A similar issue is discussed here.
    – DJP
    Dec 11, 2011 at 23:30
  • 2
    (In a friendly tone) Doesn't it take more effort to type \placefraction{1}{2} than it does \frac12 or even \frac{1}{2}? Or have I missed the point?
    – cmhughes
    Dec 11, 2011 at 23:43
  • 1
    When writing the question, I simplified the problem.
    – Village
    Dec 11, 2011 at 23:57

2 Answers 2


Use the \ensuremath macro


The % at the end of the line prevent extra spaces as Boris said in his answer.

But I wonder why you want this. In my eyes it’s more consistent to write all math in a math „environment“ (= markup). For example I’ll write

... Assuming that $x=0$ we can see that $\placefraction{1}{2}$ is obviously not
the same as $1+\placefraction{2}{8}-x$ ...

instead of

... Assuming that $x=0$ we can see that \placefraction{1}{2} is obviously not
the same as $1+\placefraction{2}{8}-x$ ...

because x=0, \placefraction{1}{2} and 1+\placefraction{2}{8}-x are from the same kind (math formulas) and should be treated in the same way, which is put both in $…$ in this case.

See Ryan’s comment …

  • 7
    +1 because you seem to be my allied in the campaign against the abuse of \ensuremath. :)
    – egreg
    Dec 11, 2011 at 23:33
  • What is the meaning of writing $\placefraction{1}{2}$ like $1+\placefraction{2}{8}-x$? Those make two different results, right?
    – Village
    Dec 12, 2011 at 0:03
  • 5
    @Village: Both of those are sample snippets of math, with similar content, and therefore should be written the same way. If, for example, your fraction one day needs to be put into a larger expression involving terms that must be in math mode, you have to change the appearance of your code. So using \ensuremath just encourages a confusing inconsistency in your writing. (I also share @egreg's campaign.)
    – Ryan Reich
    Dec 12, 2011 at 4:28
  • @Village: It’s exactly meant like Ryan said.
    – Tobi
    Dec 12, 2011 at 13:57

\ensuremath is intended for this purpose. Also, you may want to protect against spurious spaces:


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .