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 have a macro which appears throughout my document in various places:

\newcommand{\placefraction}[2]{
    $\frac{#1}{#2}$
}

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?
share|improve this question
    
If memory serves me correctly, you want to look into using \ensuremath in your definition. A similar issue is discussed here. –  DJP Dec 11 '11 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 '11 at 23:43
1  
When writing the question, I simplified the problem. –  Village Dec 11 '11 at 23:57
add comment

2 Answers

up vote 22 down vote accepted

Use the \ensuremath macro

\newcommand{\placefraction}[2]{%
    \ensuremath{\frac{#1}{#2}}%
}

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 …

share|improve this answer
5  
+1 because you seem to be my allied in the campaign against the abuse of \ensuremath. :) –  egreg Dec 11 '11 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 '11 at 0:03
2  
@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 '11 at 4:28
    
@Village: It’s exactly meant like Ryan said. –  Tobi Dec 12 '11 at 13:57
    
@RyanReich: Thanks. :-) –  Tobi Dec 12 '11 at 13:57
add comment

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

\newcommand{\placefraction}[2]{%
    \ensuremath{\frac{#1}{#2}}%
}
share|improve this answer
3  
+1 for the hint about spurious spaces. –  lockstep Dec 11 '11 at 23:29
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.