# Automatic left and right commands

Is it possible to automatically "add" \left and \right commands in front of all brackets? The source code should be left intact, but LaTeX should interpret every pair of brackets as if they were preceded by such commands. This is because adding \left and \right commands is time consuming and makes source code longer and less readable. I considered defining new commands such as \lb and \rb for \left( and \right), but it doesn't seem very elegant to me: what if I also want to add square brackets for example?

Any ideas? Thanks.

• I strongly advise against automatically replacing ( and ) with \left( and right) since it can cause spacing problems. For details see this answer. – Hendrik Vogt Jun 5 '12 at 12:46

The \DeclarePairedDelimiter command from mathtools is useful. For example defining an absolute value macro;

\DeclarePairedDelimiter\abs{\lvert}{\rvert}


gives also a starred version with \left and \right

\abs*{\frac{a}{b}}


and the unstarred version takes an optional argument that can be \big, \Big, etc

\abs[\Bigg]{\frac{a}{b}}


Adding \left and \right to all delimiters is difficult and likely not to yield the desired result in many places.

The nath package aims at making \left and \right obsolete. From the readme:

delimiters adapt their size to the material enclosed, rendering \left and \right almost obsolete.

Edit: I'm leaving this answer up for “academic purposes” but, consider Lev Bishop's answer for an easier solution than mine.

Over the years I've evolved my own solution which I think turned out quite nice. It gives you a command which you can call like

\newdelimcommand{parens}{(}{)}


which in turn will define for you a \parens command which you can then use as \parens{thing} to produce something like (thing) where, moreover, the parenthesis automatically resize. In fact you can use the command in three different ways

\parens{thing}       % automatic resize
\parens*{thing}      % no resize
\parens[big]{thing}  % use the specified size


For the latter option you can use auto (the default), base (no resize), or one of the sizes: big, Big, bigg, Bigg.

Furthermore, inside of thing you can use the commands \ldelim, \rdelim, and \mdelim to create even more delimiters with the appropriate size selected by the given options. This allows, for example, to use \mdelim to automagically resize a “middle” bar. (See example at the end of the code).

For this to work you have to drop the following code in a .sty file and include it in your main .tex file with \usepackage.

\RequirePackage{amsmath}

% delim sizing options
\let\delim@autol\left  \let\delim@autor\right \let\delim@autom\middle
\let\delim@basel\relax \let\delim@baser\relax \let\delim@basem\relax
\let\delim@bigl\bigl   \let\delim@bigr\bigr   \let\delim@bigm\big
\let\delim@Bigl\Bigl   \let\delim@Bigr\Bigr   \let\delim@Bigm\Big
\let\delim@biggl\biggl \let\delim@biggr\biggr \let\delim@biggm\bigg
\let\delim@Biggl\Biggl \let\delim@Biggr\Biggr \let\delim@Biggm\Bigg

% default definitions
\newcommand\ldelim{\relax}
\newcommand\rdelim{\relax}
\newcommand\mdelim{\relax}

