Universal Parentheses

I know it's a bit of an odd request but it is one to which the solution would save me a lot of time.

I write a lot of documents in LaTex (especially maths - no surprise) and I prefer the look of

$\left(\right)$


parentheses on a lot of things because they actually encompass the things inside. For example:

$\deg\left(m_{\alpha_{1}/K}\right)$


looks better than

$\deg(m_{\alpha_{1}/K})$


in my opinion.

Unfortunately, writing \left and \right every time is a bit tiresome.

Is there any way that I can make them all do it automatically? There is a solution here: automatic size adjustment for nested parentheses but it interferes with tikz (presumably because it affects the parentheses).

I tried a solution like

• @StevenB.Segletes I forgot the closing parenthesis, sorry; but the problem is that ( and ) will not be active in that context. – egreg Mar 30 '17 at 11:09

An easy solution would just be to use the qty command from the physics package. Here's a MWE:

\documentclass{article}
\usepackage{physics}
\begin{document}
With \texttt{qty}:
$\qty(m_{\frac\alpha\beta}) + \qty(\frac{1}{x+y}) + \qty(\int^{\infty}_{0} x^2 \dd{x})$

Without \texttt{qty}:
$(m_{\frac\alpha\beta}) + (\frac{1}{x+y}) + (\int^{\infty}_{0} x^2 \dd{x})$
\end{document}


• This is very elegant. I assume there is no way of making this a universal operation within math mode? – PercyF2519 Mar 29 '17 at 12:09
• Not that I know of. The solution I propose here is merely a working alternative to what you were trying to implement. :) – Troy Mar 29 '17 at 12:10
• It is very nice - much nicer than the standard\left( \right) method. – PercyF2519 Mar 29 '17 at 12:14

It's definitely a bad idea, but if you really want to get ugly documents, here it is. ;-)

\documentclass{article}

