# How to automatically add parentheses around fractions?

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

-

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:

-
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
@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