5

In my thesis I'm working with a lot of brackets around fractions and every time I have to use \left( \frac{a}{b} \right). It can get really confusing if i have a long equation and a lot of fractions.

Is there a way to redefine ( such that every time I type (, it automatically gets interpreted as \left(?

I tried to redefine it as a macro, but it doesn't seem to work with brackets.

Thank you for your suggestions.

2 Answers2

11

Redefining the meaning of the parentheses, ( and ), may be rather dangerous since they can occur in so many contexts, even in math material. There may also be situations where you would not want the parentheses to be enlarged automatically.

Rather than redefining the meaning of the parentheses, you may want to consider defining a new macro called, say, \pfrac (short for "parenthetical fraction", I suppose) that encloses the fraction expression automatically in auto-sized parentheses:

\newcommand{\pfrac}[2]{\left( \frac{#1}{#2} \right)}

This will let you write expressions such as \pfrac{1}{2}. Observe that this is marginally simpler (and, importantly, less prone to "accidents" caused by omitted opening and/or closing parentheses) than writing (\frac{1}{2}).

If you go this route, i.e., have lots and lots of fractional expressions encased in auto-sized parentheses, I would recommend you also load the mleftright package and issue the command \mleftright in the preamble. Doing so gets rid of the extra space inserted in various situations by \left and \right.

enter image description here

\documentclass{article}
\usepackage{amsmath}
\newcommand{\pfrac}[2]{\left(\frac{#1}{#2}\right)}
\usepackage{mleftright}
\mleftright
\begin{document}
\[ 
\pfrac{1}{2} \pfrac{3}{4} \pfrac{5}{6} 
\textstyle \pfrac{1}{2} \pfrac{3}{4} \pfrac{5}{6} 
\]
\end{document}

Addendum, prompted by a comment by egreg: One could also utilize the macro \genfrac of the amsmath package to define a fraction macro that automatically surrounds its contents with a pair of parentheses. For instance, one could define

\newcommand{\qfrac}[2]{\genfrac {(} {)} {} {} {#1} {#2}}

Substituting this macro in place of \pfrac in the MWE above yields:

enter image description here

One notes that the parentheses are now set very close to the fraction expressions. You may (or may not) prefer this look to that produced by the \pfrac macro given above.

Mico
  • 506,678
  • 2
    There is already \genfrac that can make fractions with suitable delimiters, so defining \pfrac is best done in terms of \genfrac. – egreg Sep 21 '13 at 19:11
  • @egreg - Thanks for this suggestion. I've provided an addendum that shows how one could use the \genfrac approach. The parentheses end up being typeset much closer to the fractions they enclose than if one uses the \pfrac macro set up in the initial part of the answer. – Mico Sep 21 '13 at 19:42
3

A first attempt (without restriction to math mode etc.):

\documentclass{article}

\begin{document}

\let\lb=(
\let\rb=)

\catcode`\(=\active
\def({\left\lb}
\catcode`\)=\active
\def){\right\rb}

$$
\lb\frac{a}{b}\rb
\quad
(\frac{a}{b})
$$


\end{document}
  • 3
    This will break many things. Maybe making ( and ) math active is less risky. I can't recommend it, though. – egreg Sep 21 '13 at 19:09
  • @egreg Certainly, Mico's solution is safer. But if the OP really needs \left-like brackets, additional restrictions in definition and suggested limitations of usage can be added. – Przemysław Scherwentke Sep 21 '13 at 19:16
  • And what do you do if you need \makebox(0,0){text}? Or, more simply, parentheses in text? – egreg Sep 21 '13 at 19:17