\newcommand{\extlparen}{\left\delimiter"4028300 }
\newcommand{\extrparen}{\right\delimiter"5029301 }
\begingroup\lccode~=( \lowercase{\endgroup\let~}\extlparen
\begingroup\lccode~=) \lowercase{\endgroup\let~}\extrparen

\AtBeginDocument{\mathcode(="8000 \mathcode)="8000 }

\begin{document}
$\deg(m_{\alpha_{1}/K})+(\sum_{i=1}^n i^2)$
\end{document}


The same with proper markup:

$\deg(m_{\alpha_{1}/K})+\biggl(\,\sum_{i=1}^n i^2\biggr)$


The parentheses are much better in the second case than in the top one.

• Really? Honestly, I prefer the top case. Except the gap between deg and the parenthesis - but that is an issue I can solve. – PercyF2519 Mar 29 '17 at 15:54
• @PercyF2519 then try \left(\sum_{\substack{j<i\\ i\in A}}\right), trust us it is not a good idea in general. – daleif Mar 30 '17 at 10:38

While you ask about an automatic solution to this, i would still like to present a systematic Approach to this. The mathtools package allows you to define commands with paired delimiters. The starred variants of the command (the one you created) adds a \left and a \right:

\documentclass{article}

\usepackage{mathtools}
% 'braced' fraction:
\DeclarePairedDelimiterX\bfrac[2]{(}{)}{\frac{#1}{#2}}

\begin{document}
\begin{align*}
\bfrac{a}{x + c} + \bfrac*{a}{x + c}
\end{align*}
\end{document}


Sorry, i can't post a screenshot now, but the first fraction puts normal braces, the second one bigger ones (scaled with \left and \right).

Note that the starred Version of \bfrac was not defined.

One can do a lot of stuff with this, i recommend reading the respective part in the mathtools manual.

I think the mathtools package gives the perfect solution to the original problem. You put

\DeclarePairedDelimiter\paren{\lparen}{\rparen}

\paren*{\frac12}


The starred version allows the parentheses grow automatically. If you do not like how the parenthesis grows, you can specify the exact size

\paren[\Big]{\frac12}


Here is a full example

\documentclass{article}

\usepackage{mathtools}
\DeclarePairedDelimiter{\paren}{\lparen}{\rparen}

\begin{document}
Here with the starred version of the command, the parentheses grow
automatically, the nonstarred just gives the usual parens, and in the
third version with the 'Big' option, you specify the size by hand.

\begin{equation*}
\end{equation*}
\end{document}


You can see the output at http://wd369.csi.hu/apu/paren.png

Using plain TeX commands inside a LaTeX document, this is pretty straightforward:

\documentclass{article}

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

\begin{document}

$\deg(m_{\alpha_{1}/K})+(\sum_{i=1}^n i^2)$

In the rare case that you need normal parentheses:

$\deg\lparen m_{\alpha_{1}/K}\rparen+\lparen\sum_{i=1}^n i^2\rparen$

This is (pretty) normal text.

\end{document}


It's just making \lparen an alias for an ordinary parenthesis and then redefining it. After that, you can still use the \lparen macro, just in case you don't like the active parentheses.

Another option is to use the nath package:

\documentclass{article}
\usepackage{nath}
\begin{document}
$$\deg(\frac{m_{\alpha_{1}}}{K}) + (\sum_{i=1}^n i^2)$$
$\deg(\frac{m_{\alpha_{1}}}{K}) + (\sum_{i=1}^n i^2)$
\end{document}


which gives:

Note that the scaling of delimiters is better than the default scaling with \left( ... \right). As an added bonus, \frac behaves intelligently in inline vs display mode.

• Unfortunately, nath is incompatible with so many packages… – egreg Mar 29 '17 at 22:32
• Only with amsmath; what else :-) It still appears to be a better solution than making ( active and mapping it to \left(. – Aditya Mar 29 '17 at 23:17
• Incompatibility with amsmath is something of a deal-breaker for a mathematics package, no? – David Richerby Mar 30 '17 at 19:25
• @DavidRicherby: It depends. I believe that nath works if you do not use multiline display environments from amsmath. It is not an ideal solution but a trade-off. For some people, the drawbacks outweigh the gains; but for others it is the other way round. Depends on what type of math environment you need. I use nath for notes, assignment, and exams for undergrad level EE courses, where nath gives correct (or rather better than default) scaling of delimiters without too much thought. For research papers, I make heavy use to amsmath and don't use nath. So, .... it depends. – Aditya Mar 30 '17 at 23:28

I often use the xparse package to define operators that can take parenthesized input, but if they don't find that input, they treat themselves as not having any input.

\documentclass{article}
\usepackage{mathtools}
\usepackage{xparse}
\RenewDocumentCommand\deg{d()}{\ensuremath{\operatorname{deg}\IfValueT{#1}{\left(#1\right)}}}
\begin{document}
The degree $$\deg$$ of the vector bundle $$V$$ is
$\deg(V) = 1.$
Its slope is $$\deg/\operatorname{rank}$$.
\end{document}

• why \ensuremath when you are always using it in math mode? – daleif Mar 30 '17 at 10:38

Something that I use is the following:

\newcommand\del[1]{\left( #1 \right)}
\newcommand\sbr[1]{\left[ #1 \right]}
\newcommand\cbr[1]{\left\{ #1 \right\}}
\newcommand\abr[1]{\left\langle #1 \right\rangle}


Then in the code, one can just use \del{ ... } and get delimiters. Also I can just change del to sbr if I want to have square brackets.

The package commath also defines very similar commands, but that package has very mixed reviews.

I personally use the vim-latex suite. It makes your life easier in many ways, and on of them is the automatic replacement for brackets, underscores and circumflexes if typed doubled, so that (( becomes \left( \right)<++>, as well as for example __ becomes _{}<++>.

So it's always you who decides if to use the scaled version, but if you want it, it's only a second opening bracket away.

May not be the best solution at all but you can try editing your document with TexStudio. It is a LaTeX editor which has a special tab to insert delimiters and other practical things in your document.