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.

Is there any way to make LaTeX automatically put parentheses around a \frac if it is followed by a superscript expression?

share|improve this question
add comment

1 Answer

up vote 12 down vote accepted

You could redefine \frac to look ahead:

\documentclass{article}
\makeatletter
\let\old@frac=\frac
\def\frac#1#2{%
 \@ifnextchar^
  {\left(\old@frac{#1}{#2}\right)}
  {\old@frac{#1}{#2}}%
}
\makeatother
\begin{document}
 $\frac{1}{2}, \frac{1}{2}^2$. 

 \[\frac{1}{2}, \frac{1}{2}^2\]
\end{document}

This gives:

enter image description here

share|improve this answer
2  
Genius!! I was about to comment that it can't be done, but thought it better to wait... Not that I think this is a good idea to do, but good to know that it can be done. –  Peter Grill Feb 1 '12 at 2:24
    
Genius indeed! Thanks a lot! –  Mehrdad Feb 1 '12 at 2:34
1  
@PeterGrill: Funny thing is, I was also about to comment that it can't be done. I also agree that it isn't a good idea, but I didn't say that in my answer because I couldn't actually think of a concrete reason why. It just seems wrong. –  Ryan Reich Feb 1 '12 at 2:43
    
I think it is a personal preference thing. I don't like seeing \frac12 even though it produces 1/2, but perhaps others do. –  Peter Grill Feb 1 '12 at 2:47
    
Oh, I agree about that for sure, but the thing with parentheses seems more questionable. In other contexts I'd say it is enforcing a default choice you may not always want, but I really can't think of when one would want a fraction with a superscript but no parentheses; it's always ambiguous. –  Ryan Reich Feb 1 '12 at 2:57
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.