# Macros to make [ ] and ( ) automatically have \mleft and \mright

Since I'm writing a document with thousands of brackets of all kinds, manually sizing all the brackets or manually add \mleft and \mright every time will be need a lot of time in the end. Moreover, I don't have strict typographic rules to follow since the document is for private use.

So in this case it is not a bad idea to redefine ( and ) as \mleft( and \mright) respectively, and the same also for square and curly brackets (obviously in math mode only).

I'm aware about all the drawbacks about the use of auto resizing brackets (for example Spacing around \left and \right or Why doesn't LaTeX interpret ( as \left( and ) as \right)? or Is it ever bad to use \left and \right?), at least using \mleft and \right the spacing problem is solved.

In the page How to make \{ and \} automatically have \left and \right egreg explains that there is an easy way to achieve the goal in the case of curly brackets. The macro consists in adding \left and \right in the definitions of \{ and \} that are found in latex.ltx file.

\documentclass{article}
\usepackage{amsmath}
\usepackage{mleftright}

\let\{\relax \let\}\relax % to avoid warnings about the redefinition
\DeclareRobustCommand{\{}{\ifmmode\mleft\lbrace\else\textbraceleft\fi}
\DeclareRobustCommand{\}}{\ifmmode\mright\rbrace\else\textbraceright\fi}

\begin{document}
$a\{\dfrac13\}a$
\end{document}


I'm looking for a similar way to redefine the commands of curved and square brackets, but I don't know how to find their definitions in the file latex.ltx since it is a very big file.

• Well, they are not macros, but are defined as delimiters, see lines 162-164 of fontmath.ltx. – campa Aug 15 '19 at 9:28
• In my opinion, this is more of a problem for your editor… – Bernard Aug 15 '19 at 9:29
• You might be interested in the \DeclarePairedDelimiter, and its variants, command from mathtools. – Bernard Aug 15 '19 at 9:31
• the \{ redefinition will break any existing use of \left\{x\right\} which occurs in multiple packages, if not in your document, there are really only downsides to this. – David Carlisle Aug 15 '19 at 9:45
• The nath package does it by default, but nath display math clashes with amsmath, so it doesn't tend to be used much. – Aditya Aug 15 '19 at 10:48

You can make these symbols math active by setting their mathcode to 8000 and then define them by the lowercase trick:

\documentclass{article}
\usepackage{amsmath}
\usepackage{mleftright}

\makeatletter
\def\@mathactive#1#2{\begingroup
\lccode~=#1
\lowercase{\endgroup
\def~{#2}}%
\mathcode#1="8000\relax}

\@mathactive({\mleft(}
\@mathactive){\mright)}
% Do the same for brackets. (Do not try it with braces.)
\makeatother

\begin{document}
$(\dfrac12)$
\end{document}


Edit: Here is a code that works with PDFTeX. In PDFTeX, AMSMath seems to have a problem with active math chars.

\documentclass{article}
\usepackage{amsmath}
\usepackage{mleftright}

\begingroup
\catcode$$=\active \catcode$$=\active
\gdef({\mleft\lparen}
\gdef){\mright\rparen}
\endgroup

\def\lparen{\delimiter"028300\relax}
\def\rparen{\delimiter"029301\relax}

\everymath{\mathcode$$="8000 \mathcode$$="8000}
\everydisplay{\mathcode$$="8000 \mathcode$$="8000}

\begin{document}

$(\dfrac12)$

$(\frac18)$

\end{document}

• Yes, I took LuaTeX. Sorry for not mentioning this. – user125730 Aug 15 '19 at 15:07
• I have added a version that works with PDFTeX. – user125730 Aug 15 '19 at 15:48
• amsmath has no problem with active characters but has (see line 679) \mathchardef\@tempa\mathcode(\relax, which of course cannot work. But +1 anyway, even if I wouldn't recommend this even under torture :-) – campa Aug 15 '19 at 16:05
• Erm, I don't know how to get a backquote in code between backquotes... :-/` – campa Aug 15 '19 at 16:07
• I argree with you, I do not recommend this, too. But it is an answer to the question. :D – user125730 Aug 15 '19 at 16:12