17

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

$\newcommand{\(}{\left(}$

but LaTeX didn't like that.

  • 3
    Note that \( is already an existing, important LaTeX command for entering textstyle math mode, and should not be usurped. – Steven B. Segletes Mar 29 '17 at 12:01
  • 3
    Note that automatic resizing is not generally recommended as they can often get too large, e.g. \left(\sum_i\right) looks ridiculous, and used in inline math, they can easily grow so much that they interfere with line spacing. – daleif Mar 29 '17 at 12:57
  • 1
    Pardon me if this isn't what you're looking for, but to offer a simple solution: have you considered AutoHotkey and just doing something like Alt + Shift + ( > \left? – Luke Sawczak Mar 29 '17 at 15:09
  • 2
    @LukeSawczak Who said anything about the asker using Windows? – David Richerby Mar 30 '17 at 19:23
  • 1
    another drawback might be that a linebreak is not possible between \left and \right. – jakun Apr 5 '17 at 7:18

11 Answers 11

18

Making ( active is not really a solution because it is used in so many other ways, but here is how it would be done.

To help in this regard. I create \pactive to make ( active which, if used inside a group like an equation, can revert ( to its normal definition after the group is closed.

\documentclass{article}
\let\svlp(
\catcode`(=\active %
\def(#1){\left\svlp#1\right)}
\catcode`(=12 %
\newcommand\pactive{\catcode`(=\active }
\begin{document}
\[
\pactive
\deg(m_{\alpha_{1}/K}) + (\frac{a}{x+c})
\]
Normal use of ( or even \fbox{\makebox(20,30){test}}
\end{document}

enter image description here

The OP asks about automating the invocation of \pactive. Again, I don't recommend it, because there may be need in math mode to use the () set as an argument to some macro. But the use of

\everymath{\pactive}
\everydisplay{\pactive}

will accomplish it:

\documentclass{article}
\let\svlp(
\catcode`(=\active %
\def(#1){\left\svlp#1\right)}
\catcode`(=12 %
\newcommand\pactive{\catcode`(=\active }
\everymath{\pactive}
\everydisplay{\pactive}
\begin{document}
\[
%\pactive
\deg(m_{\alpha_{1}/K}) + (\frac{a}{x+c})
\]
Normal use of ( or even \fbox{\makebox(20,30){test}}

\(
%\pactive
\deg(m_{\alpha_{1}/K}) + (\frac{a}{x+c})
\)
Normal use of ( or even \fbox{\makebox(20,30){test}}
\end{document}

enter image description here

Thanks to Timm for reminding that, if one really wants to automate this, then one should have handy the override macro

\newcommand\pinactive{\catcode`(=12 }
  • 1
    Is there no way to restrict this active behaviour to the math mode? Edit: I guess your edit is a suitable solution. – Timm Mar 29 '17 at 12:00
  • This is definitely getting there. I know this is asking a lot now but is there a way to do this without having to put \pactive at the beginning of every math mode? For instance, is it possible to define the group you are opening to have \pactive automatically? – PercyF2519 Mar 29 '17 at 12:06
  • 1
    @Timm Yes it would certainly be reasonable to define \newcommand\pinactive{\catcode`(=12 }, the only difference from yours is a space after the 12, which I seem to recall egreg always stressed could be important in some circumstances that elude me. – Steven B. Segletes Mar 29 '17 at 12:50
  • 1
    Try \mbox{$\displaystyle(\frac{1}{2}$} – egreg Mar 29 '17 at 19:51
  • 1
    @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
13

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}

with and without qty

  • 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
  • 1
    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
7

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}

enter image description here

The same with proper markup:

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

enter image description here

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

  • 1
    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
  • 3
    @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
3

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.

2

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*}
  \paren*{\frac12} \quad \paren{\frac12} \quad \paren[\Big]{\frac12} 
\end{equation*}
\end{document}

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

1

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.

1

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: enter image description here

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
1

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.

Image of the tex output

\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}
  • 3
    why \ensuremath when you are always using it in math mode? – daleif Mar 30 '17 at 10:38
0

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.

0

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.

0

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.

Tabs

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.