% the actual command
\newcommand\delim@command[4]{{%   #1 size #2 ldelim #3 rdelim #4 content
\def\ldelim{\csname delim@#1l\endcsname}%
\def\rdelim{\csname delim@#1r\endcsname}%
\def\mdelim{\csname delim@#1m\endcsname}%
\ldelim#2#4\rdelim#3}}

% a factory to define new delimiter commands
\newcommand{\newdelimcommand}[3]{% #1 name #2 ldelim #3 rdelim
\expandafter\newcommand\csname delim@#1@st\endcsname[1]{% ##1 content
\delim@command{base}{#2}{#3}{##1}}%
\expandafter\newcommand\csname delim@#1@ns\endcsname[2][auto]{%
% ##1 size ##2 content
\delim@command{##1}{#2}{#3}{##2}}%
\expandafter\DeclareRobustCommand\csname#1\endcsname{%
\@ifstar{\csname delim@#1@st\endcsname}{\csname delim@#1@ns\endcsname}%
}%
}

% syntactically named delimiters
\newdelimcommand{braces}{\lbrace}{\rbrace}
\newdelimcommand{angles}{\langle}{\rangle}
\newdelimcommand{verts}{\lvert}{\rvert}
\newdelimcommand{Verts}{\lVert}{\rVert}
\newdelimcommand{brackets}{[}{]}

% semantically named delimiters
\newcommand{\set}{\braces}
\newcommand{\abs}{\verts}
\newcommand{\size}{\verts}
\newcommand{\norm}{\Verts}
\newcommand{\tuple}{\angles}

% Automagic such that' for set comprehension. Inside an automagic
% delimiter command, the vertical bar will resize appropriately
% Example:
%   \set{ x \in W \st x > 0 }
\newcommand{\st}{\;\mdelim\vert\;}


I've been thinking to eventually release this (and other bits of code) as a proper style file. But for the time being here it is for you to copy&paste. :)

• This looks very much like \DeclarePairedDelimiter from mathtools, except that the meaning of starred and unstarred is reversed. – Lev Bishop Aug 15 '10 at 13:00
• Reading this question I wondered if it was possible to make use of \mleft and \mright in this code. I prefer this over mathtools because the meaning of starred and unstarred is reversed and because the manual input is easier ([bigg] vs. [\bigg]). Any ideas? – Manuel Jan 12 '13 at 20:02
• I guess you could just drop \mleft instead of \left, etc. But probably you want it to sometimes use \left and sometimes \mleft, right? Then you have to come up with an interface that allows you to select between the two. – Juan A. Navarro Jan 12 '13 at 20:37
• @JuanA.Navarro no, I always would like to use \mleft. I tried loading \usepackage{mleftright}\mleftright, where the latter command changes the meaning of \left to \mleft, before your code. But it doesn't work. – Manuel Jan 15 '13 at 16:21
• I've just tried it now and it does seem to compile fine without problems. Are you getting some errors or unexpected behaviour? – Juan A. Navarro Jan 15 '13 at 19:31

To make auto-sized bracketing as painless and concise as possible, here's what I do:

\usepackage{mathtools}

\DeclarePairedDelimiter\autobracket{(}{)}
\newcommand{\br}[1]{\autobracket*{#1}}


Example:

$$\br{\frac{1}{2}} + \br{1} - \br{\frac{\int_0^{2\pi} f(x)}{\sqrt[2]{\frac{1}{2}}}}$$


I use the following newcommands:

\newcommand{\of}[1]{\left(#1\right)}
\newcommand{\off}[1]{\left[#1\right]}
\newcommand{\offf}[1]{\left\{#1\right\}}


Hence, in the text, f\of{x} will appear as f(x), M\ast\offf{g\off{x}+h\off{x}} will appear as M*{g[x]+h[x]}, etc. with the \left and \right commands automatically built-in.

• you might find that \mathopen{}\left(, etc., gives you better spacing (but that might be a personal preference). – Will Robertson Sep 2 '10 at 1:15

There is another possibility to do it, if the automatic size should be the default (as here) and explicit sizes should be easier to remember (numbers 0 till 4 instead of exotic names)

\documentclass{article}

\usepackage{amsmath}

% Enable the new LaTeX 3 commands.
\usepackage{xparse}

% Provide the \alias command substituting the \let command by working with optional arguments.
\usepackage{letltxmacro}
\LetLtxMacro{\alias}{\LetLtxMacro}

% Define a clear and useful programming interface. You should define \new, \renew and \provide similarly and use the correct one below.
\alias{\declare}{\DeclareDocumentCommand}

% Fix the spacing around \left and \right: https://tex.stackexchange.com/questions/2607/spacing-around-left-and-right
\alias{\leftOld}{\left}
\alias{\rightOld}{\right}
\declare{\left}{}{\mathopen{}\mathclose\bgroup\leftOld}
\declare{\right}{}{\aftergroup\egroup\rightOld}

% Usage: \newdelimited{functionName}{left delimiter}{right delimiter}
% Example: \newdelimited{parens}{(}{)}
%
% Usage of inner command: \functionName[explicit size]{delimited content}
% An auto sized delimiter is used, if no explicit size is given. Explicit sizes range from 0 (small) to 4 (huge)
% Examples: \parens{x}, \parens{\frac{x}{y}}, \parens[3]{x}, \parens[0]{\frac{x}{y}}
%
\declare{\newdelimited}{mmm}{%
\declare{#1}{om}{%
\IfNoValueTF{##1}{%
\left#2 ##2 \right#3%
}{%
% Use the amsmath delimiter sizes to have proper scaling all the time.
\ifcase##1\relax%
#2 ##2 #3%
\or \bigl#2 ##2 \bigr#3%
\or \Bigl#2 ##2 \Bigr#3%
\or \biggl#2 ##2 \biggr#3%
\or \Biggl#2 ##2 \Biggr#3%
\else \PackageError{newdelimited}{Only optional values 0, 1, 2, 3 and 4 are supported.}%
\fi%
}%
}%
}

\begin{document}
\newdelimited{\parens}{(}{)}
$\parens{x}, \parens{\frac{M}{W}}, \parens[3]{x}, \parens[0]{\frac{M}{W}}$
\end{document}


The result looks like this:

It seems to me that it's easy to do exactly what you say, but you might not really want that; see the caveat below.

\let\lparen=(\let\rparen=)
\catcode(=\active\catcode)=\active
\def({\left\lparen}\def){\right\rparen}


The catch: There are some obvious disadvantages to this, such as that parentheses now only work in math mode; any text-mode or un-stretchy parentheses have to be entered as \lparen\rparen. You may be willing to do this in your own code, but you'll have to be quite careful about any external packages you include. (If you were really dedicated, you could have something like a \stretchparens / \plainparens pair to turn this behaviour on and off.)

I wonder if those wiser than I could comment on subtler disadvantages?

• Parentheses are widely used in text, so this approach is not recommendable; rather make the parentheses math active (mathcode "8000). My opinion is that automatically providing \left and \right is A Bad Thing. – egreg May 7 '12 at 6:43

I always use some commands. For example:

\newcommand{\angles}[1]{\left\lange #1 \right\rangle}%      ⟨⟩
\newcommand{\braces}[1]{\left\lbrace #1 \right\rbrace}%     {}
\newcommand{\bracks}[1]{\left\lbrack #1 \right\rbrack}%     []
\newcommand{\pars}[1]{\left( #1 \right)}%                   ()
\newcommand{\ds}[1]{\displaystyle{#1}}%
\newcommand{\+}{^{\dagger}}%
\newcommand{\partiald}[3][]{{\partial^{#1}#2 \over \partial {#3}^{#1}}}%


It works like this:

• \pars{\Huge A}
• \partiald[3]{y\pars{x,t}}{t}
• A\\+
• etc.

You can leave them in a file (for example myMacros.tex) and use \input in the main document: \input{pathToFile/myMacros.tex}`.