40

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.

4
  • 3
    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
    Commented Oct 14, 2011 at 8:27
  • 1
    @daleif: Agreed. But (\sum_{\substack{i,j\\i<j}} ...) does not look good either. Commented Apr 30, 2012 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
    Commented Apr 30, 2012 at 20:27
  • \Big instead of \left and \right would perhaps be appropriate here, so you've got a point. Another example where replacing all ( with \left( and all ) with \right) completely fails is when you have a multi-line expression, split in for example in the array environment, and you want to fence the entire expression. If you try this with \left( and \right) you will generate an error, but using plain parenthesis or (e.g.) \Big( and \Big) works. Commented Feb 25, 2021 at 23:45

5 Answers 5

41

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

11
  • 1
    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)"? Commented Dec 16, 2011 at 1:16
  • 1
    @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
    – user2478
    Commented Dec 16, 2011 at 7:04
  • Is there any danger in using either Herbert's or Werner's solution in a custom package? Joe
    – user14095
    Commented Apr 30, 2012 at 18:24
  • 2
    To fix the unwanted space before the left parenthesis, I redefined \left and \right (before the code provided in this answer) per this answer to another question: tex.stackexchange.com/a/2610
    – Devin
    Commented Sep 24, 2014 at 20:06
  • 1
    The spacing in f(x) seems to be fixed by using the mleftright package and replacing \left with \mleft and \right with \mright in the code above. This is in the same vein as @Devin's comment. Commented Jul 26, 2017 at 5:02
23

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.

1
  • 1
    Note that even the mleftright does not solve the squeezing issue; it fixes only the immediate spacing around the delimiters.
    – bers
    Commented Jun 14, 2017 at 9:23
21

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

3
15

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

4
  • Thanks. Is it compatible with amsmath? I guess Herberts' solution goes on the same lines
    – Andro
    Commented Oct 14, 2011 at 10:35
  • 5
    @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". Commented Apr 30, 2012 at 19:05
  • @barbarabeeton: That's a valuable resource, thanks.
    – Werner
    Commented Apr 30, 2012 at 19:53
  • @Werner is it possible to do the same for square brackets and curly brackets?
    – sound wave
    Commented Aug 14, 2019 at 22:38
6

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.

2
  • 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
    Commented Oct 14, 2011 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
    Commented Oct 14, 2011 at 10:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .