# Automatic matching and scaling delimiters

When I'm writing maths in TeX, I often have to use the scaling delimiters \left( and \right) and so on. I find the heavy use of them very unreadable in the source code.

Is there any possibility that when I write in math mode ( that it will have the effect that \left( normally has and the other way around? This way, the readablity of long or big formulae could be improved.

Here is a MWE:

\documentclass{article}
\begin{document}
$\displaystyle\left(\frac{t}{2}\right)$ for $t\in [0,1)$.
\end{document}


What I want is being able to write something like

\documentclass{article}
% Fancy code or package here
\begin{document}
$\displaystyle (\frac{t}{2})$ for $t\in \left[0,1\right)$.
\end{document}


and get the same output as above.

Non-matching delimiters like in the interval above are far more rare in mathematical texts, in my opinion. That is the reason I'd like to have this possibility.

Bonus question for the comments: Why is this a good/bad idea or how to circumvent this issue?

• You don't want \left[0,1\right). – egreg Jul 9 '14 at 9:44
• In general it is a bad idea because autoscaling often ends up too large. If used in text (as in $...$) autoscaling often ends up disturbing the line height. In displayed math consider \left(\sum_{\substack{i,j\\i>j}} ... \right) that is just way too large. In your example the [0,1) should not be scaled at all. So there is no real overhead. In most of my edits manual scalling is used where it makes sense and more often than not \left...\right is not used at all. – daleif Jul 9 '14 at 9:44

This example shows how to define ( and ) as active characters only in math environment. The \mathcode"8000 is the key of this.

{\catcode(=13 \catcode)=13
\gdef({\left\delimiter\delcode(}
\gdef){\right\delimiter\delcode)}
}
\mathcode(="8000 \mathcode)="8000
\def\nm{\mathchar}

Here is (normal) text.
$$(1\over2)+3, \quad \nm( {1\over2} \nm) + 3$$


First fraction will be surrounded by big brackets and second by normal brackets.

You can use the prefix \nm if you need to use the brackets in the normal way in math.

Comment: You can define char as active only in math mode if you do two things:

{\catcodechar=13 \gdef char{...} }.


It means that the activity of the char is only local when its activity is defined. But the definition is global. Second step is:

\mathcodechar="8000


This special number of \mathcode tells to TeX that the char have to be trated as active only in math mode.

The code above includes more triks. The prefix \nm is defined as

\mathchar


Thus \nm( expands to

\mathchar(


and this is treated as ( in math mode and the activity of the ( is irrelevant. Similar trick is used when ( is defined as:

\left\delimiter\delcode(


which is treated as \left( and the activity of the ( is irrelevant.

The problematic about mathcodes and delcodes is more extensive but I hope that my comment to this particular task is sufficient.

• Could you comment on what \mathcode"8000 does and how to expand this behaviour to other delimiters like [] and \{\}? – Wauzl Jul 9 '14 at 18:03
• @Wauzl Doing the same thing to [] is not as easy as this. Those brackets are used for optional arguments, which won't work correctly if [] have been touched. However, you can always define commands for this, \br{…} for “brackets” and, e.g., \pa{…} for “parenthesis”. – Manuel Jul 9 '14 at 19:12
• @Manuel Doing the same thing is easy for [] beacuse they are untouched, they are not active. They have only special \mathcode which influences only math mode when it is processed. – wipet Jul 9 '14 at 19:29
• @wipet But would something like \sqrt[3]{27} work? (Of course, I'm talking inside math mode.) – Manuel Jul 9 '14 at 19:31
• @Manuel Yes, it will work. Because \sqrt scans [] by their catcodes no mathcodes. – wipet Jul 9 '14 at 19:39

As the comments of egreg and deleif suggest, this probably a bad idea in general. If you really want to do it, however, then here is one way of modifying () to do what you want. Extending this idea to other delimiters is easy enough but by just doing () I can also highlight what needs to happen if your self-adjusting delimiters do not match.

Here is the code:

\documentclass{article}

\let\LeftBracket(
\let\RightBracket)
\catcode(=13
\catcode)=13
\def({\left\LeftBracket}
\def){\right\RightBracket}

\begin{document}

$\displaystyle(\frac{t}{2})$ for $t\in [0,1\RightBracket$

\end{document}


and here is the output:

Notice, for example, that you need to use \RightBrakcet whenever there is a non-matching delimiter for ). Of course, if [ expanded to \left[ then you could just use ) here.

Finally, it is quite likely that you will play havoc with other packages by redefining you delimiters in this way. For example, I doubt that the calc package would be happy with this.

• Defining ( and ) as math active characters would be better, but still it's conceptually wrong. – egreg Jul 9 '14 at 17:29
• @egreg Yes, I agree it shouldn't be done. Will edit to make active only in math mode – Andrew Jul 9 '14 at 17:38
• As wipet has already added math active delimiters, and more efficiently than I would have done so, I defer to his post:) – Andrew Jul 9 '14 at 17:56