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 know there has been discussion about this topic, but I haven't seen a working solution. Is it possible to define a macro within LaTeX, such that inside math mode, ( would produce \left( and ) would produce \right)?

I know it is possible to define a different macro, say \lparen, but it has the disadvantage of not being as visually clear, and also not as simple, as (. I would like to override the ( command only in math mode. This issue is pretty annoying when writing a lot of math.

share|improve this question
1  
In general it would be a bad idea: \left(\sum_{\substack{i,j\\i<j}} ...\right), just look stupid. Though nath is able to handle most of it. –  daleif Oct 14 '11 at 8:27
1  
@daleif: Agreed. But (\sum_{\substack{i,j\\i<j}} ...) does not look good either. –  Peter Grill Apr 30 '12 at 18:53
1  
never said I would not scale the fences. I usually imply the following rule: Scale the fences to such a degree that the reader has a clear distinction of what they are fencing in, but not to such a degree that the fences dominate the expression. –  daleif Apr 30 '12 at 20:27

5 Answers 5

up vote 24 down vote accepted

without using amsmath:

\documentclass{article}
\begingroup
  \catcode`(\active \xdef({\left\string(}  % ( is defined as \left(
  \catcode`)\active \xdef){\right\string)} % ) is defined as \right)
\endgroup
\mathcode`(="8000 \mathcode`)="8000        % active in mathmode

\begin{document}
\[ 
   (\frac{(3-\frac{1}{x})^2}{(\frac4x-1)x}) 
\]    
\end{document}

enter image description here

and if you are using package amsmath

\documentclass{article}
\usepackage{amsmath}
\makeatletter
\def\resetMathstrut@{%
  \setbox\z@\hbox{%
    \mathchardef\@tempa\mathcode`\[\relax
    \def\@tempb##1"##2##3{\the\textfont"##3\char"}%
    \expandafter\@tempb\meaning\@tempa \relax
  }%
  \ht\Mathstrutbox@\ht\z@ \dp\Mathstrutbox@\dp\z@}
\makeatother
\begingroup
  \catcode`(\active \xdef({\left\string(}
  \catcode`)\active \xdef){\right\string)}
\endgroup
\mathcode`(="8000 \mathcode`)="8000
\begin{document}

\begin{align}
 f(x) &= (\frac{(3-\frac{1}{x})^2}{(\frac4x-1)x}-1)x^2 \\
\end{align}

\end{document}

enter image description here

share|improve this answer
    
Thanks! it's working. –  Andro Oct 14 '11 at 10:34
    
Herbert, I really like this solution. I haven't run it yet (I just stumbled into this thread), but looking at the image provided, I noticed something that struck me as odd. Do you know why there is extra space between the "f" and the "(" in "f(x)"? –  Todd Lehman Dec 16 '11 at 1:16
    
@Todd, this is the tribute to \left which is used by default. If you don't want the space use f\!(x) &= in the above example –  Herbert Dec 16 '11 at 7:04
    
Is there any danger in using either Herbert's or Werner's solution in a custom package? Joe –  user14095 Apr 30 '12 at 18:24
    
@Joe: See my answer. –  Hendrik Vogt Jun 5 '12 at 12:32

Have a look at the nath package. It does automatic scaling of all delimiters. For example

\documentclass{article}
\usepackage{nath}

\begin{document}
\[ ( \sum_{i=1}^n a_i b_i ) \]
\end{document}

gives

enter image description here

share|improve this answer
1  
Thanks. it does work, but I heard there are compatibility problems with other packages. Is that true? –  Andro Oct 14 '11 at 10:33
1  
Yes, nath is incompatible with some packages. One of the biggest "incompatibilities" is that everybody assumes you will be using amsmath :-) However, I used it for my dissertation and I liked it a lot. –  Martin Geisler Dec 13 '11 at 14:43

I strongly advise against automatically replacing ( and ) with \left( and right). This has a serious drawback when formulas have to be squeezed in order to fit them into a line. The problem is that the stuff between \left( and right) is set with natural spacing, whereas the top level math list is subject to stretching and shrinking (see E-TeX: Guidelines for Future TeX Extensions by Frank Mittelbach, Section 11). See yourself:

comparison

In the first line, the space around the first + is shrunk to 0, whereas the space around the second + is set to the natural width. In the second line, both +'s are treated in the same way.

\documentclass{article}
\textwidth=4.715cm
\begin{document}
$a+\left(b+c\right)$ doesn't look good \par
$a+(b+c)$ doesn't\llap{\rule[0.6ex]{1.1em}{0.7pt}\hskip0.1em} look good
\end{document}

Let me point out that the above does not apply to AMS environments like align or gather since in those the spaces are always set to their natural width.

share|improve this answer

You can redefine the category codes for ( and ) to be \active. And then, condition on whether or not you're in math mode via \ifmmode...\else...\fi:

\documentclass{article}

\def\lparen{(}% left parenthesis (
\catcode`(=\active
\newcommand{(}{\ifmmode\left\lparen\else\lparen\fi}
\def\rparen{)}% right parenthesis )
\catcode`)=\active
\newcommand{)}{\ifmmode\right\rparen\else\rparen\fi}

\begin{document}
This is some regular (bracketed) stuff in text mode. Here is some stuff in $(xyz)$ text style math mode. Here is some display style math stuff:
\[
  (\sum_{i=1}^n i)
\]
\end{document}

enter image description here

share|improve this answer
    
Thanks. Is it compatible with amsmath? I guess Herberts' solution goes on the same lines –  Andro Oct 14 '11 at 10:35
3  
@Werner -- the trouble here is that the parens are really too big. quoting from the maple press style book (1931), p.152: "In mathematical expressions, braces, brackets and parentheses must be just large enough to enclose the widest part of the material between them without counting the superior or inferior exponents of such expressions." (here, "widest" means "tallest from top to bottom".) that's why knuth provided additional sizes, \big, \Big, etc., to fine tune expressions where \left and \right produce too large delimiters. programming would have been possible, but too "expensive". –  barbara beeton Apr 30 '12 at 19:05
    
@barbarabeeton: That's a valuable resource, thanks. –  Werner Apr 30 '12 at 19:53

I learned that making your file too "special" has its minus - in particular when you want to collaborate. Therefore, your source should be as simple as possible. In order to make the typing quicker, I got to know YASNIPPET for emacs; you can find similar tools, namely snippets, for other editors as well. For example in order to type

\( \left( x^2 \right) \)

I type:

m[TAB] pare[TAB]x^2[TAB][TAB]

yasnippet takes care of what I need according to the snippets I defined. This way I get a standard .tex file, easily entered. If you want further details, let me know.

share|improve this answer
    
There is a sense in what you're saying. I am using it especiallyt for myself, but there may be times when I'd want to export my code. Is this snippet working only on mac? I am working on a windows machine –  Andro Oct 14 '11 at 10:38
1  
This is a mode of emacs and thus cross platform. In order to work with my solution you'll have to install emacs+yasnippet. Together with AUCTeX and latexmk you get the perfect environment in my mind. It takes a little while to get used to, but worth it. Note the idea of snippets exists in other editors as well. –  Dror Oct 14 '11 at 10:52

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.